Skip to main content

What is x402?

x402 is a machine-to-machine payment protocol built around HTTP 402 Payment Required. It enables AI agents to automatically pay for API access without human intervention. AgentWallex implements x402 v2 with EIP-3009 authorization payloads.

x402 v2 Headers

The protocol uses three HTTP headers:
HeaderDirectionPurpose
PAYMENT-REQUIREDServer to Client402 challenge with pricing info
PAYMENT-SIGNATUREClient to ServerSigned payment payload
PAYMENT-RESPONSEServer to ClientSettlement confirmation

Payment Flow

Client Agent               Paid API               AgentWallex
    |                        |                         |
    |-- GET /resource ------>|                         |
    |<-- 402 + PAYMENT-REQUIRED                        |
    |                        |                         |
    |-- POST /api/v1/x402/pay -----------------------> |
    |                        |     (sign + policy)     |
    |<-- payment_info        |                         |
    |                        |                         |
    |-- GET /resource + PAYMENT-SIGNATURE -----------> |
    |<-- 200 + PAYMENT-RESPONSE                        |
1

Agent requests a resource

The agent sends a standard HTTP request to a paid API endpoint.
2

Server returns 402

The API responds with HTTP 402 and a PAYMENT-REQUIRED header containing pricing details (amount, token, chain, payTo address).
3

Agent pays via AgentWallex

The agent sends the payment details to POST /x402/pay. AgentWallex evaluates policies and signs the payment.
4

Agent retries with payment proof

The agent retries the original request with the PAYMENT-SIGNATURE header attached.
5

Server verifies and responds

The API verifies the payment signature (via the AgentWallex facilitator) and returns the resource.

Using x402 APIs

Check if a URL Supports x402

curl -X POST https://api.agentwallex.com/api/v1/x402/check \
  -H "X-API-Key: awx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://paid-api.example.com/v1/data"}'

Create a Session Budget

Sessions let you pre-authorize a spending budget for repeated API calls:
curl -X POST https://api.agentwallex.com/api/v1/x402/sessions \
  -H "X-API-Key: awx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "agent_uuid",
    "budget_limit": "100.00",
    "chain": "eip155:84532",
    "ttl_seconds": 3600,
    "allowed_urls": ["https://paid-api.example.com/v1/data"]
  }'

Trigger Payment Negotiation

curl -X POST https://api.agentwallex.com/api/v1/x402/pay \
  -H "X-API-Key: awx_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "agent_id": "agent_uuid",
    "target_url": "https://paid-api.example.com/v1/data",
    "session_id": "optional_session_uuid",
    "chain": "eip155:84532"
  }'
The response includes payment_info with:
FieldDescription
ledger_idInternal ledger entry ID
amountPayment amount
fee_amountPlatform fee deducted
fee_rateFee percentage applied
tokenToken used (e.g., USDC)
chainChain used for settlement
statusPayment status

SDK Integration

Automatic HTTP Interceptor

The TypeScript SDK provides an interceptor that handles the full x402 flow automatically:
const fetchWithPayment = aw.x402.httpInterceptor({
  agentId: "agent_abc123",
  chain: "eip155:84532",
});

// This automatically handles 402 challenges
const response = await fetchWithPayment("https://paid-api.example.com/v1/data");
const data = await response.json();

Manual Flow

// 1. Create a session budget
const session = await aw.x402.createSession({
  agentId: "agent_abc123",
  budgetLimit: "100.00",
  chain: "eip155:84532",
  ttlSeconds: 3600,
  allowedUrls: ["https://paid-api.example.com/v1/data"],
});

// 2. Pay for an API call
const result = await aw.x402.pay({
  agentId: "agent_abc123",
  targetUrl: "https://paid-api.example.com/v1/data",
  sessionId: session.id,
  chain: "eip155:84532",
});

// 3. Or pay using the session directly
await aw.x402.sessionPay(session.id, {
  targetUrl: "https://paid-api.example.com/v1/data",
});

For Service Providers

If you expose paid APIs, your server should return an x402 v2 challenge when payment is missing.

Return 402 with PAYMENT-REQUIRED

Encode a JSON challenge (base64) into the PAYMENT-REQUIRED header:
{
  "x402Version": 2,
  "resource": "https://your-api.com/v1/data",
  "accepts": [
    {
      "scheme": "exact",
      "network": "eip155:8453",
      "amount": "0.10",
      "asset": "USDC",
      "payTo": "0xYourAddress",
      "maxTimeoutSeconds": 300
    }
  ]
}

Verify and Settle

Use the AgentWallex facilitator endpoints:
# Verify a payment signature
POST /api/v1/x402/facilitator/verify

# Settle a verified payment
POST /api/v1/x402/facilitator/settle

# Query supported chains
GET /api/v1/x402/facilitator/supported

Operational Defaults

ParameterValue
Settlement interval300 seconds
Settlement threshold$10.00
Max settlement delay3,600 seconds
Supported chainseip155:84532, eip155:8453, eip155:1, eip155:137