Python implementation of CryptAPI's payment gateway with both synchronous and asynchronous support.
This library provides a simple and powerful interface to integrate cryptocurrency payments into your Python applications using the CryptAPI payment gateway. It supports all cryptocurrencies available on CryptAPI.
- Simple integration with CryptAPI payment gateway
- Support for all cryptocurrencies offered by CryptAPI
- Easy generation of payment addresses and QR codes
- Callback handling for payment notifications
- Real-time payment log retrieval
- Currency conversion utilities
- Fee estimation tools
- Support for both synchronous and asynchronous operations
- Utility functions for URL preparation and encoding
Python >= 3.0
Requests >= 2.20
aiohttp >= 3.9.1 (for async support)
certifi >= 2024.2.2 (for async support)
Install the package using pip:
pip install python-cryptapiAvailable on PyPI or on GitHub
The library offers two main classes:
CryptAPIHelper: For synchronous (blocking) API callsAsyncCryptAPIHelper: For asynchronous (non-blocking) API calls
from cryptapi import CryptAPIHelper # Synchronous API
from cryptapi import AsyncCryptAPIHelper # Asynchronous APIBoth synchronous and asynchronous classes require the same initialization parameters:
# Common parameters for both APIs
coin = 'btc' # The cryptocurrency to use (e.g., 'btc', 'eth', 'bep20_usdt')
my_address = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' # Your wallet address where funds will be forwarded
callback_url = 'https://example.com/payment/callback' # URL that will be called when payment is received
params = { # Custom parameters to append to callback URL
'order_id': '12345',
'customer_id': 'C12345'
}
cryptapi_params = { # Additional parameters for CryptAPI
'convert': 1,
'multi_token': 1,
'post': 1 # Use POST instead of GET for callbacks
}from cryptapi import CryptAPIHelper
# Initialize the helper
ca = CryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# Generate a new payment address
result = ca.get_address()
address = result['address_in']
print(f"Payment address: {address}")
# Access other information in the result
print(f"Address URL: {result.get('address_url', '')}")
print(f"Minimum confirmations: {result.get('minimum_confirmations', '')}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
# Initialize the async helper
ca = AsyncCryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# Generate a new payment address
result = await ca.get_address()
address = result['address_in']
print(f"Payment address: {address}")
# Access other information in the result
print(f"Address URL: {result.get('address_url', '')}")
print(f"Minimum confirmations: {result.get('minimum_confirmations', '')}")
# Run the async function
asyncio.run(main())The get_address() method returns a dictionary containing:
address_in: The generated payment addressaddress_out: Your own address where funds will be forwardedcallback_url: The processed callback URL with your parametersaddress_url: URL to a CryptAPI page showing payment status- And other information about the payment setup
Once your customer makes a payment, CryptAPI will send a callback to your callback_url. The callback data includes:
address_in: The payment addressaddress_out: Your wallet addresscoin: The cryptocurrency usedconfirmations: Number of confirmationstxid_in: Transaction ID on the blockchainvalue: Amount received- And any custom parameters you provided in the
paramsdictionary
By default, callbacks are sent as GET requests, but you can set post: 1 in the cryptapi_params to receive POST requests instead.
Note: The callback URL is automatically URL encoded to ensure it's properly handled by the API. The library preserves the structure of the URL by maintaining essential special characters (:/?=&) while encoding other characters that might cause issues.
For more details on callback parameters, refer to the CryptAPI documentation.
The library provides utility functions that you can use directly in your application:
from cryptapi import prepare_url
# Prepare and encode a URL with parameters
base_url = "https://example.com/callback"
params = {"order_id": "12345", "customer_id": "C12345"}
encoded_url = prepare_url(base_url, params)
print(f"Encoded URL: {encoded_url}")The prepare_url function:
- Appends parameters to a URL using the appropriate separator (? or &)
- URL-encodes the result while preserving essential URL structure characters (:/?=&)
- Works with both absolute and relative URLs
from cryptapi import CryptAPIHelper
ca = CryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# Get logs for this callback URL
logs = ca.get_logs()
# Process the logs
if logs.get('status') == 'success':
callbacks = logs.get('callbacks', [])
print(f"Found {len(callbacks)} callback records")
for callback in callbacks:
print(f"Transaction: {callback.get('txid_in')}")
print(f"Amount: {callback.get('value')} {coin}")
print(f"Confirmations: {callback.get('confirmations')}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
ca = AsyncCryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# Get logs for this callback URL
logs = await ca.get_logs()
# Process the logs
if logs.get('status') == 'success':
callbacks = logs.get('callbacks', [])
print(f"Found {len(callbacks)} callback records")
for callback in callbacks:
print(f"Transaction: {callback.get('txid_in')}")
print(f"Amount: {callback.get('value')} {coin}")
print(f"Confirmations: {callback.get('confirmations')}")
# Run the async function
asyncio.run(main())The get_logs() method returns a dictionary containing:
status: The status of the request ('success' or 'error')callbacks: An array of callback records, each containing transaction details- Additional information about the callback history
from cryptapi import CryptAPIHelper
ca = CryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# First, generate the address
ca.get_address()
# Then generate a QR code with an optional amount and custom size
value = '0.001' # Optional amount to pay
size = 300 # Size in pixels (default is 300)
qr_result = ca.get_qrcode(value, size)
# The QR code is provided as a base64 encoded image
qr_code_base64 = qr_result.get('qr_code')
print(f"QR Code (base64): {qr_code_base64}")
# The payment URI can be used directly in wallet apps
payment_uri = qr_result.get('payment_uri')
print(f"Payment URI: {payment_uri}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
ca = AsyncCryptAPIHelper(coin, my_address, callback_url, params, cryptapi_params)
# First, generate the address
await ca.get_address()
# Then generate a QR code with an optional amount and custom size
value = '0.001' # Optional amount to pay
size = 300 # Size in pixels (default is 300)
qr_result = await ca.get_qrcode(value, size)
# The QR code is provided as a base64 encoded image
qr_code_base64 = qr_result.get('qr_code')
print(f"QR Code (base64): {qr_code_base64}")
# The payment URI can be used directly in wallet apps
payment_uri = qr_result.get('payment_uri')
print(f"Payment URI: {payment_uri}")
# Run the async function
asyncio.run(main())Important: You must first call get_address() before generating a QR code, as the QR code is for the payment address.
The get_qrcode() method returns a dictionary containing:
qr_code: A base64 encoded image of the QR codepayment_uri: The cryptocurrency payment URI encoded in the QR code (can be used in wallet apps)
from cryptapi import CryptAPIHelper
# This is a static method, so you can call it directly without initializing
fees = CryptAPIHelper.get_estimate('btc', addresses=1, priority='default')
print(f"Estimated fee: {fees.get('estimated_cost')} BTC")
print(f"Estimated fee in USD: ${fees.get('estimated_cost_usd')}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
# This is a static method, so you can call it directly without initializing
fees = await AsyncCryptAPIHelper.get_estimate('btc', addresses=1, priority='default')
print(f"Estimated fee: {fees.get('estimated_cost')} BTC")
print(f"Estimated fee in USD: ${fees.get('estimated_cost_usd')}")
# Run the async function
asyncio.run(main())Parameters:
coin: The cryptocurrency to check (e.g., 'btc', 'eth', 'bep20_usdt')addresses: The number of addresses to forward funds to (default: 1)priority: Transaction priority - 'default', 'fast', or 'fastest' (default: 'default')
The get_estimate() method returns a dictionary containing:
estimated_cost: Estimated transaction fee in the cryptocurrencyestimated_cost_usd: Estimated transaction fee in USD
from cryptapi import CryptAPIHelper
ca = CryptAPIHelper('btc', my_address, callback_url, params, cryptapi_params)
# Convert 100 EUR to BTC
conversion = ca.get_conversion('eur', 100)
print(f"100 EUR = {conversion.get('value_coin')} BTC")
print(f"Exchange rate: 1 EUR = {conversion.get('exchange_rate')} BTC")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
ca = AsyncCryptAPIHelper('btc', my_address, callback_url, params, cryptapi_params)
# Convert 100 EUR to BTC
conversion = await ca.get_conversion('eur', 100)
print(f"100 EUR = {conversion.get('value_coin')} BTC")
print(f"Exchange rate: 1 EUR = {conversion.get('exchange_rate')} BTC")
# Run the async function
asyncio.run(main())Parameters:
from_coin: Currency to convert from, FIAT (e.g., 'usd', 'eur') or cryptovalue: Amount to convert
The get_conversion() method returns a dictionary containing:
value_coin: The converted amount in the target cryptocurrencyexchange_rate: The exchange rate between the two currencies
from cryptapi import CryptAPIHelper
# Get info about a specific coin
btc_info = CryptAPIHelper.get_info('btc')
print(f"BTC Info: {btc_info}")
# Get info about all coins (no parameter)
all_coins_info = CryptAPIHelper.get_info()
print(f"Total coins with info: {len(all_coins_info)}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
# Get info about a specific coin
btc_info = await AsyncCryptAPIHelper.get_info('btc')
print(f"BTC Info: {btc_info}")
# Get info about all coins (no parameter)
all_coins_info = await AsyncCryptAPIHelper.get_info()
print(f"Total coins with info: {len(all_coins_info)}")
# Run the async function
asyncio.run(main())Parameters:
coin: (Optional) Specific coin to get info for. If omitted, returns info for all coins.
The get_info() method returns a dictionary containing detailed information about the requested coin(s).
from cryptapi import CryptAPIHelper
# Get a dictionary of all supported coins
supported_coins = CryptAPIHelper.get_supported_coins()
# Print the number of supported coins
print(f"Number of supported coins: {len(supported_coins)}")
# Print the list of supported coins
for ticker, name in supported_coins.items():
print(f"{ticker}: {name}")import asyncio
from cryptapi import AsyncCryptAPIHelper
async def main():
# Get a dictionary of all supported coins
supported_coins = await AsyncCryptAPIHelper.get_supported_coins()
# Print the number of supported coins
print(f"Number of supported coins: {len(supported_coins)}")
# Print the list of supported coins
for ticker, name in supported_coins.items():
print(f"{ticker}: {name}")
# Run the async function
asyncio.run(main())The get_supported_coins() method returns a dictionary with:
- Keys: Cryptocurrency tickers (e.g., 'btc', 'eth', 'bep20_usdt')
- Values: Full names of the cryptocurrencies
Both APIs raise a CryptAPIException when the CryptAPI service returns an error. You should handle these exceptions in your code:
from cryptapi import CryptAPIHelper, CryptAPIException
try:
ca = CryptAPIHelper('btc', my_address, callback_url, params, cryptapi_params)
address = ca.get_address()
# Process result...
except CryptAPIException as e:
print(f"CryptAPI error: {str(e)}")
except Exception as e:
print(f"General error: {str(e)}")For async code:
import asyncio
from cryptapi import AsyncCryptAPIHelper, CryptAPIException
async def main():
try:
ca = AsyncCryptAPIHelper('btc', my_address, callback_url, params, cryptapi_params)
address = await ca.get_address()
# Process result...
except CryptAPIException as e:
print(f"CryptAPI error: {str(e)}")
except Exception as e:
print(f"General error: {str(e)}")
asyncio.run(main())The AsyncCryptAPIHelper automatically configures a secure SSL context using the certifi package:
import ssl
import certifi
# This happens automatically when you create an AsyncCryptAPIHelper instance
ssl_context = ssl.create_default_context(cafile=certifi.where())Need help or support?
Contact us @ https://cryptapi.io/contacts/
- Initial Release
- Various minor fixes and improvements
- Improved error handling
- Added asynchronous API support via AsyncCryptAPIHelper
- Added aiohttp and certifi dependencies
- Added SSL context for secure connections
- Comprehensive documentation updates
