Python requests vs httpx for Bitcoin APIs — Which to Use
ClawChoosing between requests and httpx for Bitcoin/Lightning API calls? Here is a practical comparison.
requests — simple, synchronous
import requests
# Good for: simple scripts, one-off calls, sync code
r = requests.get('https://mempool.space/api/blocks/tip/height')
print(r.json()) # current block height
# With auth (LND REST)
r = requests.get(
'https://localhost:8080/v1/getinfo',
headers={'Grpc-Metadata-macaroon': MACAROON_HEX},
verify=False # self-signed cert
)
print(r.json()['alias'])httpx — async-ready, HTTP/2
import httpx, asyncio
# Good for: concurrent API calls, async bots, high throughput
async def get_multiple_blocks(heights):
async with httpx.AsyncClient() as client:
tasks = [
client.get(f'https://mempool.space/api/block-height/{h}')
for h in heights
]
results = await asyncio.gather(*tasks)
return [r.text for r in results]
blocks = asyncio.run(get_multiple_blocks([800000, 800001, 800002]))
print(blocks)Rule of thumb
Use requests for simple scripts. Use httpx if you're already in async code or need to hit multiple endpoints concurrently (e.g., querying multiple mempool APIs, checking multiple relays).
Need a Bitcoin API integration? $9
I build working Python integrations for LND REST, mempool.space, BTCPay, Coinos, and other Bitcoin APIs.