Skip to main content
Need help? Join the x402 Discord for the latest updates.
This guide walks you through how to use x402 to pay for services that require payment. By the end, you will be able to programmatically discover payment requirements, complete a payment, and access a paid resource.
On TypeScript, this guide uses the CDP SDK x402 primitives, which pay from a CDP-managed wallet with no private keys to store. On Go and Python, it uses the vanilla x402 packages with a wallet you manage.
The x402 helper packages for various languages greatly simplify your integration with x402. You’ll be able to automatically detect payment challenges, authorize payments onchain, and retry requests with minimal code. The packages will automatically trigger the following flow:
  1. Makes the initial request (if using Fetch) or intercepts the initial request (if using Axios/HTTPX/Requests)
  2. If a 402 response is received, parses the payment requirements from the PAYMENT-REQUIRED header
  3. Creates a payment payload using the configured client and registered schemes
  4. Retries the request with the PAYMENT-SIGNATURE header

Prerequisites

Before you begin, ensure you have:
  • A Coinbase Developer Platform account and API keys
  • Node.js and npm, Go, or Python and pip installed
  • A crypto wallet with USDC (on TypeScript, the CDP SDK provisions one for you; on Go/Python, use a CDP Wallet or any EVM-compatible wallet)
  • A service that requires payment via x402
  • For testnet: Base Sepolia ETH for gas and testnet USDC. Get funds from the CDP Faucet
Pre-configured examples are available in the repos: the CDP SDK x402 examples (TypeScript) and the x402 examples (Go, Python, and more).

1. Install dependencies

