Skip to main content

Async Python client for the Fragment API. Buy Stars and Premium, top up GRAM (ex TON) and Ads balances, run giveaways, manage anonymous numbers, and search Fragment listings.

Project description

pyfragment

pyfragment

PyPI Downloads Python Tests License

Async Python client for the Fragment marketplace API. Buy Stars & Premium, run giveaways, top up GRAM (ex TON) and Ads balances, manage anonymous numbers, and search Fragment listings.

Documentation · Examples · Changelog · Donate GRAM

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
from pyfragment.enums import PaymentMethod


async def main() -> None:
    async with FragmentClient(
        seed="word1 word2 ... word24",
        api_key="YOUR_API_KEY",  # tonconsole.com (tonapi, default) or t.me/toncenter
        cookies={
            "stel_ssid": "...",
            "stel_dt": "...",
            "stel_token": "...",
            "stel_ton_token": "...",
        },
        wallet_version="V5R1",   # or "V4R2", "HighloadV2", "HighloadV3R1"
        api_provider="tonapi",   # or "toncenter"
    ) as client:
        wallet = await client.get_wallet()
        print(f"GRAM: {wallet.gram_balance} | USDT: {wallet.usdt_balance}")

        recipient = "https://t.me/username"  # also: @username, username

        stars = await client.purchase_stars(recipient, amount=500, payment_method=PaymentMethod.USDT_GRAM)
        print(f"Sent {stars.amount} Stars to {stars.username} | tx: {stars.transaction_id}")

        premium = await client.purchase_premium(recipient, months=6, payment_method=PaymentMethod.GRAM)
        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 GRAM (ex TON) wallet mnemonic
api_key str API key for the chosen provider (see api_provider)
cookies dict | str Fragment session cookies
wallet_version str "V5R1" "V4R2" or "V5R1" — also accepts WalletVersion literal
api_provider str "tonapi" "tonapi" (tonconsole.com) or "toncenter" (t.me/toncenter)
timeout float 30.0 HTTP request timeout in seconds

Credentials

Fragment cookies

Log in to fragment.com and connect your GRAM (ex 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 GRAM (ex 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 GRAM (ex 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


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.3.2.tar.gz (48.9 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.3.2-py3-none-any.whl (42.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for pyfragment-2026.3.2.tar.gz
Algorithm Hash digest
SHA256 631b24ab52f8e326b9ba68882709df7e5f6e12a32e1371356b47a79f4fcee958
MD5 d044e5384b7832c3156d75279e5e129b
BLAKE2b-256 b137122ded30194ef34f6ef910abbdeeb483423b913ea7b0b916742733c5a2fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfragment-2026.3.2.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.3.2-py3-none-any.whl.

File metadata

  • Download URL: pyfragment-2026.3.2-py3-none-any.whl
  • Upload date:
  • Size: 42.1 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.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8312a6d9bf99f422f60b5d74099464097a2de58b41edaca00c1de2753136aa9
MD5 6bf8993c299cb9909756472f77b172f4
BLAKE2b-256 e5139616a63f26275e6aa0e2081f4d2c23a1507efe09fdd475f729be34f7e5e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyfragment-2026.3.2-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