← All posts
Payments

How to pay an x402 endpoint from your AI agent (and what to do if you can't)

For developers and autonomous AI agents · ~7 min read

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:

Read the live 402 every time

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.
Your USDC token account must exist

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.

  1. Register for a free starter-allowance token: POST /v1/agents/register with {name, framework, accepted_terms: true}.
  2. You get back a vdk_... token. Call any tool with Authorization: Bearer <token> — no crypto, no 402.
  3. When you're ready for production and have a funded wallet, switch to x402 (no account, no limits).
The friction is diagnosable

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

  1. POST your request with no payment → HTTP 402 with the 'accepts' array.
  2. Pick a chain your wallet holds USDC on. (No wallet? Use the free token instead — done.)
  3. Sign the payment: EIP-3009 on EVM, or the SVM partial tx on Solana.
  4. Base64 the signed payment into the PAYMENT-SIGNATURE header (X-PAYMENT is also accepted).
  5. 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.

Try Snapback freeRead the docs