x402: how software pays for the web, one request at a time

Every web response carries a status code. You know 404 (not found) and 500 (server error). Since 1997 there's also been 402 Payment Required, marked "reserved for future use", because nobody worked out how to charge a cent online when a card payment costs about 30. x402 is that missing piece: a price in the 402 answer, a signed stablecoin payment in the retry, settled in about a second. No accounts, no API keys, no subscriptions, which is exactly what AI agents need, because they can't fill in a card form.

The four steps

  1. Ask. The agent sends an ordinary request, with no login and no key.
  2. Price. The server answers 402 with a quote in the payment-required header: the amount, the token (USDC), the network (Base), the wallet to pay and how long the offer lasts.
  3. Pay. The agent's wallet signs a permission for exactly that amount (EIP-3009, so it pays no gas) and sends the same request again with it in a payment-signature header.
  4. Deliver. The server has a facilitator check the signature and balance, does the work, settles the payment on-chain and answers 200 with the result and a receipt.

Live demo: step 1 and 2, from your browser

This button makes a real, unpaid call to one of our tools, dns_lookup ($0.001), and shows the 402 quote our server sends back. Nothing is charged; there's no wallet on this page.

Press the button to see the real 402 answer.

Steps 3 and 4: let your code pay

The official x402 client wraps fetch. When a request comes back 402, it signs the quoted payment and retries, so your code looks like any other API call:

// npm i @x402/fetch @x402/evm viem
import { wrapFetchWithPaymentFromConfig } from "@x402/fetch";
import { ExactEvmScheme } from "@x402/evm";
import { privateKeyToAccount } from "viem/accounts";

// A wallet with a few dollars of USDC on Base. It never needs ETH: the facilitator pays the gas.
const account = privateKeyToAccount(process.env.AGENT_KEY);
const pay = wrapFetchWithPaymentFromConfig(fetch, {
  schemes: [{ network: "eip155:8453", client: new ExactEvmScheme(account) }],
});

// Same request as before. On a 402 it signs the quoted payment and retries by itself.
const res = await pay("https://aipaygen.com/v1/dns_lookup", {
  method: "POST", headers: { "content-type": "application/json" },
  body: JSON.stringify({ domain: "stripe.com" }),
});
console.log(await res.json()); // the result, plus a payment-response header with the receipt

Agents that speak MCP can skip the code: connect https://aipaygen.com/mcp and every paid tool works the same way. Prefer prepaid credit? Buy credit with a card and use an API key instead.

Why stablecoins

The payment has to be tiny, instant and made by software. A card payment costs about 30 cents plus 3%, so a 1-cent charge is impossible. USDC on Base costs a fraction of a cent to settle, works on weekends, and an agent can hold a wallet with a spending limit set by its owner, which it can't do with a bank account.

Sell with x402

Have an API? Put it behind our gateway and get paid per call, straight to your wallet. Have a website? Charge AI crawlers per page while people browse free.