Skip to main content
When an API returns HTTP 402 Payment Required, the x402 module handles payment automatically. Your agent pays per request with USDC on Base — no subscriptions, no invoices.

How it works

Setup

You need a wallet with USDC on Base. See Wallets & Payments to generate one.

Create a paying fetch

Make requests

Use fetchWithPayment exactly like fetch. When the server returns 402, payment is handled transparently:
If the API doesn’t return 402, the request passes through as a normal fetch — no payment occurs.

Testnet

Use base-sepolia for development and testing:
Get testnet USDC from a Base Sepolia faucet to fund your wallet.

Wrapping a custom fetch

If you have a custom fetch (e.g., with auth headers or logging), pass it as fetchFn:

Advanced re-exports

The x402 subpackage re-exports lower-level primitives for custom integrations:

Next steps

Wallets & Payments

Generate wallets, check balances, send USDC.

Build an LLM Agent

Combine pay-per-call APIs with your agent’s reasoning loop.

x402 Protocol

The HTTP 402 payment protocol specification.

API Reference

Full x402 API signatures and options.