What is x402?
x402 is an HTTP-native payment protocol. A server returns HTTP 402 (Payment Required) with payment details in a header. The client pays, then retries the request with proof of payment. No API keys, no subscriptions: the agent pays per-request. This pattern is gaining traction for AI agent APIs where usage-based billing makes more sense than monthly plans. Your agent discovers the price at request time, pays through its wallet, and gets access.How the x402 flow works
The protocol follows five steps:- Agent requests a resource. Standard GET or POST to the API endpoint.
- Server returns HTTP 402. The response includes an
X-Payment-Requiredheader (orX-Payment-Info) containing a JSON object. - Header contains payment details. The JSON includes
amount,currency,paymentAddress,chainId, and optionallytokenAddress. - Agent pays through Mandate. The transfer goes through the same policy engine as any other transaction. Spend limits, allowlists, and approval rules all apply.
- Agent retries with proof. The original request is sent again with
Payment-SignatureandX-Payment-TxHashheaders set to the transaction hash.
SDK: wallet.x402Pay()
MandateWallet.x402Pay() handles the entire flow in one call. It probes the URL, parses the 402 header, executes the transfer through Mandate validation, and retries with the payment proof.
x402Pay() returns the original response unchanged. No payment is attempted. This makes it safe to wrap any request: pay-walled endpoints trigger payment, free endpoints pass through.
The
reason field defaults to "x402 payment for {url}" when not provided. A descriptive reason improves your audit log and helps the policy engine evaluate the transaction context.Manual flow
If you need more control or useMandateClient directly, handle each step yourself.
1. Probe the endpoint
2. Parse the payment header
3. Execute the transfer through Mandate
4. Retry with proof
Policy considerations
x402 payments go through the same policy engine as every other transaction. Your MANDATE.md rules and dashboard policies apply in full. A few things to plan for:- Spend limits: Set per-transaction limits high enough for the services your agent uses. A 1 per-tx limit is a common misconfiguration.
- Address allowlists: If you use an allowlist, add the payment addresses of services your agent calls. Otherwise x402 payments will be blocked with
address_not_allowed. - Approval thresholds: Decide whether x402 payments should require human approval above a certain amount. A low threshold means your agent pauses for approval on expensive API calls.
- Reason field: Always provide a descriptive reason. The default
"x402 payment for {url}"is acceptable, but specific reasons like"Market data API, hourly price feed"produce better audit logs.
Next Steps
MandateWallet Reference
Full API reference for x402Pay, transfer, sendEth, and sendTransaction.
Validate Transactions
Understand the validation flow that x402 payments go through.
Chain Reference
Supported chains, token addresses, and RPC endpoints.
Handle Errors
Catch PolicyBlockedError and other exceptions from x402 transfers.