Skip to main content

← x402

This walkthrough shows how to purchase a product through Rye’s x402 endpoint using the AgentCash client. AgentCash handles the 402 → sign → retry loop transparently, so your code reads like a normal HTTP call.

Prerequisites

  • An AgentCash wallet with a small USDC balance on Base (≥ $1 is plenty for testing — covers the access fees and a low-priced test purchase)
  • A product URL from a supported merchant (Shopify storefronts, etc.)
  • The AgentCash CLI or SDK — follow the AgentCash setup guide to install and initialize a wallet
The first time you initialize AgentCash it generates a wallet and stores keys at ~/.agentcash/wallet.json (EVM) and ~/.agentcash/solana-wallet.json (Solana). Top up the wallet with USDC on the network you plan to use before making any paid call.

End-to-end purchase

The example below uses @agentcash/fetch — the programmatic AgentCash SDK. Its executeFetch function handles the 402 → sign → retry loop transparently, so your code reads like a normal HTTP call.
Prefer the CLI or an MCP-installed agent? AgentCash also ships npx agentcash fetch <url> and an MCP server (claude mcp add agentcash --scope user -- npx -y agentcash@latest). Both speak the same canonical x402 v2 against this endpoint.

What happens behind the scenes

  • executeFetch receives a 402 Payment Required from the proxy with a PAYMENT-REQUIRED header. It signs an EIP-3009 TransferWithAuthorization (off-chain only — the buyer never broadcasts) and retries the same request with a PAYMENT-SIGNATURE header.
  • The proxy verifies the signature, broadcasts transferWithAuthorization on Base, and pays the gas. On the success response it returns an X-PAYMENT-RESPONSE header carrying the on-chain transaction hash, which executeFetch exposes as paymentInfo.payment.transactionHash.
  • For step 1 the authorization is for 0.02.Forstep4itcoverstheofferspurchasetotalplusthe0.02. For step 4 it covers the offer's purchase total plus the 0.03 API fee — bundled into a single signed authorization.
  • GET calls in steps 2 and 5 are free and use a normal fetch. The X-Wallet-Address header scopes the read to the wallet that paid for the intent.
  • After step 4 returns, Rye places the order asynchronously. The intent moves to placing_order, then to completed or failed. See Checkout Intent Lifecycle for the full state machine.

Timing

Order placement runs asynchronously — your code does not block while Rye places the order at the merchant. Keep polling GET /v1/checkout-intents?id=… until you see state: "completed" or state: "failed".

Failure modes

  • Insufficient wallet balance — the on-chain transfer fails; the intent moves to failed and no order is placed.
  • Offer retrieval fails (out of stock, unsupported product) — state: "failed" after step 2; the $0.02 access fee is not refunded because the offer retrieval work was performed.
  • Order placement fails at the merchant — state: "failed" after step 4; the purchase amount is automatically refunded to the signing wallet on-chain.
  • Signature expired between calls — the retry returns 400 with a fresh PAYMENT-REQUIRED; executeFetch re-signs and retries automatically.
See the Endpoint Reference for full request and response shapes, including how to call the API without the AgentCash SDK.