Building Agentic Commerce #4: x402 Stablecoin Payments — When Agents Pay in USDC
How AI agents make on-chain USDC payments using the x402 protocol — multi-chain settlement, EIP-712 signatures, and production-ready stablecoin checkout.
Executive summary
Part 4 of 'Building Agentic Commerce' explains the x402 stablecoin payment protocol — how AI agents discover x402-enabled merchants, sign EIP-712 authorizations, settle on-chain via facilitators, and handle retries with exponential backoff. Covers 5 supported chains, USDC amount conversion, SSRF protection, and 197 tests.
Published
2026-04-06
14 min
Author
Trusteed Engineering
Core Protocol Team
Category
developer-guide
In the first three parts of this series, we covered <a href='/en/blog/building-agentic-commerce-multi-protocol-checkout'>multi-protocol checkout</a>, <a href='/en/blog/building-agentic-commerce-agent-discovery-nlweb'>agent discovery via NLWeb</a>, and <a href='/en/blog/building-agentic-commerce-trust-scores'>trust scores</a>. Now we tackle the protocol that makes agents truly autonomous: <strong>x402 — stablecoin payments where agents pay directly on-chain in USDC</strong>.
Why Stablecoin Payments Matter for Agents
Traditional payment protocols (card networks, PayPal) require human identity — a cardholder, a PayPal account, a billing address. AI agents don't have these. The x402 protocol solves this by using wallet-based authentication: an agent proves it can pay by signing a cryptographic authorization, and settlement happens on-chain without any human identity requirement.
The HTTP 402 status code ("Payment Required") was reserved in the original HTTP specification but never standardized. The x402 protocol finally gives it a concrete implementation: when a server returns HTTP 402, it includes a machine-readable payment requirement that an agent can fulfill autonomously.
How x402 Works: The Complete Flow
Step 1: Agent Requests a Resource
Step 2: Server Returns HTTP 402
The amount <code>5000000</code> represents 5 USDC. USDC uses 6 decimal places, so 1 USDC = 1,000,000 smallest units. The conversion: USD cents × 10,000 = USDC smallest units.
Step 3: Agent Signs and Settles
Step 4: Settlement with Exponential Backoff
Supported Chains and Tokens
Trusteed supports x402 payments across 5 blockchain networks, all using USDC as the settlement token:
- 1<strong>Base</strong> — USDC at <code>0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913</code> (recommended: lowest fees)
- 2<strong>Ethereum</strong> — USDC at <code>0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48</code>
- 3<strong>Polygon</strong> — USDC at <code>0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359</code>
- 4<strong>Arbitrum</strong> — USDC at <code>0xaf88d065e77c8cC2239327C5EDb3A432268e5831</code>
- 5<strong>Solana</strong> — USDC at <code>EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v</code>
Merchants can override the default USDC address with a custom <code>tokenAddress</code> in their protocol configuration. A <code>maxTransactionAmount</code> safety cap is also supported.
Amount Conversion: USDC ↔ USD Cents
Safety checks prevent sub-cent precision errors (values must be divisible by 10,000), reject negative amounts, and enforce IEEE-754 safe integer bounds (~$9 billion max).
Protocol Detection: How x402 Is Identified
x402 is one of 8 payment protocols in the Trusteed detection system. The multi-protocol detector assigns confidence scores to determine which adapter handles each request:
- 1<code>X-Protocol: X402</code> header → confidence <strong>1.0</strong> (explicit declaration)
- 2<code>PAYMENT-SIGNATURE</code> header (V2) → confidence <strong>0.95</strong>
- 3<code>X-PAYMENT</code> header (V1 legacy) → confidence <strong>0.90</strong>
- 4Body shape match (<code>paymentPayload</code>, <code>x402</code>, or <code>paymentRequired</code> fields) → confidence <strong>0.85</strong>
- 5No match → falls back to ACP at confidence 0.5
This means x402 coexists with ACP, UCP, PayPal, Visa VIC, Mastercard Agent Pay, and other protocols. The highest-confidence adapter wins, and the protocol bridge routes the payment to the correct settlement path.
Merchant Configuration
Security Model
- 1<strong>No sensitive data in audit logs</strong> — wallet addresses, signatures, and nonces are never stored in OrderEvent records. Only protocol name, network, and transaction hash references are logged.
- 2<strong>Facilitator-delegated verification</strong> — the bridge never validates EIP-712 signatures locally (no key material on the server). Verification is delegated to the facilitator's <code>/verify</code> endpoint.
- 3<strong>Atomic state transitions</strong> — order status updates use database-level atomicity. The expiration job double-checks status before updating to prevent race conditions.
- 4<strong>SSRF protection</strong> — merchant-provided facilitator URLs are validated against private IP ranges and internal hostnames.
- 5<strong>15-second fetch timeout</strong> — all facilitator requests have a hard timeout to prevent hanging connections.
Order Status State Machine
V1 vs V2 Header Format
Production Tips
- 1<strong>Choose Base for lowest fees</strong> — L2 transaction costs are a fraction of Ethereum mainnet. Most agent-to-merchant payments are small (<$100), making Base the optimal default.
- 2<strong>Set maxTransactionAmount</strong> — a per-merchant safety cap prevents accidental large settlements. Start conservative (e.g., 100 USDC) and increase as trust builds.
- 3<strong>Monitor the expiration job</strong> — orders stuck in <code>x402_pending_payment</code> for >30 minutes indicate facilitator issues or network congestion. Set up alerts on the <code>x402_payment_expired</code> OrderEvent.
- 4<strong>Use V2 headers exclusively</strong> — V1 lacks replay protection. Disable V1 support in production if your agent ecosystem supports V2.
- 5<strong>Handle 402 responses gracefully</strong> — when building agent clients, parse the <code>PAYMENT-REQUIRED</code> header, extract the facilitator URLs, and implement the sign-and-settle flow. The facilitator handles all on-chain complexity.
Test Coverage
The x402 implementation is backed by 197 test cases across 6 test files (~2,554 lines of test code). Coverage includes: detection (8 tests), normalization (12 tests), settlement with backoff (45+ tests), configuration validation with SSRF (18 tests), end-to-end integration (22 tests), and 92+ edge cases covering precision errors, timeouts, network failures, and concurrent state updates.
What's Next
Frequently asked questions
What is x402 and how does it relate to HTTP 402?
x402 is a payment protocol that implements the long-reserved HTTP 402 (Payment Required) status code. When a server returns 402, it includes a machine-readable PaymentRequired object that AI agents can fulfill autonomously using USDC stablecoin payments on-chain.
Which blockchains does x402 support?
Trusteed supports x402 on 5 networks: Base (recommended for lowest fees), Ethereum, Polygon, Arbitrum, and Solana. All use USDC as the settlement token with well-known contract addresses per chain.
How does x402 handle failed settlements?
The settlement service uses exponential backoff with up to 5 retry attempts (delays of 5s, 10s, 20s, 40s). Each facilitator request has a 15-second timeout. If all retries fail, the order is marked x402_failed. A background job expires stuck orders after 30 minutes.
Is x402 secure? How are signatures verified?
Yes. x402 V2 uses EIP-712 typed data signatures with replay protection (nonce) and time bounds. Signature verification is delegated to the facilitator — no private key material exists on the server. Wallet addresses and signatures are never stored in audit logs.
Can x402 work alongside other payment protocols?
Absolutely. x402 is one of 8 payment protocols in the Trusteed detection system. The multi-protocol detector assigns confidence scores, and the highest-confidence adapter wins. If a merchant doesn't have x402 enabled, the bridge falls back to their preferred protocol (ACP, PayPal, etc.).
Sources and references
- x402 Protocol Specification
x402 Project (Coinbase CDP)
- EIP-712: Typed structured data hashing and signing
Ethereum Foundation
- USDC Documentation — Circle
Circle
- HTTP 402 Payment Required — MDN Web Docs
Mozilla
Related articles
developer-guide
Building Agentic Commerce #1: Multi-Protocol Checkout — MCP + x402 + ACP in One Flow
One agent, three protocols, one checkout. Here's how MCP, x402 stablecoin payments, and ACP work together to let AI agents buy products — with code examples you can run today.
developer-guide
Building Agentic Commerce #3: Trust Scores — How Agents Decide Who to Buy From
When an AI agent evaluates merchants, it doesn't read reviews or recognize logos. It reads trust scores — 12 machine-verifiable signals that determine search ranking, checkout eligibility, and payment friction. Here's how the system works.
developer-guide
Building Agentic Commerce #2: How AI Agents Discover Your Store Without an API Key
Before an agent can buy anything, it needs to find your store. Here's the 6-phase discovery chain that takes an AI agent from zero knowledge to checkout-ready in under 2 seconds — no pre-configuration required.