x402
x402 is an open protocol that makes HTTP 402 Payment Required a working payment flow: the server puts machine-readable payment terms in a PAYMENT-REQUIRED header, the client retries with a signed stablecoin authorization in PAYMENT-SIGNATURE, a facilitator verifies and settles it on chain, and the answer carries a PAYMENT-RESPONSE receipt.
The short version
x402 is an open protocol for paying for an HTTP request with the request itself. There is no account to open and no API key to issue: the server states a price, the client pays it, and the same URL answers. It is built for software that has to buy things without a human in the loop, which makes it a natural fit for AI agents paying for tools and data.
It uses three headers. PAYMENT-REQUIRED on a 402 response carries the offer. PAYMENT-SIGNATURE on the retry carries the payment. PAYMENT-RESPONSE on the successful answer carries the receipt, including the on-chain transaction.
How one payment works
- The client calls a paid URL with no payment attached.
- The server answers
402 Payment Requiredwith aPAYMENT-REQUIREDheader: base64 JSON listing each accepted way to pay (scheme, network, asset, amount, recipient, validity window). - The client picks one entry and signs an authorization for exactly that amount to exactly that recipient. On EVM chains this is an EIP-3009
transferWithAuthorization, so the payer needs USDC but no gas. - The client repeats the request with the signed authorization in
PAYMENT-SIGNATURE. - The server has a facilitator verify the signature, runs the request, and settles the transfer on chain.
- The answer comes back with
PAYMENT-RESPONSEnaming the settlement transaction.
The offer this server sends
Here is the decoded PAYMENT-REQUIRED header from a request to POST /api/hash, trimmed to the Base entry. The live response lists one entry per chain this server accepts. "amount": "1000" is in the asset's smallest unit: USDC has 6 decimals, so this is $0.001.
{
"x402Version": 2,
"error": "Payment required",
"resource": {
"url": "https://agent402.tools/api/hash",
"mimeType": "application/json",
"serviceName": "Hash"
},
"accepts": [
{
"scheme": "exact",
"network": "eip155:8453",
"amount": "1000",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"payTo": "0xPAYTO...",
"maxTimeoutSeconds": 300,
"extra": { "name": "USD Coin", "version": "2" }
}
]
}
The body of the 402 is not where the offer lives. A 402 with an empty body can still be a complete offer, because the terms are in the header.
Paying from code
A stock x402 client wraps fetch: it reads the 402, signs one of the offered entries and retries, so the calling code sees an ordinary 200.
import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
const client = new x402Client();
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
const payFetch = wrapFetchWithPayment(fetch, client);
const res = await payFetch("https://agent402.tools/api/hash", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ text: "hello world" }),
});
console.log(res.status, await res.json()); // 200, and a PAYMENT-RESPONSE header
What happens when a call fails
On this server the tool runs before settlement, and settlement completes only for a successful response. An error, a timeout or an upstream failure cancels the payment, so nothing moves. You can check it from the response you hold: no PAYMENT-RESPONSE header means you were not charged. Send an Idempotency-Key header and a retry of a call that was already served and paid replays the first answer.
Related
- What is x402? (the full explainer)
- Test your x402 client against a real 402
- x402 in 5 minutes
- agent402-client
- hash, the tool in the example
- Glossary entry
More explainers: HTTP 402 Payment Required · MPP (Machine Payments Protocol) · Agent payments · Pay-per-call API · MCP payments