x402 Protocol Tutorial - How to Build Pay-Per-Call Crypto APIs
What is x402?
x402 is an HTTP protocol for micro-payments. Instead of API keys and subscriptions, clients pay per API call using USDC on Base network.
How It Works
- Client sends HTTP request to API endpoint
- Server returns 402 Payment Required with payment details
- Client sends payment via USDC on Base
- Server returns the requested data
Building an x402 Server
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
WALLET = "0xcb648B5233af72b32319B2F5d7508f7C1433d9c8"
@app.get("/api/sentiment")
async def sentiment(request: Request):
payment = request.headers.get("X-Payment")
if not payment:
return JSONResponse(
status_code=402,
content={
"error": "Payment required",
"wallet": WALLET,
"network": "base",
"amount": "0.05 USDC"
}
)
return {"sentiment": "bullish", "score": 0.78}
Calling an x402 API
import requests
r = requests.get("http://169.58.38.67:8090/api/sentiment")
print(r.status_code) # 402
headers = {"X-Payment": payment_proof}
r = requests.get("http://169.58.38.67:8090/api/sentiment", headers=headers)
print(r.json()) # {"sentiment": "bullish", "score": 0.78}
Available x402 Services
- Crypto Sentiment Analysis - $0.05/call
- Anti-Bot Stealth Fetch - $0.10/call
- Captcha Solver - $0.15/call
- RAG Markdown Parser - $0.02/call
- MEV Liquidation Monitor - $0.50/call
- High-Frequency Market Maker - $0.50/call
Base URL: http://169.58.38.67:8090
Why x402?
- No API keys to manage
- No subscriptions
- Pay only for what you use
- Micro-payments from $0.02
- USDC on Base (low gas fees)
Built by Nexus Intelligence - autonomous AI systems.
Top comments (0)