Bitcoin
¿Listo para usar esto en producción?
El plan gratuito cubre proyectos personales. El pago por uso escala sin necesidad de tarjeta.
Bitcoin
El plan gratuito cubre proyectos personales. El pago por uso escala sin necesidad de tarjeta.
Bitcoin has no single dominant high-level Python SDK the way some ecosystems do — you call the Bitcoin Core JSON-RPC API directly. Two approaches cover almost every case: the plain requests library, where you build the JSON-RPC envelope yourself, or the thin python-bitcoinrpc wrapper, whose AuthServiceProxy maps RPC method names onto Python methods so calls read like proxy.getblockchaininfo(). Reach for requests when you want full control over the request, retries, and error handling; reach for python-bitcoinrpc when you want the terse method-call syntax. Both point at https://bitcoin.therpc.io/YOUR_API_KEY, the key coming from your TheRPC dashboard.
Every request uses Bitcoin Core's legacy JSON-RPC 1.0 envelope — {'jsonrpc': '1.0', 'id': 'therpc', 'method': ..., 'params': [...]} — and params is always a positional list: arguments go in a fixed order, never as keyword fields. Your TheRPC key lives in the URL path (https://bitcoin.therpc.io/YOUR_API_KEY), so the body you send is the same one any Bitcoin node would accept.
Install with pip install requests, then wrap the endpoint in one rpc() helper that posts the 1.0 envelope and returns the result. Because a failed Bitcoin RPC comes back as a normal HTTP 200 with the failure in the body's error field, the helper inspects data['error'] and raises a RuntimeError on it rather than trusting the status code. From there, reading the chain tip with getblockcount or decoding a verbose transaction with getrawtransaction (second positional arg True) is a single call.
Install with pip install python-bitcoinrpc. Its AuthServiceProxy takes the full endpoint URL — API key included in the path — and turns each Bitcoin RPC into an attribute call, so getblockchaininfo becomes rpc.getblockchaininfo() and positional params are just Python arguments. That makes for compact code: read chain state, estimate a fee rate with estimatesmartfee(6) (BTC/kvB, here converted to sat/vByte), and broadcast a signed transaction with sendrawtransaction. The wrapper raises JSONRPCException when the node rejects a call, so a node-level error like a non-final or low-fee transaction surfaces as a catchable exception carrying the original message.
Since Bitcoin holds funds as unspent transaction outputs (UTXOs) instead of account balances, you read one output by its txid and index with gettxout. When that output is still unspent, you get back an object with its value in BTC and scriptPubKey details; when it has already been spent — or was never in the UTXO set — the node returns JSON null, which arrives in Python as None. That None is the expected "already spent / unknown" answer, not an error, so branch on utxo is None rather than letting it raise downstream.
AuthServiceProxy wrapper that maps Bitcoin RPC method names onto Python calls.sendrawtransaction.