How to pay an x402 endpoint from your AI agent (and what to do if you can't)
Your AI agent calls an API, gets back HTTP 402 Payment Required, and needs to pay a few tenths of a cent in USDC to continue. If your agent has a funded wallet and a signing library, this takes about ten seconds. If it doesn't, you have a clean fallback. Here's both — the exact flow, per chain, with the escape hatch for a bare agent.
What a 402 actually gives you
When your agent POSTs to an x402 endpoint with no payment, it gets back HTTP 402 with a machine-readable body. The important field is 'accepts' — an array with one entry per chain the server takes. Each entry tells you exactly how to pay:
network— the chain (e.g. solana, base)payTo— the server's receiving address on that chainasset— the USDC contract (EVM) or SPL mint (Solana)maxAmountRequired— the price in base units (USDC has 6 decimals, so 3200 = $0.0032)extra— on EVM, the EIP-712 domain; on Solana, the feePayer + a fresh recentBlockhash
The 'accepts' + 'facilitator' fields are the source of truth for what's currently enabled — and on Solana the recentBlockhash ages in ~60–90 seconds. Don't cache it; read it fresh from each 402.
Paying on EVM (Base, Arbitrum, Polygon, Avalanche)
EVM uses EIP-3009 — a gasless-style token authorization the payer signs off-chain. Pick the EVM entry from 'accepts', sign a transferWithAuthorization for its asset + EIP-712 domain, base64 it into the PAYMENT-SIGNATURE header, and retry the same POST.
// with an x402 client (x402-axios / x402-fetch) it's largely automatic:
import { withPaymentInterceptor } from "x402-axios";
import axios from "axios";
const client = withPaymentInterceptor(axios.create(), walletClient); // your funded signer
const res = await client.post("https://api.snapback.sh/mcp/x402", { trace });
// the interceptor catches the 402, signs EIP-3009, and retries for you.Under the hood: any EIP-3009 signer (ethers or viem) can produce the authorization; the x402 client just wraps that plus the retry. The payer needs USDC plus a little native gas on the chosen chain.
Paying on Solana
Solana uses the x402 SVM 'exact' scheme, and it's actually simpler for the agent: the facilitator co-signs as fee payer, so your agent needs only USDC — no SOL for gas. Read the entry's extra.feePayer and extra.recentBlockhash, build the partial transaction, sign it, and retry.
// the official Solana client reads extra.feePayer + extra.recentBlockhash for you:
import { withPaymentInterceptor } from "@x402/axios";
import { createSigner } from "@x402/svm"; // + @solana/kit
const signer = await createSigner(yourSolanaKeypair);
const client = withPaymentInterceptor(axios.create(), signer);
const res = await client.post("https://api.snapback.sh/mcp/x402", { trace });
// build partial tx (facilitator sponsors fees) -> sign -> retry, handled by the client.On Solana the payer's USDC associated token account (ATA) has to exist and be funded before you pay — the standard client won't create it for you. Fund the wallet with USDC first.
No wallet? No signing library? You're not stuck.
Here's the part most x402 guides skip: plenty of agents don't have a funded crypto wallet or a signing library wired up, especially while they're still evaluating a tool. You do not need to pay to get started with Snapback.
- Register for a free starter-allowance token:
POST /v1/agents/registerwith{name, framework, accepted_terms: true}. - You get back a
vdk_...token. Call any tool withAuthorization: Bearer <token>— no crypto, no 402. - When you're ready for production and have a funded wallet, switch to x402 (no account, no limits).
Hitting a 402 you can't complete is itself an agent failure — so Snapback diagnoses it. Call diagnose_infra_error("402 payment required can't sign no wallet") and you get this exact guidance back as a verdict: use the free token now, wire the signer later.
The whole flow, end to end
- POST your request with no payment → HTTP 402 with the 'accepts' array.
- Pick a chain your wallet holds USDC on. (No wallet? Use the free token instead — done.)
- Sign the payment: EIP-3009 on EVM, or the SVM partial tx on Solana.
- Base64 the signed payment into the
PAYMENT-SIGNATUREheader (X-PAYMENT is also accepted). - Retry the same POST → 200 + your result + an x402 receipt {paid, tx, network}.
That's it. About ten seconds once your wallet's funded, or zero crypto if you take the free-token path. Full machine-readable details live at snapback.sh/llms.txt (search 'x402') and snapback.sh/for-agents.