Need help? Join the x402 Discord for the latest
updates.
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.
- Makes the initial request (if using Fetch) or intercepts the initial request (if using Axios/HTTPX/Requests)
- If a 402 response is received, parses the payment requirements from the
PAYMENT-REQUIREDheader - Creates a payment payload using the configured client and registered schemes
- Retries the request with the
PAYMENT-SIGNATUREheader
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
- Node.js
- Go
- Python
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
- Node.js
- Go
- Python
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.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.- Node.js
- Go
- Python (httpx)
- Python (requests)
Create a Features:
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.- @x402/fetch
- @x402/axios
wrapFetchWithPayment extends the native fetch API to handle 402 responses and payment
headers for you. Full example here.- 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.- TypeScript
- Go
- Python
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
Theupto 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.
- TypeScript
- Go
- Python
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 forCdpX402Client 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:
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 toCdpX402Client 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:
4. Fund your wallet
Your wallet needs USDC before it can pay.- Node.js
- Python
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:
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.- Node.js
- Go
- Python
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: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.
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.
- Node.js
- Python
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
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 (
exactfor fixed-price,uptofor 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
uptowhere you only pay the actual usage
References
- x402 in the CDP SDK - TypeScript primitives reference
- @x402/fetch on npm
- @x402/axios on npm
- @x402/evm on npm
- x402 Go module
- x402 Python package on PyPI
- x402 Bazaar documentation - Discover available services
- X402 with CDP Wallets - User-facing applications with CDP wallets