Async Python client for the Fragment API. Buy Stars and Premium, top up TON and Ads balances, run giveaways, manage anonymous numbers, and search Fragment listings.
Project description
pyfragment
Async Python client for the Fragment marketplace API. Buy Stars & Premium, run giveaways, top up TON and Ads balances, manage anonymous numbers, and search Fragment listings.
Disclaimer: This project is not affiliated with Fragment or Telegram.
Installation
pip install pyfragment
# Latest dev build
pip install git+https://github.com/bohd4nx/pyfragment.git@dev
Quick Start
import asyncio
from pyfragment import FragmentClient
async def main() -> None:
async with FragmentClient(
seed="word1 word2 ... word24",
api_key="YOUR_TONAPI_KEY",
cookies={
"stel_ssid": "...",
"stel_dt": "...",
"stel_token": "...",
"stel_ton_token": "...",
},
) as client:
wallet = await client.get_wallet()
print(f"TON: {wallet.ton_balance} | USDT: {wallet.usdt_balance}")
recipient = "https://t.me/username" # also: @username, username
stars = await client.purchase_stars(recipient, amount=500, payment_method="usdt_ton")
print(f"Sent {stars.amount} Stars to {stars.username} | tx: {stars.transaction_id}")
premium = await client.purchase_premium(recipient, months=6, payment_method="ton")
print(f"Sent Premium {premium.amount}m to {premium.username} | tx: {premium.transaction_id}")
asyncio.run(main())
Configuration
| Parameter | Type | Default | Description |
|---|---|---|---|
seed |
str |
— | 12- or 24-word TON wallet mnemonic |
api_key |
str |
— | Tonapi key from tonconsole.com |
cookies |
dict | str |
— | Fragment session cookies |
wallet_version |
str |
"V5R1" |
"V4R2" or "V5R1" — also accepts WalletVersion literal |
timeout |
float |
30.0 |
HTTP request timeout in seconds |
Credentials
Fragment cookies
Log in to fragment.com and connect your TON wallet.
Automatically (recommended) — reads directly from your browser, no extension needed:
pip install "pyfragment[browser]"
from pyfragment import get_cookies_from_browser
result = get_cookies_from_browser("chrome") # firefox, edge, brave, ...
# result.cookies — dict[str, str] ready to pass to FragmentClient
# result.expires — ISO 8601 expiry of stel_ssid, or None for session cookies
Manually — use Cookie Editor and export: stel_ssid, stel_dt, stel_token, stel_ton_token. Pass as a dict or JSON string.
Refresh cookies when you get authentication errors.
Tonapi key
Generate at tonconsole.com.
Seed phrase
12- or 24-word mnemonic from your TON wallet (Tonkeeper → Settings → Backup). Never share it.
Logging
pyfragment uses the standard logging module under the pyfragment namespace and is silent by default:
import logging
logging.basicConfig(level=logging.INFO)
logging.getLogger("pyfragment").setLevel(logging.DEBUG) # for detailed request logs
Error Handling
All exceptions inherit from FragmentError:
from pyfragment import (
ConfigurationError, # invalid arguments (amount, months, payment_method…)
UserNotFoundError, # recipient not found on Fragment
WalletError, # insufficient TON or USDT balance
TransactionError, # broadcast failed, duplicate seqno, invalid payload
FragmentAPIError, # Fragment API returned an error response
FragmentPageError, # page fetch or hash extraction failed
AnonymousNumberError, # number not owned, wrong state, login code issues
CookieError, # missing or malformed session cookies
ParseError, # failed to decode Fragment payload
VerificationError, # on-chain verification step failed
OperationError, # generic operation-level failure
UnexpectedError, # unexpected API response structure
)
Made with ❤️ by @bohd4nx · Contributing · Security
Star ⭐ if you found it useful
Project details
Release history Release notifications | RSS feed
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 pyfragment-2026.3.1.tar.gz.
File metadata
- Download URL: pyfragment-2026.3.1.tar.gz
- Upload date:
- Size: 44.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7554822410ff013e4ce66cc83d020bf11f3cbc8a85493e70c4a5386e8d618bf9
|
|
| MD5 |
63ff258f3240ddc3c7a3651ae3299064
|
|
| BLAKE2b-256 |
422dee62d46a13e686993cf4a557c08dc1de23aeb5286c4c454430422e32ac6c
|
Provenance
The following attestation bundles were made for pyfragment-2026.3.1.tar.gz:
Publisher:
publish.yml on bohd4nx/pyfragment
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfragment-2026.3.1.tar.gz -
Subject digest:
7554822410ff013e4ce66cc83d020bf11f3cbc8a85493e70c4a5386e8d618bf9 - Sigstore transparency entry: 1661905067
- Sigstore integration time:
-
Permalink:
bohd4nx/pyfragment@3d0cf38f13974c8dcf59cfd6830b7863c0d6be0f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/bohd4nx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3d0cf38f13974c8dcf59cfd6830b7863c0d6be0f -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file pyfragment-2026.3.1-py3-none-any.whl.
File metadata
- Download URL: pyfragment-2026.3.1-py3-none-any.whl
- Upload date:
- Size: 39.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c9448f22ef22941883dcfc7c60db76f3b4dd57d55bd2382da1fa2c569fda423
|
|
| MD5 |
43d3251e2b9c395c56242ef46935a72f
|
|
| BLAKE2b-256 |
e9dc9cbb00d0d7a45015a55a798b3b9adf18dda841a562a42a08c8e4fa2ba793
|
Provenance
The following attestation bundles were made for pyfragment-2026.3.1-py3-none-any.whl:
Publisher:
publish.yml on bohd4nx/pyfragment
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyfragment-2026.3.1-py3-none-any.whl -
Subject digest:
4c9448f22ef22941883dcfc7c60db76f3b4dd57d55bd2382da1fa2c569fda423 - Sigstore transparency entry: 1661905241
- Sigstore integration time:
-
Permalink:
bohd4nx/pyfragment@3d0cf38f13974c8dcf59cfd6830b7863c0d6be0f -
Branch / Tag:
refs/heads/master - Owner: https://github.com/bohd4nx
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3d0cf38f13974c8dcf59cfd6830b7863c0d6be0f -
Trigger Event:
workflow_run
-
Statement type: