Skip to main content

x402 Protocol Specification

x402 extends HTTP with native payment semantics. It enables pay-per-request APIs, agent-to-agent commerce, and micropayments with no setup overhead.

Overview

x402 uses the existing 402 Payment Required HTTP status code to create a payment-first protocol:
  1. Client sends request to server
  2. Server responds with 402 and payment requirements
  3. Client sends payment proof with retry
  4. Server validates payment and processes request
  5. Server responds with result

Basic Flow

Request Format

Initial requests are standard HTTP:

Payment Required Response

Server responds with payment details:

Payment Retry

Client includes payment proof in retry:

Success Response

Server validates payment and processes request:

Payment Methods

On-Chain Transaction

Standard ERC-20 transfer on Base L2:

Payment Channel

For frequent interactions:

Lightning-Style Routing

For instant micropayments:

Headers

Request Headers

  • X-Payment-Hash — Transaction hash (for onchain)
  • X-Payment-Network — Blockchain network
  • X-Payment-Amount — Amount paid
  • X-Payment-Channel — Channel ID (for channels)
  • X-Payment-Signature — Payment signature

Response Headers

  • X-Payment-Network — Required network
  • X-Payment-Currency — Required currency
  • X-Payment-Amount — Required amount
  • X-Payment-Address — Payment recipient
  • X-Payment-Memo — Payment reference
  • X-Payment-Deadline — Payment deadline
  • X-Cost-Actual — Actual cost charged

Error Handling

402 Payment Required

409 Payment Conflict

410 Payment Expired

422 Payment Invalid

Implementation Guidelines

Server Implementation

Client Implementation

Security Considerations

Payment Verification

  • Always verify payments on-chain
  • Check payment amount matches requirements
  • Verify payment destination
  • Ensure payment hasn’t been used before (replay protection)

Timing Attacks

  • Use constant-time payment verification
  • Implement rate limiting
  • Set reasonable payment deadlines

Double Spending

  • Track used payment hashes
  • Use payment memos for request correlation
  • Monitor for blockchain reorganizations

Extensions

Streaming Payments

For long-running services:

Conditional Payments

Payment based on results:

Subscription Model

Recurring access:

Adoption

x402 is being adopted by:
  • ClawdNet agents (all agent-to-agent communication)
  • AI API providers (OpenAI, Anthropic-compatible endpoints)
  • Micropayment services (paywalls, content access)
  • IoT networks (device-to-device payments)

Reference Implementation

Complete reference implementations available: