Quickstart for Buyers
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.
Prerequisites
Before you begin, ensure you have:
A crypto wallet with USDC (any EVM-compatible wallet)
Node.js and npm, or Python and pip
A service that requires payment via x402
Note We have pre-configured examples available in our repo, including examples for fetch, Axios, and MCP.
1. Install Dependencies
HTTP Clients (Axios/Fetch) Install x402-axios or x402-fetch:
npm install x402-axios
# or
npm install x402-fetchMCP (Unofficial) This community package showcases how AI agents can use Model Context Protocol (MCP) with x402. We're working on enshrining an official MCP spec in x402 soon.
Install the required packages for MCP support:
npm install x402-mcp ai @modelcontextprotocol/sdkInstall the x402 package
pip install x4022. Create a Wallet Client
Create a Wallet Client
Install the required package:
npm install viemThen instantiate the wallet account:
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 thisInstall the required package:
pip install eth_accountThen instantiate the wallet account:
from eth_account import Account
account = Account.from_key("your_private_key") # we recommend using an environment variable for thisSolana (SVM)
Use SolanaKit to instantiate a signer:
import { createKeyPairSignerFromBytes } from "@solana/kit";
import { base58 } from "@scure/base";
// 64-byte base58 secret key (private + public)
const signer = await createKeyPairSignerFromBytes(
base58.decode(process.env.SOLANA_PRIVATE_KEY!)
);3. Make Paid Requests Automatically
Node.js
You can use either x402-fetch or x402-axios to automatically handle 402 Payment Required responses and complete payment flows.
x402-fetch extends the native fetch API to handle 402 responses and payment headers for you. Full example here
import { wrapFetchWithPayment, decodeXPaymentResponse } from "x402-fetch";
// other imports...
// wallet creation logic...
const fetchWithPayment = wrapFetchWithPayment(fetch, account);
fetchWithPayment(url, { //url should be something like https://api.example.com/paid-endpoint
method: "GET",
})
.then(async response => {
const body = await response.json();
console.log(body);
const paymentResponse = decodeXPaymentResponse(response.headers.get("x-payment-response")!);
console.log(paymentResponse);
})
.catch(error => {
console.error(error.response?.data?.error);
});x402-axios adds a payment interceptor to Axios, so your requests are retried with payment headers automatically. Full example here
import { withPaymentInterceptor, decodeXPaymentResponse } from "x402-axios";
import axios from "axios";
// other imports...
// wallet creation logic...
// Create an Axios instance with payment handling
const api = withPaymentInterceptor(
axios.create({
baseURL, // e.g. https://api.example.com
}),
account,
);
api
.get(endpointPath) // e.g. /paid-endpoint
.then(response => {
console.log(response.data);
const paymentResponse = decodeXPaymentResponse(response.headers["x-payment-response"]);
console.log(paymentResponse);
})
.catch(error => {
console.error(error.response?.data?.error);
});x402-mcp provides payment handling for MCP clients, allowing AI agents to automatically pay for tools. Full example here
import { convertToModelMessages, stepCountIs, streamText } from "ai";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { experimental_createMCPClient as createMCPClient } from "ai";
import { withPayment } from "x402-mcp";
// Create MCP client with payment capabilities
const mcpClient = await createMCPClient({
transport: new StreamableHTTPClientTransport(mcpServerUrl), // URL of your MCP server
}).then((client) => withPayment(client, {
account, // Your wallet account from step 2
network: "base" // or "base-sepolia" for testnet
}));
// Get available tools (both paid and free)
const tools = await mcpClient.tools();
// Use the tools with your AI model
const result = streamText({
model: "gpt-4", // or any AI model
tools,
messages: convertToModelMessages(messages),
stopWhen: stepCountIs(5), // Limit tool calls for safety
onFinish: async () => {
await mcpClient.close();
},
system: "ALWAYS prompt the user to confirm before authorizing payments",
});Features:
Automatically detects when MCP tools require payment
Handles x402 payment flow transparently
Supports both paid and free tools from the same server
Integrates seamlessly with Vercel AI SDK
Note: The withPayment wrapper adds payment capabilities to any MCP client. When a tool call requires payment, it will automatically handle the x402 payment flow using your configured wallet.
Python
You can use either httpx or Requests to automatically handle 402 Payment Required responses and complete payment flows.
Requests is a well-established library for synchronous HTTP requests. It is simple and ideal for straightforward, sequential workflows.
HTTPX is a modern library that supports both synchronous and asynchronous (async) HTTP requests. Use HTTPX if you need high concurrency, advanced features like HTTP/2, or want to leverage Python’s async capabilities
Both support a simple and extensible approach. The simple returns a pre-configured client that handles payments automatically, while the extensible lets you use an existing session/client. The simple is covered here, while the extensible is in the README of the full examples linked below.
from x402.clients.httpx import x402HttpxClient
# Other imports...
# Wallet creation logic ...
# Create client and make request
async with x402HttpxClient(account=account, base_url="https://api.example.com") as client:
response = await client.get("/protected-endpoint")
print(await response.aread())from x402.clients.requests import x402_requests
# Other imports...
# Wallet creation logic ...
# Create session and make request
session = x402_requests(account)
response = session.get("https://api.example.com/protected-endpoint")
print(response.content)4. Error Handling
Clients will throw errors if:
The request configuration is missing
A payment has already been attempted for the request
There is an error creating the payment header
Summary
Install an x402 client package
Create a wallet client
Use the provided wrapper/interceptor to make paid API requests
Payment flows are handled automatically for you
References:
For questions or support, join our Discord.
Last updated