Skip to content

Repository files navigation

pyCryptoPayAPI

CryptoPay

Crypto Pay is a payment system based on @CryptoBot, which allows you to accept payments in cryptocurrency using the API.

This library help you to work with Crypto Pay via Crypto Pay API in yours Python scripts.

GitHub release (latest by date)CodeQLLibrary testPyPI - Python Version
Documentation available on English language
Документация доступна на Русском языке

Install

Via pip:

pip install pycryptopay-sdk

Via git:

pip install git+https://github.com/LulzLoL231/pyCryptoPayAPI.git

Via source, in source folder:

pip install ./

Usage

API

First, you need to create your application and get an API token. Open @CryptoBot or @CryptoTestnetBot (for testnet), send a command /pay to create a new app and get API Token.
Next step: try to call a simple get_me() method to check that everything is working well:

fromasyncioimportrunfromCryptoPayAPIimportCryptoPaycp=CryptoPay('YOUR_API_TOKEN')
print(run(cp.get_me())) # Returns Application object.

You can use testnet for testing your applications. Defaults is mainnet.

fromCryptoPayAPIimportCryptoPaycp=CryptoPay('YOUR_API_TOKEN', testnet=True)

You can find all available methods in Methods chapter.
Also, you can get supported assets, paid button names and invoice status:

fromasyncioimportget_event_loopfromCryptoPayAPIimportCryptoPayfromCryptoPayAPI.schemasimportAssets, PaidButtonNames, InvoiceStatuslp=get_event_loop()
cp=CryptoPay('YOUR_API_TOKEN')
print(lp.run_until_complete(cp.create_invoice(
Assets.USDT, 5.25,
description='Example page for $5.25!',
paid_btn_name=PaidButtonNames.VIEW_ITEM,
paid_btn_url='https://example.com'
))) # Prints information about created invoice.print(lp.run_until_complete(cp.get_invoices(
Assets.USDT, status=InvoiceStatus.PAID
))) # Prints all paid invoices.

Webhooks

Use Webhooks to get updates for your app, Crypto Pay will send an HTTPS POST request to the specified URL, containing a JSON-serialized Update.
Read more about webhooks in Crypto Pay Docs!
Use CryptoPay.process_webhook_update function, for processing Crypto Pay requests.
Check webhook example for more info.

CryptoPay.process_webhook_update

Coroutine. Processing webhook request, returns Update object.

Arguments:

  • body (bytes) - JSON content from Crypto Pay request in bytes.
  • headers (dict[str, str]) - Request headers.
update=awaitcp.process_webhook_update(body, headers)
print(f'Recieved {update.payload.amount}{update.payload.asset}!') # Recieved 10.0 ETH

Look full code in the examples.

API methods

get_me

A simple method for testing your app's authentication token. Requires no parameters. Returns basic information about the app.
Returns: Application object.

cp.get_me()

create_invoice

Use this method to create a new invoice. Returns object of created invoice.

Arguments:

  • asset (Assets | str) - Currency code. Supported assets: BTC, TON, ETH (only testnet), USDT, USDC, BUSD.

  • amount (float) - Amount of the invoice in float. For example: 125.50

  • description (str) - Optional. Description of the invoice. Up to 1024 symbols.

  • hidden_message (str) - Optional. The message will show when the user pays your invoice.

  • paid_btn_name (PaidButtonName | str) - Optional. Paid button name. This button will be shown when your invoice was paid. Supported names:

    • viewItem - View Item
    • openChannel - Open Channel
    • openBot - Open Bot
    • callback - Return
  • paid_btn_url (str) - Optional but requried when you use paid_btn_name. Paid button URL. You can set any payment success link (for example link on your bot). Start with https or http.

  • payload (str, up to 4kb) - Optional. Some data. User ID, payment id, or any data you want to attach to the invoice.

  • allow_comments (bool) - Optional. Allow adding comments when paying an invoice. Default is True.

  • allow_anonymous (bool) - Optional. Allow pay invoice as anonymous. Default is True.

  • expires_in (int) - Optional. You can set the expiration date of the invoice in seconds. Use this period: 1-2678400 seconds.

Returns: Invoice object of created invoice.

cp.create_invoice(
Assets.USDT, 5.25,
description='Example page for $5.25!',
paid_btn_name=PaidButtonNames.VIEW_ITEM,
paid_btn_url='https://example.com'
)

transfer

