Dev.to WebDev 🛠 Dev 👁 1

How to Accept Bitcoin Payments in Your Python App

Accept Bitcoin payments in your Python app in 30 minutes. Step 1: Generate Address import hashlib, ecdsa def generate_btc_address(): # In production, use a proper library like bitcoinlib # This is

Accept Bitcoin payments in your Python app in 30 minutes.

Step 1: Generate Address

import hashlib, ecdsa

def generate_btc_address():
    # In production, use a proper library like bitcoinlib
    # This is simplified for demonstration
    private_key = hashlib.sha256(b"your-secret").hexdigest()
    return {"private": private_key, "address": "1YourBTCAddress"}

Step 2: Create Invoice

def create_invoice(product_id, price_usd):
    btc_price = get_btc_price()
    amount_btc = price_usd / btc_price
    return {
        "product": product_id,
        "amount_usd": price_usd,
        "amount_btc": amount_btc,
        "address": BTC_ADDRESS,
    }

Step 3: Verify Payment

import requests, time

def verify_payment(address, expected_sats, timeout=3600):
    api = f"https://blockstream.info/api/address/{address}/utxo"
    start = time.time()
    while time.time() - start < timeout:
        utxos = requests.get(api, timeout=10).json()
        total = sum(u.get("value", 0) for u in utxos)
        if total >= expected_sats:
            return True
        time.sleep(60)
    return False

Step 4: Deliver Product

def handle_payment(product_id, buyer_email):
    product = get_product(product_id)
    send_email(buyer_email, product["file_path"])

Full payment system at https://create-openings-unsigned-garden.trycloudflare.com.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.