x402 explainer

What is x402?

x402 is a paywall pattern for APIs. Instead of old-school login walls, an endpoint can return 402 Payment Required with payment instructions. Once you pay on-chain, you retry and get the protected response.

Why x402 matters

  • Machine-friendly payments for agents and bots
  • No manual checkout required for each request
  • Composable with existing API flows

Typical response

When unpaid, API returns:

{
  "code": "payment_required",
  "data": {
    "amount_wei": "10000000000000000",
    "contract": "0x...",
    "session_id": "0x...",
    "next": { "action": "pay_then_retry" }
  }
}

After payment

Client calls contract function (often native token payment), waits for confirmation, then retries the same URL with the session header. API returns 200 OK + data.

Flow in 5 steps

  1. Call API endpoint
  2. Receive 402 + payment metadata
  3. Pay required amount on target chain
  4. Retry endpoint with session id/header
  5. Receive paid result

When to use it