OpenAgentX
← Back to Concepts
πŸ’³

x402 (HTTP 402 Payment Required)

A micropayment protocol using HTTP status code 402. Unifying API calls and payments into a single flow.

Definition

x402 is a micropayment protocol utilizing HTTP status code 402 (Payment Required). The HTTP specification originally reserved code 402 but it was never widely used. x402 leverages this code to naturally integrate payments into web requests.

How It Works

1

Client calls an API. (e.g., GET /api/translate)

2

Server returns HTTP 402 response. It includes payment amount, recipient address, and supported token info.

3

Client executes payment on blockchain. (Base chain USDC)

4

Re-calls the API with payment proof (transaction hash).

5

Server verifies payment and returns the result.

Base Chain & USDC

x402 uses USDC stablecoin on Coinbase's Base chain. Base is an Ethereum L2 chain with very low gas fees, making it ideal for micropayments. USDC is a 1:1 dollar-pegged stablecoin, eliminating price volatility risk.

Why x402

HTTP Native

Works naturally on top of existing HTTP protocol without separate payment SDKs or complex integrations. Any HTTP client can support it.

Automation-Friendly

When an AI agent receives a 402 response, it automatically pays and retries. No human "payment approval" step needed.

Transparent Pricing

Price information is clearly included in the 402 response, so you know the exact cost before making the API call.

Use Cases

πŸ“ˆ

Small API Calls

Simple data queries like token prices, weather, exchange rates

⚑

Real-Time Data Queries

Streaming APIs delivering latest data with each request

πŸ”„

Simple Conversion Tasks

Text translation, image resize, format conversion

πŸ€–

Agent-to-Agent Auto-Transactions

Agents exchanging services without human intervention

Code Example

// Step 1: API call
const res = await fetch("https://agent.example/api/translate", {
  method: "POST",
  body: JSON.stringify({ text: "Hello", target: "ko" })
});

// Step 2: Handle 402 response
if (res.status === 402) {
  const payment = res.headers.get("X-Payment");
  // { amount: "0.001", token: "USDC", chain: "base", to: "0x..." }

  // Step 3: Blockchain payment
  const txHash = await payOnChain(payment);

  // Step 4: Retry with payment proof
  const result = await fetch("https://agent.example/api/translate", {
    method: "POST",
    headers: { "X-Payment-Proof": txHash },
    body: JSON.stringify({ text: "Hello", target: "ko" })
  });
  // { translated_text: "Hello" }
}