Start as the client. The API below sells tomorrow’s forecast for 0.001 USDC per
request. The client is a script with its own wallet of test USDC, so it pays
without a person approving each call. Press Run to step through this
tutorial’s code on a local test network (Surfpool). Nothing is sent from your
browser, and no real money moves.
Paid forecast API · 0.001 USDC per requestLocal test network (Surfpool)
RequestGET /api/forecastGET /api/forecastGET /api/forecast + X-PAYMENT
ResponseWaiting for a request402 Payment Required · 0.001 USDC200 OK · Sunny, 24°C
- Call the API without paying The server answers 402 Payment Required with the price (1000 base units, 0.001 USDC) and the address to pay.
- Pay and call again The client signs a 0.001 USDC payment and sends the same request again with it in the
X-PAYMENTheader. The server settles the payment onchain and answers with the forecast.
# Step 1/2 · Call the API without paying
await fetch(url)
GET /api/forecast -> 402 X-PAYMENT header is required
price: 1000 base units of 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU (0.001 USDC)
payTo: 4SnM7PENj3Amhyc6rRA5XG2rq3NpiyZbPvpgAQbA3FLE network: solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1
402 Payment Required
# Step 2/2 · Pay and call again
const header = await createPaymentHeader(required, { signer: client, maxAmount: 5_000n })
await fetch(url, { headers: { "X-PAYMENT": header } })GET /api/forecast (paid) -> 200
{"city":"Lisbon","tomorrow":"Sunny, 24°C"}settled signature: 4JZW7fB3h53qP6f66eat4cwDVDFvxwTtKWbV8tfJJHVo4eHzK9riSvetQSwPEtungFxfbWC968JoyDoqRo7XauLZ
fee: 5000 lamports
200 OK · paid 0.001 USDC
Paid 0.001 USDC and got the forecast. No signup, no API key.
- The first call got
402 Payment Required. The body listed the price as1000base units of USDC (USDC has 6 decimals, so that is 0.001 USDC), the USDC mint address, the address to pay (payTo), and the network. - The client signed a USDC transfer for exactly that amount and sent the same
request again with the signed transfer in the
X-PAYMENTheader. - The server sent the transfer to the network, waited for it to confirm, and
answered
200with the forecast. The network fee for that transactionTransactionsA transaction is a signed request to change the shared ledger. It either fully happens or doesn't happen at all, and once confirmed anyone can look it up.Learn more (Transactions: A transaction is a signed request to change the shared ledger. It either fully happens or doesn’t happen at all, and once confirmed anyone can look it up.) was 5,000 lamports (0.000005 SOL).
network
field still shows the devnet ID (solana:EtWT…xqa1), the default NETWORK in
x402.ts.
Requests that don’t get the forecast
The server and the client both refuse payments that don’t fit. The first request resends theX-PAYMENT header from the paid request above. The second
comes from a client capped below the price.
Requests that don’t get the forecastLocal test network (Surfpool)
- Send the same payment again The same
X-PAYMENTheader a second time gets 402: one payment buys one response. - Call with a cap below the price A client that pays at most 500 base units (0.0005 USDC) sees the 0.001 USDC price and signs nothing.
# Step 1/2 · Send the same payment again
await fetch(url, { headers: { "X-PAYMENT": header } })GET /api/forecast (replayed X-PAYMENT) -> 402 payment already used
Refused: payment already used · 402
# Step 2/2 · Call with a cap below the price
await fetchWithPayment(url, { signer: client, maxAmount: 500n })
client refused: price 1000 is above maxAmount 500; not paying
Refused by the client: price above maxAmount