Roomsby Aurmada

x402 for agents: paying per call over HTTP

Updated

x402 lets an agent pay for an API call the moment it needs it, with no account, API key or subscription. It revives the HTTP status code 402 Payment Required: the server names a price, the client pays in stablecoins (usually USDC on Base) and retries the same request.

The loop

  1. Request. The agent calls the endpoint like any API.
  2. 402. The server answers 402 with its payment requirements: scheme (exact), network (e.g. Base, eip155:8453), asset (USDC), amount, payTo address and a timeout. x402 v2 puts them in the PAYMENT-REQUIRED header (many servers repeat them in the body); v1 used the body.
  3. Sign. The agent's wallet signs a payment authorization for exactly that amount (for USDC, an EIP-3009 transfer authorization). Nothing moves yet.
  4. Retry. The agent resends the same request with the signed payload in PAYMENT-SIGNATURE (v2) or X-PAYMENT (v1).
  5. Settle and answer. The server (usually through a facilitator) verifies and settles the payment on-chain and returns the response, with a settlement receipt in PAYMENT-RESPONSE (v2) or X-PAYMENT-RESPONSE (v1). Good servers settle only after a successful answer.

See a price (free)

An unpaid request costs nothing and shows the terms:

curl -i -X POST https://rooms.aurmada.com/x402/v1/broker/<slug> -H 'Content-Type: application/json' -d '{}'

Pay from code (TypeScript)

With the official x402 packages and a viem account holding USDC on Base:

// npm i @x402/core @x402/evm viem
import { x402Client, x402HTTPClient } from "@x402/core/client";
import { registerExactEvmScheme } from "@x402/evm/exact/client";
import { privateKeyToAccount } from "viem/accounts";

const signer = privateKeyToAccount(process.env.WALLET_KEY as `0x${string}`);
const client = new x402Client();
registerExactEvmScheme(client, { signer });
const http = new x402HTTPClient(client);

const url = "https://api.example.com/paid-endpoint";
let res = await fetch(url);
if (res.status === 402) {
  const required = http.getPaymentRequiredResponse((h) => res.headers.get(h), await res.json().catch(() => undefined));
  const payload = await client.createPaymentPayload(required);
  res = await fetch(url, { headers: http.encodePaymentSignatureHeader(payload) });
}
console.log(res.status, await res.text());

Keep a spending limit in your agent: check the amount in the 402 before signing, and use a wallet that holds only what the agent may spend.

Finding x402 APIs

Sellers can list their endpoints in public discovery lists such as the x402 Bazaar. The Rooms directory lists 69 services that take x402 payments, each checked with an unpaid request: browse x402 APIs or by capability.

Calling through Rooms

For x402 APIs in the directory, Rooms can resell the call: you make one x402 payment to Rooms (the seller's price plus a small margin), Rooms pays the seller and returns its answer with a signed receipt, and you're never charged when the seller fails. Each listing shows the exact POST /x402/v1/broker/<slug> to make. Sell an API yourself? List it free and claim it for uptime monitoring and a badge.

Related: A2A vs MCP · How to call an A2A agent