Skip to content

x402 — Stand up a test endpoint

This recipe spins up a minimal paywall in ~25 lines of FastAPI using Coinbase's official x402 package.

Prerequisites

Requirement Notes
Python 3.11+
A Base Sepolia address This is where the $0.001 test USDC lands — any address you control
pip Standard Python package manager

Install

pip install "x402[fastapi,evm]>=2.6,<3" fastapi uvicorn

Server code

Save as server.py (replace PAY_TO with your Base Sepolia address):

from fastapi import FastAPI
from x402.http.facilitator_client import FacilitatorConfig, HTTPFacilitatorClient
from x402.http.middleware.fastapi import PaymentMiddlewareASGI
from x402.http.types import RouteConfig
from x402.http.x402_http_server import PaymentOption
from x402.mechanisms.evm.exact import ExactEvmServerScheme
from x402.server import x402ResourceServer

app = FastAPI()
PAY_TO = "0xYourBaseSepoliaAddress"

facilitator = HTTPFacilitatorClient(FacilitatorConfig(url="https://x402.org/facilitator"))
server = x402ResourceServer(facilitator_clients=facilitator)
server.register("eip155:84532", ExactEvmServerScheme())

routes = {
    "GET /weather": RouteConfig(
        accepts=PaymentOption(
            scheme="exact",
            pay_to=PAY_TO,
            price="$0.001",
            network="eip155:84532",
        )
    )
}
app.add_middleware(PaymentMiddlewareASGI, routes=routes, server=server)

@app.get("/weather")
async def weather():
    return {"weather": "sunny"}

Run

uvicorn server:app --port 4021

Verify

curl -si http://localhost:4021/weather | head -5
# HTTP/1.1 402 Payment Required
# x-payment-required: ...

The x402.org/facilitator endpoint handles testnet verify/settle for free — no signup needed.

Next step

Your server is running. Now point Routeweiler at http://localhost:4021/weather — see Quickstart step 3.