x402 revives HTTP's forgotten status code to enable instant, permissionless payments between AI agents and APIs — no accounts, no API keys, no forms.
The current API economy requires accounts, KYC, API keys, and subscriptions — a flow that barely works for humans and is completely broken for autonomous AI agents.
Walk through a complete x402 exchange — from the first unauthenticated request all the way to settled payment and resource delivery.
x402 ships first-class packages for Express, Next.js, Hono, Fastify, Go, and Python. Buyers get a drop-in fetch wrapper that handles the entire payment flow transparently.
import express from "express"; import { paymentMiddleware } from "@x402/express"; const app = express(); // Require payment on any route — that's it app.use( paymentMiddleware({ "GET /weather": { accepts: [{ scheme: "exact", price: "$0.001", network: "base-mainnet", payTo: "0xYourWalletAddress", }], description: "Real-time weather data", }, }) ); app.get("/weather", (req, res) => { res.json({ temp: 72, condition: "sunny" }); }); app.listen(3000);
import { paymentMiddleware } from "@x402/next"; // Drop this in middleware.ts at your project root export default paymentMiddleware({ "/api/data": { accepts: [{ scheme: "exact", price: "$0.01", network: "base-mainnet", payTo: "0xYourWalletAddress", }], description: "Premium dataset access", }, }); export const config = { matcher: ["/api/data"], };
from fastapi import FastAPI from x402.frameworks.fastapi import X402Middleware app = FastAPI() # Wrap with payment middleware app.add_middleware( X402Middleware, routes={ "/data": { "price": "$0.001", "network": "base-mainnet", "pay_to": "0xYourWalletAddress", } } ) @app.get("/data") async def get_data(): return {"result": "premium data"}
import { wrapFetchWithPayment } from "@x402/client"; import { createWalletClient } from "viem"; // Give your agent a wallet const wallet = createWalletClient({ /* your config */ }); // Wrap fetch — payments are handled automatically const fetchWithPayment = wrapFetchWithPayment(fetch, wallet); // Your agent calls APIs as normal const res = await fetchWithPayment("https://api.example.com/weather"); const data = await res.json(); // x402 handles the full payment flow transparently: // 1. Receives 402 + payment instructions // 2. Signs and submits payment payload // 3. Retries with X-Payment header // 4. Returns the 200 response to your code
x402 is a neutral open standard — not tied to any chain or issuer. Today's primary networks are Base and Solana, with full support for any EVM chain and Algorand.
Any digital resource served over HTTP can be monetized per-request — no subscriptions, no credit systems, no signup gates required.