Python client for the Fragment API
Project description
Fragment API Python
Professional Python library for Fragment.com API with async and sync support. Send Telegram Stars, Premium, and TON with automatic wallet validation.
Features
- ✅ Async & Sync interfaces
- ✅ 3 payment methods (Stars, Premium, TON)
- ✅ Recipient lookup (get user info & avatar)
- ✅ Automatic wallet balance validation
- ✅ WalletV4R2 support
- ✅ Comprehensive error handling
- ✅ Type hints & logging
Installation
pip install fragment-api-py
Quick Start
Synchronous
from FragmentAPI import SyncFragmentAPI
api = SyncFragmentAPI(
cookies="your_cookies",
hash_value="your_hash",
wallet_mnemonic="your mnemonic...",
wallet_api_key="your_api_key"
)
# Get recipient info with avatar
recipient = api.get_recipient_stars('username')
print(f"Name: {recipient.name}")
print(f"Avatar: {recipient.avatar}")
# Send stars
result = api.buy_stars('username', 100)
if result.success:
print(f"TX: {result.transaction_hash}")
api.close()
Asynchronous
import asyncio
from FragmentAPI import AsyncFragmentAPI
async def main():
api = AsyncFragmentAPI(
cookies="your_cookies",
hash_value="your_hash",
wallet_mnemonic="your mnemonic...",
wallet_api_key="your_api_key"
)
# Get recipient info with avatar
recipient = await api.get_recipient_stars('username')
print(f"Name: {recipient.name}")
print(f"Avatar: {recipient.avatar}")
# Send stars
result = await api.buy_stars('username', 100)
if result.success:
print(f"TX: {result.transaction_hash}")
await api.close()
asyncio.run(main())
Methods
Recipient Lookup
get_recipient_stars(username)
Get recipient info for Stars transfer
- Returns:
UserInfowith name, recipient address, avatar (URL or base64)
get_recipient_premium(username)
Get recipient info for Premium gift
- Returns:
UserInfowith name, recipient address, avatar
get_recipient_ton(username)
Get recipient info for Ads account top-up
- Returns:
UserInfowith name, recipient address, avatar
Payments
buy_stars(username, quantity)
Send Telegram Stars to user
username(str): Target usernamequantity(int): Number of stars (1-999999)
gift_premium(username, months)
Gift Premium subscription (3, 6, or 12 months)
username(str): Target usernamemonths(int): Duration in months
topup_ton(username, amount)
Top up Telegram Ads account with TON
username(str): Target usernameamount(int): Amount of TON (1-999999)
Wallet
get_wallet_balance()
Get current wallet balance and address
- Returns: Dictionary with balance in TON/nanotons and address
Recipient Info
# Get user info before sending payment
user = api.get_recipient_stars('john_doe')
if user.found:
print(f"Name: {user.name}")
print(f"Address: {user.recipient}")
print(f"Avatar: {user.avatar}") # URL or base64 encoded image
UserInfo object:
name- User's display namerecipient- Blockchain addressfound- Boolean flagavatar- Avatar URL (HTTP/HTTPS) or base64 encoded image
Setup
1. Get Fragment Cookies
- Open fragment.com in browser
- Press F12 → Application → Cookies
- Copy cookies:
stel_ssid,stel_token,stel_dt,stel_ton_token - Combine:
stel_ssid=value; stel_token=value; ...
2. Get Hash Value
From DevTools Network tab → fragment.com/api requests → copy hash parameter
3. Setup TON Wallet
- Get 24-word seed phrase from TON wallet (Tonkeeper, MyTonWallet, etc)
- Fund wallet with TON for transactions
4. Get API Key
- Go to https://tonconsole.com
- Create project
- Generate API key from Settings
Exceptions
AuthenticationError- Invalid cookies/hashUserNotFoundError- User doesn't existInvalidAmountError- Invalid quantity/amountInsufficientBalanceError- Low wallet balanceTransactionError- TX execution failedNetworkError- Network request failedWalletError- Wallet operation failedPaymentInitiationError- Fragment API error
Error Handling
from FragmentAPI import InsufficientBalanceError, UserNotFoundError
try:
# Get recipient info
user = api.get_recipient_stars('username')
# Send payment
result = api.buy_stars('username', 100)
if not result.success:
print(f"Error: {result.error}")
except InsufficientBalanceError as e:
print(f"Low balance: {e}")
except UserNotFoundError as e:
print(f"User not found: {e}")
finally:
api.close()
Security
Store credentials in environment variables:
import os
api = SyncFragmentAPI(
cookies=os.getenv('FRAGMENT_COOKIES'),
hash_value=os.getenv('FRAGMENT_HASH'),
wallet_mnemonic=os.getenv('WALLET_MNEMONIC'),
wallet_api_key=os.getenv('WALLET_API_KEY')
)
Requirements
- Python 3.7+
- requests >= 2.28.0
- aiohttp >= 3.8.0
- tonutils >= 0.3.0
- pytoniq-core >= 0.1.0
Documentation
Full documentation: https://github.com/S1qwy/fragment-api-py
License
MIT License
Support
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fragment_api_py-3.0.3.tar.gz.
File metadata
- Download URL: fragment_api_py-3.0.3.tar.gz
- Upload date:
- Size: 16.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd7afc55943695ac40a523adfb89e5ce3304fa9d04e0315d200c6f7f325edee6
|
|
| MD5 |
2b99b49d06fe64d118d5591a2a73c641
|
|
| BLAKE2b-256 |
b6313b493216262f0324f2b5140b83d4cbe9773a9215bd74805de4eba80ec14d
|
File details
Details for the file fragment_api_py-3.0.3-py3-none-any.whl.
File metadata
- Download URL: fragment_api_py-3.0.3-py3-none-any.whl
- Upload date:
- Size: 19.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2dd52623098331611361a306682b7310504b96eab5ef6a81377745a033e38589
|
|
| MD5 |
a9207b73f45532392b132444be65814e
|
|
| BLAKE2b-256 |
de3d1307b5115f9482abef83fcc7b16aad3c2e752e5345f5e1cd8e079a9d79f5
|