A self-hosted x402 paywall + facilitator for selling LLM inference over HTTP β running live on Base mainnet, paid in USDC, on a homelab GPU.
This repo is the reference implementation behind a live endpoint. Any human or AI agent with a crypto wallet can buy a completion in one HTTP round-trip: no accounts, no API keys, no signup. Payment verifies on-chain before the model runs; settlement happens after β a failed request is never charged.
$0.01 POST /premium/llm 27B open-weights model, 4K token cap
$0.05 POST /premium/deep 8K tokens β double the thinking budget on the same 27B
$0.10 POST /premium/vl 30B vision model, image understanding
free GET /premium/balance live on-chain earnings (transparency endpoint)
free POST /premium/trial 3 calls per wallet, sybil-limited by EIP-191 signature
x402 (HTTP 402 "Payment Required", revived by Coinbase) lets software pay software. This repo is the smallest serious stack we found that does it self-hosted end to end:
x402_facilitator.py
β your own facilitator (verify + settle). No hosted service, no middleman cut, no regional blocks.x402_paywall.py
β FastAPI middleware: 402 with payment requirements β buyer signs β facilitator verifies β handler runs β settlement receipt in the response body (X-PAYMENT-RESPONSE
).x402_mcp_server.py
β MCP wrapper so Claude/desktop agents can purchase natively.scripts/x402_client_demo.py
β a working buyer in ~90 lines.
buyer agent ββPOST /premium/llmβββΆ Caddy (path allowlist) βββΆ FastAPI paywall
β² β
β 402 + payment requirements β verify via
β βΌ facilitator
wallet signs βββββββββββββββΆ on-chain (Base)
β
handler runs AFTER verify
settles AFTER success (β₯400 cancels)
Security posture (the interesting part for homelab operators):
- The public internet reaches only the paywall routes. A reverse-proxy path allowlist 404s everything else β the rest of the machine (fleet control, admin API, this repo's private origin) is unreachable by design. - Wallet keys are age-encrypted at rest, decrypted in memory only.
- Prompts are not logged or retained. The response dies with the connection.
- Token caps per tier bound worst-case compute cost per call; every call is prepaid, so abuse is self-funding (it pays us).
pip install fastapi uvicorn x402 c web3
age-encrypt your wallet json β vault/x402_receive_wallet.age
uvicorn app:app --port 8900 # app wires the middleware onto your routes
Point a buyer at it:
python3 scripts/x402_client_demo.py "$URL/premium/llm" "Write a haiku about payments"
- Live on Base mainnet (chain 8453), USDC settlement.
- First real external settlement: 0.12 USDC and counting β see the balance endpoint on the live deployment.
- Hardware: one RTX 5090 running an AWQ-quantized 27B. ~70% gross margin per $0.01 call after electricity.
This is homelab infra, not an SLA product: best-effort uptime, one GPU, no data-retention paperwork β because there is no data retention. Positioning is exactly that: best-effort, cheap, keyless, and honest.
License: MIT.