Skip to main content

fragment-api

Fragment API uchun Python kutubxona: botingizdan Telegram Stars va Premium soting.

from fragment_api import FragmentAPI

api = FragmentAPI("API_KALIT")
order = api.stars.buy("durov", 50)
print(order.cost)    # 0.76125

O'rnatish

pip install fragment-api

Python 3.9 yoki undan yangi versiya kerak.

API kalitni dashboard → loyiha sozlamalaridan oling. Kalitni kodga yozish o'rniga FRAGMENT_API_KEY muhit o'zgaruvchisiga qo'ysangiz, FragmentAPI() kifoya.

Metodlar

Metodlar API endpointlari bo'yicha guruhlangan: /stars/buy → api.stars.buy().

Metod Endpoint Qaytaradi
api.get_info(username) /getInfo UserInfo
api.stars.pricing(amount) /stars/pricing StarsPricing
api.stars.buy(username, amount) /stars/buy StarsOrder
api.premium.pricing() /premium/pricing PremiumPricing
api.premium.buy(username, duration) /premium/buy PremiumOrder
api.order.status(order_id) /order/status OrderStatus
api.wallet.balance() /wallet/balance WalletBalance
api.wallet.calculate() /wallet/calculate WalletCalculation

Natija maydonlarining nomlari API javobidagi bilan bir xil. Pul summalari Decimal turida keladi: aniq son, bemalol hisoblash mumkin.

user = api.get_info("durov")
user.name, user.is_premium                  # 'Pavel Durov', True

price = api.stars.pricing(50)
price.price.selected                        # Decimal('0.76125'), loyihangiz valyutasida

order = api.stars.buy("durov", 50)
order.amount, order.cost, order.payment_method      # 50, Decimal('0.76125'), 'USDT'

for p in api.premium.pricing().packages:
    print(p.months, p.usd)                  # 3 12.17 ...

order = api.premium.buy("durov", duration=3)
order.duration, order.cost                  # 3, Decimal('5.075')

wallet = api.wallet.balance()
wallet.balance_ton, wallet.balance_usdt     # Decimal('1.2400'), Decimal('12.500000')

api.wallet.calculate().stars.max_amount     # 16 — hamyon hozir nechta stars'ga yetadi

Xatolar

Har qanday xatoda FragmentError chiqadi. Uning ichida API'dagi xato kodi (e.code) va tushuntirish (e.message) bo'ladi:

from fragment_api import FragmentAPI, FragmentError

try:
    order = api.stars.buy("durov", 50)
except FragmentError as e:
    if e.code == "INSUFFICIENT_FUNDS":
        print("Hamyonni to'ldiring")
    else:
        print(e.code, e.message)
e.code Ma'nosi
VALIDATION_ERROR API kalit yoki maydon noto'g'ri, hamyon ulanmagan
IP_NOT_ALLOWED server IP'si loyihaning IP ro'yxatida yo'q
INVALID_AMOUNT_MIN / INVALID_AMOUNT_MAX stars miqdori 50 dan kam yoki 10 000 000 dan ko'p
INSUFFICIENT_FUNDS hamyonda to'lov uchun mablag' yetmaydi
INSUFFICIENT_GAS hamyonda gaz (blokcheyn to'lovi) uchun TON yetmaydi
RATE_LIMIT so'rovlar juda ko'p, e.retry_after soniya kuting
USERNAME_NOT_FOUND username topilmadi yoki foydalanuvchiga tegishli emas (kanal yoki bot) — qayta urinish foyda bermaydi
FRAGMENT_ERROR / FRAGMENT_TIMEOUT Fragment vaqtincha ishlamayapti
BLOCKCHAIN_FAIL TON tarmog'ida vaqtincha muammo
PAYMENT_REJECTED to'lov blokcheynga kiritilmadi, pul yechilmagan — qayta yuborish mumkin
PAYMENT_PENDING to'lov yuborildi, natija ~90 s ichida aniqlanmadi — OrderUncertainError, qayta yubormang
CRITICAL_SERVER_ERROR serverda kutilmagan xato
NETWORK_ERROR serverga ulanib bo'lmadi (internet)
NO_RESPONSE so'rov ketdi, lekin javob kelmadi

Muhim: xaridni ikki marta yubormang

Xarid so'rovi ketgandan keyin javob kelmasa yoki server "to'lov yuborildi, tasdiq kutilmoqda" yoki CRITICAL_SERVER_ERROR desa, pul allaqachon ketgan bo'lishi mumkin. Bunday holatlarda FragmentError'ning maxsus turi — OrderUncertainError chiqadi:

from fragment_api import FragmentError, OrderUncertainError

try:
    order = api.stars.buy("durov", 50)
except OrderUncertainError:
    print("Buyurtma tekshirilmoqda")   # QAYTA YUBORMANG — dashboarddagi Tranzaksiyalarni tekshiring
except FragmentError as e:
    print(e.message)                   # server buyurtma bajarilmaganini aytdi

Bunday xatoda server buyurtma raqamini ham beradi (e.order_id). U orqali natijani keyinroq tekshirish mumkin:

try:
    order = api.stars.buy("durov", 50)
except OrderUncertainError as e:
    st = api.order.status(e.order_id) if e.order_id else None
    # st.status: completed | processing | pending | failed | refunded
    # failed — pul yechilmagan, qayta yuborish mumkin; pending — kuting

Javob umuman kelmagan bo'lsa (NO_RESPONSE), order_id bo'lmaydi — natijani dashboarddagi Tranzaksiyalar bo'limidan tekshiring.

Muvaffaqiyatli xaridda ham raqam bor: order.order_id.

Kutubxona xaridlarni hech qachon o'zi qayta yubormaydi. Narx, balans va get_info so'rovlari esa internet xatosi yoki limitda o'zi 2 martagacha qayta urinadi.

Async (aiogram, FastAPI)

Metodlar xuddi shunday, faqat await bilan:

from fragment_api import AsyncFragmentAPI

api = AsyncFragmentAPI("API_KALIT")
order = await api.stars.buy("durov", 50)

Bitta klientni butun dastur davomida ishlating. To'liq bot misoli: examples/aiogram_bot.py.

Litsenziya

MIT

Metadata

Release files for fragment-api 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fragment-api 1.0.0
File Size Uploaded Provenance Actions
fragment_api-1.0.0.tar.gz 16.2 kB Without provenance Details for fragment_api-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for fragment-api 1.0.0
File Interpreter ABI Platform Actions
fragment_api-1.0.0-py3-none-any.whl Python 3 none any Details for fragment_api-1.0.0-py3-none-any.whl

Total release size: 28.9 kB

Release files / fragment_api-1.0.0.tar.gz

Download URL fragment_api-1.0.0.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7a8521b7a0697a02592a2fa375496dc507fe617bd9d197c65243524065c59cc1
BLAKE2b-256 checksum
How to use checksums
cc3abe117a6d7627c8111d2a7a58fc7892516246de3d3f604d813e156fbdf63b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Release files / fragment_api-1.0.0-py3-none-any.whl

Download URL fragment_api-1.0.0-py3-none-any.whl
Size 12.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7dca11fe49049ba195148166addaf21b1533e091dd6f05bd7a378411080c456f
BLAKE2b-256 checksum
How to use checksums
824be05edf2e1ffa8a6165043cd7f095166ea6653f87182603e74aaed70e15e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.12

Security for fragment-api 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page