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 — all 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/HTTPX/Requests)
If a 402 response is received, parses the payment requirements
Verifies the payment amount is within the allowed maximum
Creates a payment header using the provided wallet client
First, create an account at cdp.coinbase.com and get the following API keys from the portal to store as environment variables:
Copy
Ask AI
# store in .env or using the command `export <name>="secret-info"`CDP_API_KEY_ID=your-api-key-idCDP_API_KEY_SECRET=your-api-key-secretCDP_WALLET_SECRET=your-wallet-secret
If you prefer to use your own wallet, you can use standalone libraries:
Install the required package:
Copy
Ask AI
npm install viem
Then instantiate the wallet account:
Copy
Ask AI
import { createWalletClient, http } from "viem";import { privateKeyToAccount } from "viem/accounts";import { baseSepolia } from "viem/chains";// Create a wallet client (using your private key)const account = privateKeyToAccount("0xYourPrivateKey"); // we recommend using an environment variable for this
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.See the full example here for Python and Node.js.
Copy
Ask AI
import { useFacilitator } from "x402/verify";import { facilitator } from "@coinbase/x402";// Get the list function from the facilitatorconst { list } = useFacilitator(facilitator);// Discover all available x402 servicesconst services = await list();
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.