Use this method to send coins from your app to the user. Returns object of completed transfer.

Arguments:

  • user_id (int) - Telegram User ID. The user needs to have an account in our bot (send /start if no).
  • asset (Assets) - Currency code. Supported assets: BTC, TON, ETH (only testnet), USDT, USDC, BUSD.
  • amount (float) - Amount of the transfer in float. For example: 125.50
  • spend_id (str) - It is used to make your request idempotent. It's guaranteed that only one of the transfers with the same spend_id will be accepted by Crypto Pay API. This parameter is useful when the transfer should be retried (i.e. request timeout/connection reset/500 HTTP status/etc). You can use a withdrawal id or something. Up to 64 symbols.
  • comment (str) - Optional. The comment of the invoice. The comment will show in the notification about the transfer. Up to 1024 symbols.

Returns: Transfer object of created transfer.

cp.transfer(265300852, Assets.USDT, 3.0, 'pCBA226ghd', comment='donate')

get_invoice

Use this method to get invoices of your app. On success, the returns array of Invoice.

Arguments:

  • asset (Assets) - Optional. Currency code. Supported assets: BTC, TON, ETH (only testnet), USDT, USDC, BUSD. Default: all assets.
  • invoice_ids (str) - Optional. Invoice IDs separated by comma.
  • status (InvoiceStatus) - Optional. Status of invoices. Available statuses: active, paid and expired. Default: all statuses.
  • offset (int) - Optional. Offset needed to return a specific subset of invoices. Default 0.
  • count (int) - Optional. Number of invoices to return. Default 100, max 1000.

Returns: array of Invoice objects.

cp.get_invoices(
schemas.Assets.USDT, status=schemas.InvoiceStatus.PAID, count=10
)

get_balance

Use this method to get balance of your app. Returns array of assets.

Returns: array of Balance objects.

cp.get_balance()

get_exchange_rates

Use this method to get exchange rates of supported currencies. Returns array of currencies.

Returns: array of ExchangeRate objects.

cp.get_exchange_rates()

get_currencies

Use this method to supported currencies. Returns array of currencies.

Returns: array of Currency objects.

cp.get_currencies()

Constants and schemas

fromCryptoBotAPIimportschemas

schemas.Asset

constantvalue
Assets.BTCBTC
Assets.TONTON
Assets.ETHETH
Assets.USDTUSDT
Assets.USDCUSDC
Assets.BUSDBUSD

schemas.PaidButtonNames

constantvalue
PaidButtonNames.VIEW_ITEMviewItem
PaidButtonNames.OPEN_CHANNELopenChannel
PaidButtonNames.OPEN_BOTopenBot
PaidButtonNames.CALLBACKcallback

schemas.InvoiceStatus

constantvalue
InvoiceStatus.ACTIVEactive
InvoiceStatus.PAIDpaid
InvoiceStatus.EXPIREDexpired

schemas.Invoice

keytype
invoice_idint
statusInvoiceStatus
hashstr
assetAssets
amountdecimal.Decimal
pay_urlstr
descriptionOptional[str]
created_atdatetime.datetime
allow_commentsbool
allow_anonymousbool
expiration_dateOptional[datetime.datetime]
paid_atOptional[datetime.datetime]
paid_anonymouslyOptional[bool]
commentOptional[str]
hidden_messageOptional[str]
payloadOptional[str]
paid_btn_nameOptional[PaidButtonNames]
paid_btn_urlOptional[str]

schemas.Transfer

keytype
transfer_idint
user_idint
assetAssets
amountdecimal.Decimal
statusLiteral['completed']
completed_atdatetime.datetime
commentOptional[str]

schemas.Application

keytype
app_idint
namestr
payment_processing_bot_usernamestr

schemas.Balance

keytype
currency_codestr
availabledecimal.Decimal

schemas.ExchangeRate

keytype
is_validbool
sourcestr
targetstr
ratedecimal.Decimal

schemas.Currency

keytype
is_blockchainbool
is_stablecoinbool
is_fiatbool
namestr
codestr
urlOptional[str]
decimalsint

schemas.UpdateType

constantvalue
UpdateType.INVOICE_PAIDinvoice_paid

schemas.Update

keytype
update_idint
update_typeUpdateType
request_datedatetime
payloadInvoice

About

Python API wrapper for Crypto Pay API

Topics

Resources

Code of conduct

Stars

10 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages