Skip to main content

Python client for the Fragment API with Sync and Async support

Project description

Fragment API Python

Professional Python library for Fragment.com API. Send Telegram Stars, Premium, TON, and run Giveaways with automatic wallet validation and multi-version support.

Full Documentation: fragment.s1qwy.ru

PyPI version Python versions Documentation Status

Features

  • Sync & Async interfaces (Async requires [async] extra)
  • Payments: Stars, Premium, and TON top-up
  • Giveaways: Run Stars and Premium giveaways for channels
  • Marketplace Search: Search for Usernames, Numbers, and Gifts
  • Automatic Hash: No need to manually provide the API hash anymore
  • Wallet Support: V4R2 and V5R1 (W5)
  • Balance Validation: Automatic check before sending transactions
  • Type Safety: Full type hinting and structured results

Installation

# Basic (Sync only)
pip install fragment-api-py

# With Async support
pip install fragment-api-py[async]

Quick Start

Synchronous (Default)

from fragment_api import FragmentClient

client = FragmentClient(
    seed="your 24 words mnemonic phrase...",
    api_key="your_tonapi_key",
    cookies={
        "stel_ssid": "...",
        "stel_dt": "...",
        "stel_token": "...",
        "stel_ton_token": "..."
    }
)

# Get wallet info
wallet = client.get_wallet()
print(f"Address: {wallet.address} | Balance: {wallet.balance} TON")

# Purchase Stars
result = client.purchase_stars("@username", amount=50)
print(f"Success! TX Hash: {result.transaction_id}")

Asynchronous

import asyncio
from fragment_api.async_client import AsyncFragmentClient

async def main():
    async with AsyncFragmentClient(
        seed="mnemonic...",
        api_key="tonapi_key",
        cookies="..." # Can be a JSON string or dict
    ) as client:
        # Run a Premium giveaway
        result = await client.giveaway_premium("@my_channel", winners=5, months=3)
        print(f"Giveaway started! TX: {result.transaction_id}")

asyncio.run(main())

Wallet Versions

Version Alias Description
V5R1 W5 Latest wallet version (Default)
V4R2 - Most common standard wallet
client = FragmentClient(..., wallet_version="V4R2")

Marketplace Search

Search for available assets on Fragment:

# Search for usernames
usernames = client.search_usernames(query="durov", filter="auction")
for item in usernames.items:
    print(f"Name: {item['name']} | Price: {item['price']} TON")

# Search for anonymous numbers
numbers = client.search_numbers(query="888", sort="price_asc")

# Search for NFT Gifts
gifts = client.search_gifts(collection="artisanbrick")

Giveaways

Run official Telegram giveaways via Fragment:

# Stars Giveaway (Winners: 1-5)
client.giveaway_stars(channel="@my_channel", winners=3, amount=500)

# Premium Giveaway
client.giveaway_premium(channel="@my_channel", winners=10, months=6)

Methods Reference

Payments & Giveaways

  • purchase_stars(username, amount, show_sender=True) — Send Stars to user (50 - 1M).
  • purchase_premium(username, months, show_sender=True) — Gift Premium (3, 6, 12 months).
  • topup_ton(username, amount, show_sender=True) — Top up Telegram Ads balance.
  • giveaway_stars(channel, winners, amount) — Start Stars giveaway.
  • giveaway_premium(channel, winners, months) — Start Premium giveaway.

Search

  • search_usernames(query, sort, filter, offset_id)
  • search_numbers(query, sort, filter, offset_id)
  • search_gifts(query, collection, sort, filter, view, attr, offset)

Wallet & System

  • get_wallet() — Returns WalletInfo (address, state, balance).
  • call(method, data, page_url) — Make raw/custom API calls.

Setup Requirements

1. Fragment Cookies

You need the following cookies from an authenticated fragment.com session:

  • stel_ssid, stel_dt, stel_token, stel_ton_token.

2. TON API Key

The library uses TonAPI. You can get a free key at tonconsole.com.


Error Handling

The library uses a descriptive exception hierarchy:

from fragment_api.exceptions import (
    WalletError, 
    UserNotFoundError, 
    ConfigError,
    TransactionError
)

try:
    client.purchase_stars("@unknown_user", 50)
except UserNotFoundError:
    print("User not found!")
except WalletError as e:
    print(f"Balance too low or wallet error: {e}")
except TransactionError as e:
    print(f"Blockchain error: {e}")

Requirements

  • Python 3.10+ (Uses modern union types)
  • httpx >= 0.23.0
  • tonutils >= 0.3.0
  • pytoniq-core >= 0.1.37

Changelog

v4.0.0

  • Breaking Change: Renamed SyncFragmentAPI -> FragmentClient.
  • Breaking Change: Renamed AsyncFragmentAPI -> AsyncFragmentClient.
  • New Feature: Added Marketplace Search (Usernames, Numbers, Gifts).
  • New Feature: Added Giveaways (Stars & Premium).
  • Improvement: Automatic API Hash derivation (no more hash_value in config).
  • Improvement: Switched from requests/aiohttp to httpx.
  • Improvement: Default wallet version is now V5R1.

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

fragment_api_py-4.0.0.tar.gz (20.6 kB view details)

Uploaded Source

Built Distribution

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

fragment_api_py-4.0.0-py3-none-any.whl (32.6 kB view details)

Uploaded Python 3

File details

Details for the file fragment_api_py-4.0.0.tar.gz.

File metadata

  • Download URL: fragment_api_py-4.0.0.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for fragment_api_py-4.0.0.tar.gz
Algorithm Hash digest
SHA256 62597722899fc25515445e966bc67030bec7caad8baefdfb6b3dda3a8f63d9b8
MD5 a49f1098a6767fafff64591e81886431
BLAKE2b-256 f5a064b1ff6338b6a5b115c41d7c066e63908f1b1a21369757db218b8bcfde53

See more details on using hashes here.

File details

Details for the file fragment_api_py-4.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fragment_api_py-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3c453a1f6332fe4f8eb0e494ba8c2f8efb5f7f1c2dc071de3455850745b7eb7
MD5 79647094d5fee3b4ddfdcdda862c7ad9
BLAKE2b-256 baa9af177a98c1fe073ccd15fece2b2729a1971f4449b8b2b6bfa0f7573ce0a8

See more details on using hashes here.

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