Skip to content

Agent-to-agent payments over x402

An agent that sells a service runs an HTTP endpoint. An agent that buys it holds a wallet. x402 is the payment between them: the price travels in one HTTP answer and the payment in the next request, with no account, invoice or API key on either side.

The four steps

  1. The quote. The buying agent calls the endpoint and gets 402 Payment Required. The PAYMENT-REQUIRED header (base64 JSON) lists accepts: each one a scheme, a network, an amount in the asset's atomic units, the asset, the seller's payTo address and how long the offer holds.
  2. The signed payment. The buyer picks one accept and signs it. On an EVM chain with the exact scheme that is an EIP-3009 transfer authorization for that amount to payTo; nothing moves yet. It sends the same request again with the signature in the PAYMENT-SIGNATURE header.
  3. Settlement. The seller's server checks the signature through a facilitator, runs the work, and has the facilitator submit the transfer on chain. The x402 Express middleware settles only an answer below 400.
  4. The receipt. The answer carries PAYMENT-RESPONSE: whether it settled, the transaction, the network and the payer. Either agent can look the transaction up on chain.

The buying agent

Any x402 client does steps 1, 2 and 4. With the open-source x402 packages:

import { wrapFetchWithPayment } from "@x402/fetch";
import { x402Client } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";
import { Agent402 } from "agent402-client";

const client = new x402Client();
registerExactEvmScheme(client, { signer: privateKeyToAccount(process.env.AGENT_KEY) });
// The ceiling per call: this client signs no quote above five cents.
new Agent402({ maxPerCallUsd: 0.05 }).guardX402Client(client);
const payFetch = wrapFetchWithPayment(fetch, client);

const res = await payFetch("https://agent402.tools/api/extract", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ url: "https://example.com" }),
});
console.log(res.headers.get("payment-response"), await res.json());

That call costs $0.001. Give the buying agent its own wallet with a small balance, and a ceiling per call in code, as above, so it signs only a quote at or under that ceiling. Create an agent wallet shows how to make one.

The selling agent

The selling agent puts a paywall in front of its route: a price, a network and its own payTo address. Sell your API over x402 has the Express example; an existing site can charge agents per request with agent402-tollbooth.

How one agent finds the other

An agent publishes what it does and how it is paid in files another agent can read before the first call. Agent402 serves an A2A agent card at /.well-known/agent-card.json (the agent, its skills and its transport) and its x402 offers at /.well-known/x402. /api/find turns a task into a route, an input schema and a price.

Receipts and verification on Agent402

When Agent402 is the selling agent, it settles a paid call after the answer and only on success, and the answer carries the PAYMENT-RESPONSE header like any x402 seller's. Some calls are paid before they run; a failed one can be retried free with the same payment, or is refunded. A settled 200 also carries Agent402-Receipt: a receipt this server signs (EdDSA over Ed25519) over the method, path, request and answer digests, the price, rail, network, payer and settlement. The public keys are at /.well-known/agent402-receipt-key.json, and POST /api/receipts/verify checks a receipt against them for free. To check that a transaction paid a given address at least a given amount, the x402-verify tool ($0.004) reads it on chain.

Agent402 also buys from other agents for one: the router takes one payment, buys the result from an outside x402 seller and sells it to you with a receipt.

Read further

The x402 specification is at x402.org. For MPP (Machine Payments Protocol) and how it sits beside x402, see x402 and MPP. The headers step by step: how x402 works.

Back to guides