### Quick Start Example Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md A basic example demonstrating how to initialize the FragmentClient, get wallet balances, and purchase stars and premium services. Ensure you replace placeholder values with your actual credentials. ```python 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()) ``` -------------------------------- ### Install pyfragment Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Install the pyfragment library using pip. This is the standard way to add the package to your project. ```bash pip install pyfragment ``` -------------------------------- ### Clone Repository and Install Dependencies Source: https://github.com/bohd4nx/pyfragment/blob/master/CONTRIBUTING.md Clone the pyfragment repository and install it in editable mode with development dependencies. ```bash git clone https://github.com/bohd4nx/pyfragment.git cd pyfragment pip install -e ".[dev]" ``` -------------------------------- ### Install Latest Dev Build Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Install the latest development build directly from the GitHub repository. Use this for the newest features or bug fixes. ```bash # Latest dev build pip install git+https://github.com/bohd4nx/pyfragment.git@dev ``` -------------------------------- ### Install Browser Support for Cookies Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Install the pyfragment library with browser support to automatically extract cookies. This simplifies the process of obtaining Fragment session cookies. ```bash pip install "pyfragment[browser]" ``` -------------------------------- ### Extract Cookies from Browser Source: https://github.com/bohd4nx/pyfragment/blob/master/CHANGELOG.md Extracts Fragment session cookies directly from installed browsers. Useful for initializing FragmentClient without manual intervention. The result includes cookies and their expiry time. ```python from pyfragment import get_cookies_from_browser result = get_cookies_from_browser("chrome") # or "firefox", "edge", "brave", ... client = FragmentClient(seed="...", api_key="...", cookies=result.cookies) print(result.expires) # ISO 8601 expiry of stel_ssid, or None for session cookies ``` -------------------------------- ### Get Cookies from Browser Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Use the get_cookies_from_browser function to automatically retrieve Fragment session cookies from your browser. Specify the browser name (e.g., 'chrome', 'firefox'). ```python 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 ``` -------------------------------- ### Configure Logging Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Set up logging for the pyfragment library. You can adjust the logging level to control the verbosity of output, from basic to detailed request logs. ```python import logging logging.basicConfig(level=logging.INFO) logging.getLogger("pyfragment").setLevel(logging.DEBUG) # for detailed request logs ``` -------------------------------- ### Run Linting, Formatting, and Type Checks Source: https://github.com/bohd4nx/pyfragment/blob/master/CONTRIBUTING.md Execute linting and formatting checks using Ruff, and perform type checking with MyPy. ```bash # Lint and format ruff check . --fix && ruff format . # Type check mypy . --explicit-package-bases ``` -------------------------------- ### Run Tests Source: https://github.com/bohd4nx/pyfragment/blob/master/CONTRIBUTING.md Execute the project's unit tests using pytest. ```bash pytest ``` -------------------------------- ### PyFragment Custom Exceptions Source: https://github.com/bohd4nx/pyfragment/blob/master/README.md Imports all custom exception classes from the pyfragment library. These exceptions cover a wide range of potential errors, from configuration issues to API failures and user-specific problems. ```python 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, 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 ) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.