How to Build a Bitcoin Checkout API
A checkout endpoint should bind an authenticated customer and request identifier to a frozen order. Store the result before returning it; retries with changed parameters must conflict. This local SQLite example models the endpoint state, not an HTTP server or a wallet call.
Source code
1import sqlite3
2
3def checkout(db, customer, request, order, sats):
4 if not all((customer, request, order)) or type(sats) is not int or sats <= 0:
5 raise ValueError("invalid checkout")
6 with db:
7 db.execute("INSERT OR IGNORE INTO checkout VALUES (?,?,?,?)", (customer,request,order,sats))
8 row = db.execute("SELECT order_id,sats FROM checkout WHERE customer=? AND request_id=?", (customer,request)).fetchone()
9 if row != (order,sats): raise ValueError("request conflict")
10 return row
11
12if __name__ == "__main__":
13 db=sqlite3.connect(":memory:")
14 db.execute("CREATE TABLE checkout(customer TEXT, request_id TEXT, order_id TEXT, sats INTEGER, PRIMARY KEY(customer,request_id))")
15 assert checkout(db,"c","r","o",500)==checkout(db,"c","r","o",500)
16 try: checkout(db,"c","r","o",501)
17 except ValueError: pass
18 else: raise AssertionError("conflict accepted")Local validation
Run stage24-checkout-api.py with Python 3. The example uses synthetic data and does not execute live wallet operations. Production deployment requires authenticated access, durable storage, trusted chain evidence and integration testing.
Related documentation
https://thamer.org/api
