Skip to main content

Async Python client for the Fragment API — a unified toolkit to manage Telegram assets: purchase Stars and Premium, top up TON and Ads balances, run giveaways, manage anonymous numbers, and explore the marketplace for usernames, numbers, and gifts.

Project description

Fragment Logo

Fragment API

Async Python client for the Fragment API — a unified toolkit to manage Telegram assets: purchase Stars and Premium, top up TON and Ads balances, run giveaways, manage anonymous numbers, and explore the marketplace for usernames, numbers, and gifts.

PyPI version PyPI downloads Python License Stars CI

Report Bug · Request Feature · Donate TON

Disclaimer: This project is not affiliated with, endorsed by, or in any way officially connected with Fragment or Telegram.


Installation

pip install pyfragment

To install the latest unreleased changes from the dev branch:

pip install git+https://github.com/bohd4nx/pyfragment.git@dev

Requires Python 3.10+.


Configuration

Parameter Type Default Description
seed str 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"
timeout float 30.0 HTTP request timeout in seconds

Credentials

Fragment cookies — log in to fragment.com and connect your TON wallet. You can get cookies in two ways:

  • Automatically (recommended) — use get_cookies_from_browser(), which reads them directly from your browser's on-disk store. No extension needed:
    from pyfragment.utils import get_cookies_from_browser
    result = get_cookies_from_browser("chrome")  # or "firefox", "edge", "brave", ...
    # result.cookies — dict[str, str] to pass to FragmentClient
    # result.expires — ISO 8601 expiry of stel_ssid, or None for session cookies
    
  • Manually — install Cookie Editor and export these four keys: stel_ssid, stel_dt, stel_token, stel_ton_token. Pass them as a dict or JSON string.

Refresh when you get authentication errors.

Tonapi key — generate at tonconsole.com.

Seed phrase — 24-word mnemonic from your TON wallet (Tonkeeper → Settings → Backup). Never share it.


Usage

import asyncio
from pyfragment import (
    FragmentClient,
    FragmentError,       # base — catches everything below
    UserNotFoundError,   # username doesn't exist on Fragment
    WalletError,         # insufficient balance or misconfiguration
    CookieError,         # cookies are missing or expired
    TransactionError,    # on-chain broadcast failed
    ConfigurationError,  # invalid argument (months, amount, etc.)
    FragmentAPIError,    # unexpected Fragment API response
)


async def main() -> None:
    async with FragmentClient(
        seed="word1 word2 ... word24",  # 24-word TON wallet mnemonic
        api_key="YOUR_TONAPI_KEY",       # from tonconsole.com
        cookies={
            "stel_ssid": "...",
            "stel_dt": "...",
            "stel_token": "...",
            "stel_ton_token": "...",
        },
    ) as client:
        try:
            # Purchase 6 months of Telegram Premium
            result = await client.purchase_premium("@username", months=6)
            print(f"{result.amount} months of Premium successfully sent to {result.username} | tx: {result.transaction_id}")

            # Purchase 500 Stars
            result = await client.purchase_stars("@username", amount=500)
            print(f"{result.amount} Stars successfully sent to {result.username} | tx: {result.transaction_id}")

            # Top up 10 TON to Telegram balance
            # wallet must hold at least amount + ~0.056 TON for gas
            result = await client.topup_ton("@username", amount=10)
            print(f"{result.amount} TON successfully sent to {result.username} | tx: {result.transaction_id}")

        except UserNotFoundError:
            print(f"User was not found on fragment.com — check the username and try again.")
        except WalletError as e:
            print(f"Wallet error — insufficient balance or misconfiguration: {e}")
        except CookieError:
            print("Authentication failed — session cookies are missing or expired. Refresh them and retry.")
        except TransactionError as e:
            print(f"Transaction failed to broadcast on-chain: {e}")
        except ConfigurationError as e:
            print(f"Invalid argument: {e}")
        except FragmentAPIError as e:
            print(f"Unexpected response from Fragment API: {e}")
        except FragmentError as e:
            # catch-all for any other pyfragment error
            print(f"Unexpected error: {e}")


asyncio.run(main())

Made with ❤️ by @bohd4nx

Star ⭐ this repo if you found it useful!

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

pyfragment-2026.2.1.tar.gz (39.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyfragment-2026.2.1-py3-none-any.whl (37.8 kB view details)

Uploaded Python 3

File details

Details for the file pyfragment-2026.2.1.tar.gz.

File metadata

  • Download URL: pyfragment-2026.2.1.tar.gz
  • Upload date:
  • Size: 39.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfragment-2026.2.1.tar.gz
Algorithm Hash digest
SHA256 10915019015335147fdd3b5869ad7327a86d477e906f76037cbef93312533cf5
MD5 73074238d3d319976a29e4d5cd1bad5b
BLAKE2b-256 084236b920b57e3f763836e1a8a0819bf50f20928cd9051d2b45a14c558ef3cf

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfragment-2026.2.1.tar.gz:

Publisher: publish.yml on bohd4nx/pyfragment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyfragment-2026.2.1-py3-none-any.whl.

File metadata

  • Download URL: pyfragment-2026.2.1-py3-none-any.whl
  • Upload date:
  • Size: 37.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pyfragment-2026.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 badfb95e7280b38fee625bd43ef58979e70c1ad4dfd1d052af0e576cee2a681c
MD5 80c0968e57cc12f3109f01dfcc25764f
BLAKE2b-256 20c7cc28602a0ea6274491ca700dd0ce6c549f75fec04e3ed74577ee20bd7946

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfragment-2026.2.1-py3-none-any.whl:

Publisher: publish.yml on bohd4nx/pyfragment

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page