Meta description:
Looking for a concise crypto payment integration python guide? This post covers everything from choosing the right wallets and APIs to writing clean Python code with modern tools. Learn how to accept Bitcoin, Ethereum, and stablecoins in your web app, and discover the best developer tools to speed up the process.
---
Python’s readability, extensive ecosystem, and mature libraries make it the go‑to language for fintech developers. Whether you’re building a Shopify‑style storefront or a custom SaaS, a crypto payment integration python guide can help you:
web3.py and bitcoinlib.---
| Cryptocurrency | Network | Typical Use‑Case | Python Library |
|----------------|---------|------------------|----------------|
| Bitcoin (BTC) | Bitcoin | Direct payments, high liquidity | bitcoinlib |
| Ethereum (ETH) | ERC‑20 | Smart contracts, DeFi | web3.py |
| Tether (USDT) | ERC‑20 | Stablecoin, invoicing | web3.py |
| Binance Smart Chain | BSC | Low‑fee, high‑throughput | web3.py |
OpenNode or BTCPayServer to keep full control.**Tip:** If you’re a developer who loves control, start with a self‑hosted node and the `web3.py` library. For rapid deployment, choose a hosted API.
---
python3 -m venv venv
source venv/bin/activate
pip install fastapi uvicorn web3 python-dotenv
Create a .env file to store your API keys securely:
ETH_NODE_URL="https://mainnet.infura.io/v3/YOUR_INFURA_KEY"
BITCOIN_NODE_URL="http://localhost:8332"
---
# main.py
from fastapi import FastAPI, HTTPException
from web3 import Web3
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
w3 = Web3(Web3.HTTPProvider(os.getenv("ETH_NODE_URL")))
@app.post("/create-payment")
async def create_payment(amount_eth: float, recipient: str):
if not w3.isAddress(recipient):
raise HTTPException(status_code=400, detail="Invalid Ethereum address")
tx = {
"to": recipient,
"value": w3.toWei(amount_eth, "ether"),
"gas": 21000,
"gasPrice": w3.toWei("50", "gwei"),
}
signed_tx = w3.eth.account.sign_transaction(tx, private_key=os.getenv("PRIVATE_KEY"))
tx_hash = w3.eth.sendRawTransaction(signed_tx.rawTransaction)
return {"tx_hash": tx_hash.hex()}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Explanation
1. FastAPI provides a lightweight, async web server.
2. Web3.py connects to an Ethereum node via Infura.
3. The endpoint signs and sends a transaction using the merchant’s private key.
---
# bitcoin_payment.py
from bitcoinlib.wallets import Wallet
import os
def create_btc_payment(amount_btc, recipient_address):
wallet = Wallet.create('merchant_wallet')
tx = wallet.send_to(recipient_address, amount_btc)
return tx.info()
*Security note:* Keep the wallet seed in a hardware wallet or HSM. Never store private keys in source control.
---
If you prefer a quick start, here’s how to use CoinGate:
import requests, os
def create_coin_gate_invoice(amount, currency):
url = "https://api.coingate.com/v2/invoice/create"
payload = {
"price_amount": amount,
"price_currency": currency,
"receive_currency": currency,
"callback_url": "https://yourdomain.com/callback",
"cancel_url": "https://yourdomain.com/cancel",
"success_url": "https://yourdomain.com/success",
"order_id": "12345",
}
headers = {"Content-Type": "application/json", "Authorization": f"Token {os.getenv('COINGATE_API_KEY')}"}
r = requests.post(url, json=payload, headers=headers)
return r.json()
Why this matters:
Hosted APIs reduce compliance overhead, automatically handle exchange rates, and provide webhooks for real‑time status updates.
---
| Item | Why it matters | Tool |
|------|----------------|------|
| Unit tests | Catch bugs early | pytest, hypothesis |
| Static analysis | Detect insecure patterns | bandit, safety |
| Docker | Consistent env | Docker |
| Secrets management | Prevent leaks | HashiCorp Vault, AWS Secrets Manager |
| Rate limiting | Protect from DoS | fastapi-limiter |
---
1. Containerize the FastAPI app with Docker.
2. Use GitHub Actions to run tests and push to Docker Hub.
3. Deploy to a cloud provider (e.g., AWS ECS, GCP Cloud Run).
4. Secure the endpoint with HTTPS (Let’s Encrypt) and a WAF.
---
---
---
A: Not always. For most applications, a reputable node provider (Infura, Alchemy) or a hosted API is sufficient. Full nodes are ideal if you need full control or want to avoid third‑party dependencies.
A: Use a payment processor that offers fiat gateways, or integrate a price feed (e.g., CoinGecko API) to calculate real‑time exchange rates before generating invoices.
A: Store keys in a secure vault (HashiCorp Vault, AWS KMS), use environment variables, never commit them to VCS, and rotate keys regularly. For high security, consider using a hardware wallet or an HSM.
---
Ready to start building?
Download the full code examples from our GitHub repository and start integrating crypto payments into your Python application today!
Browse 120+ Python tools with crypto payments and instant delivery.
Browse Products →