Bitcoin Lab API Demo
Learn how to interact with the Bitcoin Lab API using a simple Python script.
Demo Script
#!/usr/bin/env python3
"""
Bitcoin Lab API (v2) — Minimal Client Demo
Usage:
1) Install: pip install requests
2) Set token: export BITLAB_API_TOKEN="YOUR_TOKEN_HERE" (Linuc / macOS)
set BITLAB_API_TOKEN=YOUR_TOKEN_HERE (Windows)
3) Run: python api_demo.py
Notes:
- Auth uses the X-API-Token header (no token in query params).
- Edit the example endpoints/params below as you like.
"""
import os, sys, json, requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
# --- Config ---
BITLAB_API_BASE = "https://beta.thebitcoinresearcher.net"
BITLAB_API_TOKEN = os.getenv("BITLAB_API_TOKEN", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") # your token (or set export / set)
# --- HTTP session with small retry ---
s = requests.Session()
s.headers.update({"X-API-Token": BITLAB_API_TOKEN, "Accept": "application/json"})
s.mount("https://", HTTPAdapter(max_retries=Retry(
total=2, backoff_factor=0.3, status_forcelist=(429, 500, 502, 503, 504)
)))
def _url(ep: str) -> str:
return f"{BITLAB_API_BASE.rstrip('/')}/{ep.lstrip('/')}"
def _pretty(out):
if isinstance(out, (dict, list)):
print(json.dumps(out, indent=2))
else:
print(out)
def get(ep: str, params: dict):
"""GET helper. Raises on HTTP errors."""
url = _url(ep)
try:
r = s.get(url, params=params, timeout=15)
except requests.RequestException as e:
print(f"[Network error] GET {url}: {e}", file=sys.stderr)
sys.exit(1)
ct = r.headers.get("content-type", "")
return r.json() if "application/json" in ct else r.text
def main():
print("Bitcoin Lab API Demo (v2)")
if BITLAB_API_TOKEN.startswith(("xxxx", "PASTE_")):
print("⚠️ Set BITLAB_API_TOKEN first (see header).\n")
# Example 1: fees/fee_avg
print("\n# 1) unrealized_profit/unrealized_profit_lth")
resp1 = get("v2/unrealizedprofit/unrealized_profit_lth", {
"from_time": "2025-08-01 00:00",
"to_time": "2025-08-02 00:00",
"resolution": "h12",
"output_format": "json",
})
_pretty(resp1)
# Example 2: URPD query
print("\n# 2) utxo_distributions/urpd_log_lth_supply_btc")
resp2 = get("v2/utxo_distributions/urpd_log_lth_supply_btc", {
"urpd_time": "2025-08-02 00:00", # UTC assumed if no 'Z'; hh:mm[:ss] accepted
"resolution": "d1",
"output_format": "json",
})
_pretty(resp2)
if __name__ == "__main__":
main()