How an AI Agent Pays for an API with x402, Step by Step
An agent can't fill in a checkout form. x402 gives it another way: the server names a price in an HTTP response, the agent signs a payment, and the agent sends the same request again with the signature attached. No account, no card form, no person.
HTTP has had a status code for this for decades. RFC 9110 still lists 402 Payment Required as "reserved for future use". x402 is one of the protocols now filling that gap.
This guide walks through x402 version 2 using a live endpoint: our own credit pack at POST https://pageverified.com/api/v1/credits/buy?pack=19. Asking for the price costs nothing. Only a signed payment moves money.
The three headers
The x402 HTTP guide defines the v2 exchange with three headers. Each one holds Base64-encoded JSON.
PAYMENT-REQUIREDgoes from server to client and holds the terms.PAYMENT-SIGNATUREgoes from client to server and holds the signed payment.PAYMENT-RESPONSEgoes from server to client and holds the settlement result, whether it worked or not.
Step 1: Ask, and get a 402
Send the request with no payment.
curl -si -X POST "https://pageverified.com/api/v1/credits/buy?pack=19"
The answer is 402 Payment Required with a payment-required header. Our endpoint also sends a WWW-Authenticate: Payment header, because it accepts a second protocol. A client picks the one it can pay with.
Step 2: Decode the terms
Decode the header. This is what our endpoint returned when we wrote this guide, with the discovery metadata removed:
{
"x402Version": 2,
"error": "Payment required",
"resource": {
"url": "https://pageverified.com/api/v1/credits/buy?pack=19",
"mimeType": "application/json"
},
"accepts": [
{
"scheme": "exact",
"network": "eip155:8453",
"amount": "19000000",
"asset": "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
"payTo": "0x5c38Aaf4D9788D3891a0B291d627510902AF5E14",
"maxTimeoutSeconds": 300,
"extra": { "name": "USD Coin", "version": "2", "pageverified_quote": "..." }
}
]
}
These names come from section 5.1 of the x402 v2 specification. Three of them trip people up:
networkis a CAIP-2 chain ID, a namespace and a reference joined by a colon.eip155:8453is Base.assetis a token contract address. Check it against the issuer's own list. Circle publishes0x8335...2913as USDC on Base mainnet.amountis in atomic units, and USDC has 6 decimals. So19000000is 19 USDC.
Step 3: Check before signing
This is the step that belongs to you. A 402 is a request from a stranger, so an agent should not sign whatever it receives. Before signing, check that:
- the network and asset are ones you hold and expect
- the amount is inside this task's budget
- the
payToaddress belongs to the seller you meant to pay - the seller's name for the resource matches what you asked for
The official client helps here. According to the buyer quickstart, by default it pays only recognised USD-pegged assets and caps each payment at $1. Those controls run before the payment is signed. Our $19 pack is over that cap, so the client will refuse it until you raise the limit on purpose. That is the behaviour you want.
Step 4: Sign an authorization
For the exact scheme on EVM chains, the agent signs an EIP-3009 transferWithAuthorization message. EIP-3009 is still marked Draft, but it is what the x402 specification uses for this payload. The signed fields are:
fromandto: the payer and thepayToaddressvalue: the amount in atomic unitsvalidAfterandvalidBefore: the time windownonce: 32 random bytes
The nonce is the replay protection. The token contract records each nonce as used and rejects a second attempt. The agent signs and does not broadcast anything, so it needs no gas of its own for this step.
Step 5: Send the request again
The client repeats the request with a PAYMENT-SIGNATURE header. Per section 5.2 of the spec, its JSON holds x402Version, accepted and payload. accepted is the entry the client chose from accepts, sent back unchanged. payload holds the signature and the authorization fields from step 4.
Step 6: The server settles through a facilitator
A resource server usually doesn't touch the chain. It hands the payload to a facilitator. The spec defines two calls: POST /verify, a read-only check, and POST /settle, which commits the payment, typically by broadcasting a transaction. For the exact scheme, money moves in this round trip.
Step 7: Read the result
On success our endpoint answers 201 with a body holding api_key, credits_added and credits_remaining, and a PAYMENT-RESPONSE header carrying the settlement details. Our OpenAPI document describes one more case. A 202 means the payment is still pending, so resend the identical request after Retry-After seconds and do not start a new payment.
Keep the response. It is the only copy of the key, which starts with pv_cap_. The optional PageVerified-Attempt and PageVerified-Recovery headers let a client recover the key if the response gets lost.
The same flow in Python
This follows the imports and options in the buyer quickstart, with the cap raised for one $19 purchase. It moves real money when run.
import asyncio
import os
from eth_account import Account
from x402 import SchemeRegistration, x402Client, x402ClientConfig
from x402.http.clients import x402HttpxClient
from x402.mechanisms.evm import EthAccountSigner
from x402.mechanisms.evm.exact import ExactEvmScheme
BUY_URL = "https://pageverified.com/api/v1/credits/buy?pack=19"
async def main() -> None:
account = Account.from_key(os.environ["EVM_PRIVATE_KEY"])
client = x402Client.from_config(
x402ClientConfig(
schemes=[
SchemeRegistration(
network="eip155:*",
client=ExactEvmScheme(EthAccountSigner(account)),
),
],
spend_controls={"max_amount_per_payment": "$19"},
)
)
async with x402HttpxClient(client) as http:
response = await http.post(BUY_URL)
print(response.status_code, response.json())
asyncio.run(main())
To test without real money, Stripe's x402 guide points to the Base Sepolia testnet (eip155:84532) and the public test facilitator at https://x402.org/facilitator.
What x402 does not tell you
x402 proves a payment was authorized and settled. It doesn't tell you the seller is who they say they are, or that the thing you bought was any good. It also doesn't keep a record of what the page or API said when the agent decided to pay. Those are separate jobs, and an agent spending real money needs an answer for each.
