x402 Protocol

How HTTP-native payments work for paid OptimAI Search requests.

x402 is an open standard for HTTP-native payments. A client requests a resource, the server asks for payment with HTTP 402 Payment Required, the client pays onchain, and the server delivers the resource.

The model does not require accounts, API keys, or long-lived sessions.

For OptimAI Search, the important idea is simple: the HTTP request itself can carry the payment proof. Your app asks for a search, receives a 402 payment challenge when payment is needed, signs the payment, and retries the same resource.

Core concepts

TermMeaning
ResourceAny HTTP or HTTPS endpoint, such as an API, webpage, or RPC method.
ClientThe entity requesting and paying for the resource.
Resource serverThe HTTP server gating the resource behind payment.
FacilitatorA server that verifies signatures and settles payments onchain.
SchemeLogical payment mechanism, such as exact for a fixed amount.
NetworkSpecific blockchain, such as eip155:8453 for Base mainnet.

Protocol flow

OptimAI paid search flow:

App request -> OptimAI Search API -> 402 payment challenge
             -> app signs payment -> paid retry
             -> decentralized search nodes -> cited answer
Client                    Resource Server              Facilitator
  |                              |                          |
  |---- GET /resource ---------->|                          |
  |                              |                          |
  |<--- 402 + PAYMENT-REQUIRED --|                          |
  |     base64 PaymentRequirements                         |
  |                              |                          |
  |  client picks scheme and network, then signs payload    |
  |                              |                          |
  |---- GET /resource ---------->|                          |
  |     PAYMENT-SIGNATURE: ...   |                          |
  |                              |---- POST /verify ------->|
  |                              |<--- verified ------------|
  |                              |                          |
  |                              |---- POST /settle ------->|
  |                              |                          |--> blockchain
  |                              |<--- Payment Execution ---|
  |                              |     Response             |
  |<--- 200 OK ------------------|                          |
  |     PAYMENT-RESPONSE: ...    |                          |

HTTP headers

Server to client on the 402 response:

PAYMENT-REQUIRED: <base64-encoded PaymentRequirements JSON>

Client to server on the paid retry:

PAYMENT-SIGNATURE: <base64-encoded PaymentPayload JSON>

Server to client on the successful response:

PAYMENT-RESPONSE: <base64-encoded Settlement Response JSON>

Payment requirements

The server sends a payment challenge shaped like:

type PaymentRequirements = {
  scheme: string
  network: string
  maxAmountRequired: string
  resource: string
  description: string
  mimeType: string
  payTo: string
  maxTimeoutSeconds: number
  asset: string
  extra?: object
}

Payment payload

The client retries with a signed payment payload:

type PaymentPayload = {
  scheme: string
  network: string
  payload: object
}

Schemes

exact is the common scheme for a fixed predetermined amount.

It can use:

  • EIP-3009 transferWithAuthorization for USDC and EURC, which enables gasless payments
  • Permit2 for generic ERC-20 tokens, which may require a one-time approval

Supported networks

EVM support is available through @x402/evm.

NetworkCAIP-2MainnetTestnet
Baseeip155:8453YesBase Sepolia
Polygoneip155:137YesNot listed
Arbitrum Oneeip155:42161YesNot listed
Worldeip155:480YesWorld Sepolia
Ethereumeip155:1CustomNot listed

Solana support is available through @x402/svm.

NetworkCAIP-2
Mainnetsolana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp
Devnetsolana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1

Facilitators

Payment verification and settlement are delegated to a facilitator. A facilitator validates the payment payload and submits settlement onchain.

Common options:

FacilitatorNotes
CDP FacilitatorCoinbase-hosted facilitator for Base, Polygon, Arbitrum, World, and Solana.
x402.org Testnet FacilitatorFree testnet facilitator for Base Sepolia and Solana Devnet.

The facilitator interface is small:

POST /verify
POST /settle

SDKs

TypeScript:

npm install @x402/core @x402/evm @x402/svm @x402/fetch

Python:

pip install x402

Go:

go get github.com/x402-foundation/x402/go

For OptimAI-specific search flows, use the @optimai-network/x402-sdk guide.