Free Resource

x402 Buyer Recipe

Pay an AI agent in USDC. The 5-minute recipe.

NETWORKBASE SEPOLIA COSTFREE (TESTNET) STACKPYTHON 3 + USDC TIME~5 MIN

// The Recipe

01

Set up a Coinbase Smart Wallet

x402 is a payment protocol that lets a script pay an HTTP endpoint in USDC. You need a wallet that can sign payment authorizations. Coinbase Smart Wallet is the easy path — passkey-based, no seed phrase, runs in the browser.

  • Open wallet.coinbase.com in a Chromium browser.
  • Click Create wallet → pick Smart Wallet → authorize with passkey (Face ID, Touch ID, or device PIN).
  • Copy the wallet address. It works on every EVM network — same address for Base Sepolia and Base mainnet.

Why it matters: a Smart Wallet signs the EIP-3009 authorization x402 needs without exposing a private key. Passkey-gated. No browser extension to install.

02

Get test ETH and test USDC

Base Sepolia is the testnet — fake money, real protocol. Two faucets, two minutes.

  • Sepolia ETH for gas: faucet.coinbase.com → pick Base Sepolia → paste your wallet address. Verify URL
  • Sepolia USDC to spend: faucet.circle.com → pick Base Sepolia → paste your address → receive 10 USDC. Verify URL

Note: with a hosted facilitator, the facilitator pays the gas. Buyer ETH is only needed if you self-host. Grab some anyway — it costs nothing and unblocks edge cases.

03

Verify on BaseScan

Confirm both balances landed before you spend a line of code.

  • Open https://sepolia.basescan.org/address/<YOUR_ADDR>
  • You should see your ETH balance up top and a USDC entry under the token list with a non-zero balance.
  • If USDC shows zero after 60 seconds, the faucet drip hasn’t indexed yet — refresh.
04

Install the x402 Python client

One pip install. The SDK ships an httpx-compatible client that handles the 402 retry, the EIP-3009 signing, and the PAYMENT-SIGNATURE header for you.

pip install x402

You also need eth-account for key handling — it ships as a dependency, no separate install.

05

The 5-line buyer script

Replace <BUYER_PRIVATE_KEY> with a hex private key for a wallet you control. Replace the URL with any 402-protected endpoint.

from x402.clients.httpx import x402HttpxClient
from eth_account import Account

account = Account.from_key("0x<BUYER_PRIVATE_KEY>")
client  = x402HttpxClient(account=account)
r       = client.get("https://<HOST>/<PAID_ENDPOINT>")

print(r.status_code, r.json())

What the SDK does on your behalf: hits the URL, gets a 402 Payment Required, reads the PaymentRequirements, signs an EIP-3009 transfer authorization with your key, retries the same URL with a PAYMENT-SIGNATURE header, returns the 200 response.

06

Confirm settlement

Read the PAYMENT-RESPONSE header on the successful response — it contains the on-chain transaction hash.

# after the request succeeds:
tx_hash = r.headers["PAYMENT-RESPONSE"]
print(f"https://sepolia.basescan.org/tx/{tx_hash}")

Open that URL. You should see USDC moved from your wallet to the seller’s address. That’s the receipt.

// Common failure modes

Network mismatch
Endpoint is on Base mainnet, your wallet is funded on Base Sepolia (or vice versa). Fix: match the network — check the network field in the 402 response body.
Stale authorization (validBefore expired)
EIP-3009 auths carry a validBefore timestamp. If your script signs and stalls, the window closes. Fix: sign and submit in the same call — don’t cache signatures.
USDC balance < price
Faucet only dripped a fraction, or you spent it on a previous test. Fix: top up via the Circle faucet again, or check the price in the maxAmountRequired field.
Wrong USDC contract address
Sepolia USDC and mainnet USDC are different ERC-20 contracts. The SDK derives this from network + asset, so the bug is almost always a hand-rolled config. Fix: let the SDK read both fields from the 402 response — don’t hardcode.

// Mainnet flip

When you’re ready for real money

Same recipe. Swap four values: network ID eip155:84532eip155:8453, the USDC asset address (mainnet contract is different), the RPC URL (sepolia.base.orgmainnet.base.org), and add CDP API keys for the mainnet facilitator.

// What to read next

CWS Knowledge Base
Built something with this?

CWS KB has the patterns the next 10 customers buy — vetted prompts, reusable workflows, and shipping-ready recipes. Pay-per-call via x402 or fixed-price.

Browse the catalog