x402 Payment Protocol
x402 is an open protocol for internet-native payments built on the long-reserved HTTP status code 402 Payment Required. It lets any HTTP API charge per request in stablecoins — no accounts, no API keys, no card forms — which makes it the natural way for AI agents to pay for the resources they use.
How it works
Client / Agent Resource server Facilitator
│ GET /weather │ │
│──────────────────────────────────▶│ │
│ 402 + PaymentRequirements │ │
│◀──────────────────────────────────│ │
│ sign EIP-3009 authorization │ │
│ GET /weather + X-PAYMENT header │ │
│──────────────────────────────────▶│ POST /verify │
│ │──────────────────────────────────▶│
│ │ { isValid: true } │
│ │◀──────────────────────────────────│
│ │ POST /settle │
│ │──────────────────────────────────▶│ ── transferWithAuthorization ─▶ chain
│ │ { success, transaction } │
│ 200 + X-PAYMENT-RESPONSE │◀──────────────────────────────────│
│◀──────────────────────────────────│ │
- The client requests a paid resource. The server answers
402with a JSON body listing the accepted payment requirements (network, token, amount, recipient). - The client signs an EIP-3009
transferWithAuthorizationfor exactly that amount and retries with the base64-encoded payload in theX-PAYMENTheader. The signature moves no funds by itself, and the client pays no gas. - The server asks a facilitator to verify the payment, serves the response, and has the facilitator settle it on-chain. The settlement transaction is returned in the
X-PAYMENT-RESPONSEheader.
Payment requirements
{
"x402Version": 1,
"accepts": [
{
"scheme": "exact",
"network": "base",
"maxAmountRequired": "10000",
"resource": "https://api.example.com/weather",
"description": "Current weather",
"mimeType": "application/json",
"payTo": "0xYourWallet",
"maxTimeoutSeconds": 60,
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"extra": { "name": "USD Coin", "version": "2" }
}
]
}
maxAmountRequired is in the token's smallest unit — 10000 is 0.01 USDC.
Accept x402 payments in your API
Use the official middleware and point it at the DeepNexus facilitator:
import express from "express";
import { paymentMiddleware } from "x402-express";
const app = express();
app.use(
paymentMiddleware(
"0xYourWallet",
{ "GET /weather": { price: "$0.01", network: "base" } },
{
url: "http://localhost:3000/x402",
createAuthHeaders: async () => {
const h = { Authorization: `Bearer ${process.env.DEEPNEXUS_FACILITATOR_KEY}` };
return { verify: h, settle: h, supported: {} };
},
},
),
);
app.get("/weather", (req, res) => res.json({ city: "Singapore", temp: 31 }));
app.listen(4021);
Create the facilitator key in the Facilitator console. See x402 Facilitator for the API.
Pay for x402 resources from an agent
import { wrapFetchWithPayment } from "x402-fetch";
import { privateKeyToAccount } from "viem/accounts";
const account = privateKeyToAccount(process.env.AGENT_PRIVATE_KEY);
const fetchWithPay = wrapFetchWithPayment(fetch, account);
const res = await fetchWithPay("https://api.example.com/weather");
console.log(await res.json());
The x402-paywall and x402-client skills teach coding agents to do both of these for you.