Async · Fragment.com API · Typed

Buy Telegram Stars, Premium & TON from Python.

A fully async client for the Fragment.com API. One call returns a ready-to-sign TON transaction to purchase Stars, gift Premium, or top up TON — pay in TON or USDT.

$ pip install afragment
Read the docs →
PyPIv0.4.0
python3.8 – 3.12
depsaiohttp only
licenseMIT
buy_stars.py async / await
1import asyncio
2from afragment import AsyncFragmentClient, nano_to_ton
3
4async def main():
5    async with AsyncFragmentClient(
6        fragment_hash=HASH, fragment_cookie=COOKIE,
7    ) as client:
8        # one call → ready-to-sign TON transaction
9        result = await client.buy_stars(
10            "durov", 100, wallet, payment_method="ton",
11        )
12
13        tx = result["transaction"]["messages"][0]
14        print(f"To: {result['recipient']['name']}")
15        print(f"Pay: {nano_to_ton(int(tx['amount']))} TON")
16
17asyncio.run(main())
stdout · ✓ exit 0 · 412ms
To: Pavel Durov  ·  100 Telegram Stars
Pay: 2.45 TON  →  EQDr…k9fA   ref#a91c3f
3line setup
1dependency · aiohttp
2pay methods · TON / USDT
3.8+python
// what you can buy

Three products, one async call each.

Pass a username, an amount and your wallet address — every method returns a ready-to-sign TON transaction with recipient, price and payload.

Telegram Stars

Buy Stars for any @username and get back a signable transaction in one call.

buy_stars("user", 100, wallet)
min 50 stars · TON or USDT

Premium Gifts

Gift Telegram Premium for 3, 6 or 12 months to any username.

buy_premium("user", 12, wallet)
months 3 · 6 · 12

TON Topup

Top up a Fragment TON balance by any whole amount, on-chain.

buy_ton_topup("user", 10, wallet)
min 1 TON · whole numbers
// api reference

Compact, predictable surface.

Every purchase method shares the same signature and return shape. Validation runs before any network call.

MethodParametersValidation
buy_stars(username, quantity, wallet, payment_method) Purchase Telegram Stars quantity ≥ 50
buy_premium(username, months, wallet, payment_method) Gift Telegram Premium months ∈ 3 · 6 · 12
buy_ton_topup(username, amount, wallet, payment_method) TON balance topup amount ≥ 1, whole
// authentication

Two credentials, then you're live.

afragment authenticates with your own Fragment.com session — grab the hash and cookie once from your browser.

Get your credentials

You need FRAGMENT_HASH and FRAGMENT_COOKIE from a logged-in session.

1Log in to Fragment.com and complete KYC if needed.
2Open DevToolsNetwork tab → filter Fetch/XHR.
3Trigger any action (e.g. search a username) and open an API request.
4HASH is the ?hash= URL param; COOKIE is in the request Cookie header.

Error handling

Rich, typed exceptions — all inherit from FragmentAPIError.

FragmentAPIErrorBase exception for all API errors
AuthenticationErrorInvalid or expired credentials
PriceChangedErrorPrice changed mid-request — retry
InvalidRecipientErrorUser not found or not eligible
ValueErrorInput validation failed
// utilities

Helpers for payloads & amounts.

Decode transaction payloads and convert between nanoTON and TON without pulling in the TON SDK.

nano_to_ton()Convert nanoTON → TON float.
ton_to_nano()Convert TON → nanoTON int.
decode_payload()Decode a base64 transaction payload.
extract_ref_id()Pull the reference ID from a payload.
extract_transaction_text()Human-readable transaction comment.
AsyncFragmentClientAsync context-managed API client.

Automate Fragment in three lines.

Async, typed, MIT licensed — depends only on aiohttp.