DEV Community

nanoempireai
nanoempireai

Posted on

How to Add x402 Payments to FastAPI in 5 Minutes

How to Add x402 Payments to FastAPI in 5 Minutes

Turn any FastAPI endpoint into a paid API that autonomous AI agents can discover and pay automatically.

The Problem

You've built a great API. But AI agents can't use it because:

  • They can't fill out Stripe checkout forms
  • They don't have credit cards
  • They need programmatic, instant payments

The Solution: x402 (HTTP 402 Payment Required)

x402 is the web standard for machine-to-machine micropayments. Your API returns HTTP 402 with a payment challenge; the agent pays USDC on Base (gasless via EIP-3009) and retries with a cryptographic receipt.

5-Minute Integration

1. Install the SDK

pip install nano-empire-tollbooth
Enter fullscreen mode Exit fullscreen mode

2. Protect Your Endpoint

from fastapi import FastAPI
from nano_empire_tollbooth import monetize

app = FastAPI()

@monetize(price_usd=0.05)
@app.post("/api/process")
async def process_data(data: dict):
    return {"result": "processed"}
Enter fullscreen mode Exit fullscreen mode

That's it. Your endpoint now:

  • Returns HTTP 402 for unpaid requests
  • Supports free tier (5 calls/day/IP) and paid tier ($0.05/call)
  • Settles in USDC on Base (gasless, instant)
  • Builds a public proof chain

3. Configure Treasury Wallet

export TREASURY_WALLET="0xYourUSDCWalletOnBase"
Enter fullscreen mode Exit fullscreen mode

4. Test It

curl -X POST http://localhost:8000/api/process   -H "Content-Type: application/json"   -d '{"input": "test"}'
Enter fullscreen mode Exit fullscreen mode

Live Proof It Works

Our parser API has processed 5 paid calls from 2 autonomous agents, earning $0.125 total. Every transaction is publicly verifiable: View Live Proof Chain

Get Started

pip install nano-empire-tollbooth
Enter fullscreen mode Exit fullscreen mode

One decorator. Your API is now agent-payable.

Top comments (0)