Need help? Join the x402 Discord for the latest
updates.
Facilitator URLs
We recommend the CDP facilitator for both testnet and mainnet. Use x402.org if you want to try x402 without signing up.
This guide walks you through how to use x402 to interact with services that require payment. By the end of this guide, you will be able to programmatically discover payment requirements, complete a payment, and access a paid resource.
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:
- Makes the initial request (if using Fetch) or intercepts the initial request (if using Axios)
- If a 402 response is received, parses the payment requirements from the
PAYMENT-REQUIREDheader - Creates a payment payload using the configured x402Client and registered schemes
- Retries the request with the
PAYMENT-SIGNATUREheader
Prerequisites
Before you begin, ensure you have:- A crypto wallet with USDC (any EVM-compatible wallet, e.g., CDP Wallet, AgentKit)
- Node.js and npm, Go, or Python and pip installed
- A service that requires payment via x402
- For testnet: Base Sepolia ETH for gas and testnet USDC. Get funds from the CDP Faucet
We have pre-configured examples available in our
repo, including examples
for fetch, Axios, and MCP.
1. Install Dependencies
- Node.js
- Go
- Python
Install the x402 client packages:
2. Create a Wallet Client
Create a wallet client using a CDP Non-custodial Wallet (recommended) or a standalone wallet library (viem for EVM on Node.js, or Go’s crypto libraries).CDP Non-custodial Wallet (Recommended)
First, create an account at cdp.coinbase.com and get the following API keys from the portal to store as environment variables:Node.js
Node.js
Standalone Wallet Libraries
If you prefer to use your own wallet, you can use standalone libraries:EVM (Node.js with viem)
EVM (Go)
EVM (Python)
Install the required package:Solana (SVM)
Use SolanaKit to instantiate a signer: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)
You can use either
@x402/fetch or @x402/axios:- @x402/fetch
- @x402/axios
@x402/fetch extends the native Features:
fetch API to handle 402 responses and payment headers for you. Full example here- Automatically handles 402 Payment Required responses
- Verifies payment and generates
PAYMENT-SIGNATUREheaders - Retries the request with proof of payment
- Supports all standard fetch options
Multi-Network Client Setup
You can register multiple payment schemes to handle different networks:- TypeScript
- Go
- Python
Supporting the Upto Scheme
If you need to interact with services that use theupto payment scheme (usage-based billing), register the UptoEvmScheme alongside your existing exact schemes. The upto scheme is transparent to the buyer — the SDK handles the max-authorization signing automatically, and you are only charged the actual settled amount (which may be less than the authorized maximum).
- TypeScript
- Go
- Python
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.4. 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
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.5. Paying with Any ERC-20 Token via Permit2 (EVM)
Some endpoints accept payment in non-USDC ERC-20 tokens via Permit2. The official TypeScript, 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.
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.
6. Error Handling
Clients will 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
Summary
- Install x402 client packages (
@x402/fetchor@x402/axios) and mechanism packages (@x402/evm,@x402/svm) - Create a wallet signer
- Create an
x402Clientand register payment schemes (exactfor fixed-price,uptofor usage-based billing) - Use the provided wrapper/interceptor to make paid API requests
- (Optional) Use the x402 Bazaar to discover services dynamically
- Payment flows are handled automatically for you — including
uptowhere you only pay the actual usage
References:
- @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