x402 List

x402 Protocol Service Directory

← Back to Learn

2026-04-12

How AI Agents Use X402 for Payments

--------

The Challenge

AI agents are autonomous programs that take actions on behalf of users. They browse the web, call APIs, and chain services together to complete tasks. But most paid APIs require account creation, API keys, and billing setup -- steps that assume a human in the loop. An agent that needs a new API mid-task hits a wall.

X402 removes that wall. Because payment instructions are embedded in HTTP responses, an agent with a funded wallet can discover, evaluate, and pay for any X402 service without human intervention.

Step-by-Step Flow

Here is how an AI agent interacts with an X402-enabled API:

Step 1: Discovery. The agent finds a service endpoint, either from a directory like the x402 List service directory, a search engine, or a tool manifest. It sends a standard HTTP request.

Step 2: Payment Required. The server returns HTTP 402 with payment details in the response headers:

HTTP/1.1 402 Payment Required
X-Payment-Amount: 0.001
X-Payment-Currency: USDC
X-Payment-Network: base
X-Payment-Address: 0xabc...def
X-Payment-Memo: req_7f3a9b2c

Step 3: Evaluate. The agent checks its budget policy. Is the price within the per-request limit? Is the total spend for this task within budget? If so, it proceeds. If not, it skips the service or asks the user for approval.

Step 4: Pay. The agent constructs a blockchain transaction on the specified network, sending the exact amount to the payment address with the memo attached.

Step 5: Retry with proof. The agent retries the original request, including the payment proof:

GET /api/translate?text=hello&lang=fr HTTP/1.1
Host: translate-402.example.com
X-Payment-Proof: 0x9f8e7d...abc123
X-Payment-Network: base

Step 6: Receive data. The server validates the proof on-chain and returns the requested data. The entire exchange completes in seconds.

Code Example

Here is a simplified agent loop in pseudo-code:

async function x402_request(url, options) {
  // 1. Make the initial request
  let response = await fetch(url, options);

  // 2. Check if payment is required
  if (response.status !== 402) {
    return response;
  }

  // 3. Extract payment details from headers
  let payment = {
    amount:   response.headers["X-Payment-Amount"],
    currency: response.headers["X-Payment-Currency"],
    network:  response.headers["X-Payment-Network"],
    address:  response.headers["X-Payment-Address"],
    memo:     response.headers["X-Payment-Memo"],
  };

  // 4. Check agent budget policy
  if (!budget.allows(payment.amount)) {
    throw new Error("Payment exceeds budget limit");
  }

  // 5. Submit payment on-chain
  let tx = await wallet.send({
    to:       payment.address,
    amount:   payment.amount,
    currency: payment.currency,
    network:  payment.network,
    memo:     payment.memo,
  });

  // 6. Retry with payment proof
  return await fetch(url, {
    ...options,
    headers: {
      ...options.headers,
      "X-Payment-Proof":   tx.hash,
      "X-Payment-Network": payment.network,
    },
  });
}

Budget Controls

Responsible agent design includes spending limits. Agents should enforce per-request caps, per-task budgets, and per-session maximums. The X402 flow makes this straightforward because every cost is declared upfront in the 402 response, before any money moves. The agent always knows the price before it pays.

Why This Matters

X402 turns the web into a marketplace that machines can navigate autonomously. An agent can chain ten different paid APIs in a single task -- translation, image generation, code review, data lookup -- without any of them sharing an account system. Each interaction is an independent, atomic HTTP exchange. This composability is what makes X402 a foundational protocol for the agentic web. Developers can also integrate with the x402 List API to programmatically discover services.