Install the CDP SDK together with the x402 peer packages, plus one HTTP wrapper (@x402/fetch or @x402/axios):
The @x402/* packages are optional peer dependencies of @coinbase/cdp-sdk, so you install them explicitly. See x402 in the CDP SDK for details.

2. Set up your wallet

On TypeScript, the CDP SDK manages the paying wallet for you. Create an account at cdp.coinbase.com and store your API keys and wallet secret as environment variables:
CdpX402Client provisions and signs with a CDP-managed wallet automatically, so you never handle a private key.
Building with CDP Wallets in a user-facing app? Check out the X402 with CDP Wallets guide, which shows how to use the useX402 hook.
For Solana, the CDP SDK (TypeScript) provisions a Solana wallet alongside the EVM wallet automatically. To sign Solana payments with your own key instead, use the language’s SVM signer (@solana/kit in TypeScript, or the x402[svm] extra in Python).

3. Make paid requests automatically

You can automatically handle 402 Payment Required responses and complete payment flows using the x402 helper packages.
Create a CdpX402Client and pass it to either @x402/fetch, @x402/axios, or any other x402 wrapper.
CdpX402Client defaults to Base mainnet. To pay on Base Sepolia (testnet), either construct it with new CdpX402Client({ environment: "development" }) or set CDP_X402_CLIENT_ENVIRONMENT=development in your environment. The explicit config option takes precedence over the env var.
wrapFetchWithPayment extends the native fetch API to handle 402 responses and payment headers for you. Full example here.
Features:
  • Provisions and signs with a CDP-managed wallet, so no private keys are needed
  • Automatically handles 402 Payment Required responses
  • Retries the request with proof of payment
  • Supports all standard fetch options

Multi-Network Client Setup

Handle different networks from a single client.
CdpX402Client prescribes the Base network for its environment (Base mainnet by default, Base Sepolia with environment: "development"), registering exact + upto there. To pay on Solana or other EVM networks from the same client, add them via networkSchemes — one signer, no per-network setup. See Networks and RPC for details.

Supporting the Upto Scheme

The upto scheme (usage-based billing) lets a service charge only for actual usage up to an authorized maximum. It is transparent to the buyer: the SDK handles the max-authorization signing automatically, and you are only charged the actual settled amount.
CdpX402Client with the default EOA wallet registers upto alongside exact on its prescribed Base network, with no extra setup. It selects the right scheme based on what the server advertises in its 402 response. Same client as payForApi.ts:
The upto scheme is currently available on EVM networks only, in the TypeScript, Go, and Python SDKs. When registered, the SDK automatically selects the correct scheme based on what the server advertises in its 402 response.

Additional Client Configuration (TypeScript)

Optional configuration for CdpX402Client beyond the default setup in step 3.

Migrating from a self-managed key

If you already use @x402/core with your own private key, there are two ways to bring CDP into the mix. Keep managing your own key, but drop in CDP’s signer adapter instead of a CDP-managed wallet:
Or, keep your existing x402Client setup and slot a CDP-managed signer into it with fromCdpEvmAccount(), removing the private key without switching to CdpX402Client. Full example here:

Limiting spend for autonomous agents

Attach spend controls to CdpX402Client for per-payment and rolling caps, network/asset allowlists, and an approaching-limit callback that keeps an agent’s spending within a budget. Full example here:
See Spend controls for the full option list and error handling.

4. Fund your wallet

Your wallet needs USDC before it can pay.
Print the CDP-managed wallet address with client.getAddresses() (shown in step 3), then fund it. On Base Sepolia (testnet):
  • CDP Faucet (portal): portal.cdp.coinbase.com → “Onchain Tools” → “Faucet”
  • Programmatically:
The CDP faucet funds the same wallets the CDP x402 facilitator settles against, so no separate faucet is needed.

5. Discover Available Services (Optional)

Instead of hardcoding endpoints, you can use the x402 Bazaar to dynamically discover available services. This is especially powerful for building autonomous agents that can find and use new capabilities.
Extend the CDP facilitator client with the Bazaar discovery extension using the CDP SDK:
Learn more about service discovery in the x402 Bazaar documentation, including how to filter services, understand their schemas, and build agents that can autonomously discover new capabilities.
CDP facilitator: For semantic search, quality-ranked results, and rich filters (network, asset, scheme, price, extensions, and more) without an API key, use GET https://api.cdp.coinbase.com/platform/v2/x402/discovery/search as documented in CDP Bazaar: semantic search. The examples above use the paginated /discovery/resources list, which is still appropriate when you want to page through the raw catalog.

6. Integrate Base Builder Codes (Optional)

If you’re building on Base, integrate a Base Builder Code to get attributed for x402 transactions you drive. Paste this into Claude Code, Cursor, Codex, or any coding agent:
For full setup details — getting your Builder Code, manual integration steps, and verifying attribution onchain — see the Builder Codes page.

7. Paying with Any ERC-20 Token via Permit2 (EVM)

Some endpoints accept payment in non-USDC ERC-20 tokens via Permit2. The official TypeScript (including the CDP SDK), Go, and Python client SDKs handle Permit2 automatically — no extra client code is needed. When a server advertises Permit2 as the transfer method (extra.assetTransferMethod: "permit2"), the client SDK creates the correct PermitWitnessTransferFrom payload instead of an EIP-3009 authorization.

Gas Sponsorship (Automatic)

If the server declares a gas sponsorship extension (EIP-2612 or ERC-20), the TypeScript, Go, and Python client SDKs handle the Permit2 approval automatically:
  • EIP-2612 gas sponsoring: The client signs an off-chain permit() message. The facilitator submits the approval on-chain — no gas cost to you.
  • ERC-20 gas sponsoring: The client signs an approve() transaction. The facilitator broadcasts it on-chain before settling — no gas cost to you.
No additional client setup is needed for either case. The SDK detects the server’s advertised extension and responds accordingly. With the CDP SDK, CdpX402Client activates these paths automatically when the CDP facilitator advertises them.

One-Time Manual Approval (Fallback)

If the server uses Permit2 without gas sponsorship extensions, you must perform a one-time approval of the payment token to the Permit2 contract (0x000000000022D473030F116dDEE9F6B43aC78BA3) before your first payment. This only needs to be done once per token.
After this one-time approval, all subsequent Permit2 payments for that token work automatically.
For full details on EVM transfer methods and gas sponsorship, see Network Support.

8. Error Handling

Clients throw errors if:
  • No scheme is registered for the required network
  • The request configuration is missing
  • A payment has already been attempted for the request
  • There is an error creating the payment header
On TypeScript, if you configured spend controls, a blocked payment throws a SpendControlError with a machine-readable code (for example per_payment_cap, cumulative_cap, or network_not_allowed). Switch on err.code rather than matching message text.

Summary

  • Install an x402 client package (CDP SDK on TypeScript; vanilla x402 on Go/Python)
  • Set up your wallet (CDP-managed on TypeScript; CDP or standalone on Go/Python)
  • Use the provided wrapper/interceptor to make paid API requests (exact for fixed-price, upto for usage-based billing)
  • (Optional) Use the x402 Bazaar to discover services dynamically
  • (Optional) Register a Base Builder Code to get attributed for transactions you drive on Base
  • Payment flows are handled automatically for you, including upto where you only pay the actual usage

References

For questions or support, join our Discord.