Payments without pre-registration
AI agents excel at complex tasks, but hit a wall when they need to pay for external services. The usual path—registering with every API, managing keys, setting up billing—doesn't scale for autonomous discovery. x402 solves this by building payment into HTTP itself, using the 402 Payment Required status code so endpoints can request payment without any prior account setup.
Unlike vendor-specific payment SDKs, x402 is an open web protocol with no centralized provider. It settles most transactions as one-time USDC payments on the Base blockchain for speed and accountless transfers, but the standard itself supports other networks and even non-crypto schemes. The x402-mcp package bridges this protocol to MCP servers and the Vercel AI SDK.
Protecting API routes
Servers add payment requirements with a simple middleware declaration:
import { paymentMiddleware } from "x402-next";
export const middleware = paymentMiddleware(
{
"/protected": {
price: 0.01,
config: {
description: "Access to protected content"
}
},
}
);
On the client side, a fetch wrapper handles the negotiation:
import { wrapFetchWithPayment } from "x402-fetch";
const fetchWithPay = wrapFetchWithPayment(fetch, client);
const response = await fetchWithPay("https://api.example.com/paid-endpoint");
Agents that pay for MCP tools
The x402-mcp package wraps the existing mcp-handler package, letting MCP servers define paidTools with an associated price per call:
import { createPaidMcpHandler } from "x402-mcp";
import z from "zod";
const handler = createPaidMcpHandler(
(server) => {
server.paidTool(
"add_numbers",
{
// declare a price of $0.001
price: 0.001
},
{ a: z.number(), b: z.number() },
async (args) => {
// ...your tool call
}
);
},
{ recipient: process.env.WALLET_ADDRESS }
);
export { handler as GET, handler as POST };
Client-side, a single wrapper adds payment capability. It also injects extra tools so agents can decide autonomously when and how to authorize tool payments over MCP, in a transport-agnostic way:
import { experimental_createMCPClient as createMCPClient } from "ai";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { withPayment } from "x402-mcp";
const mcpClient = await createMCPClient({
transport: new StreamableHTTPClientTransport(url),
}).then((client) => withPayment(client, { account }));
const tools = await mcpClient.tools();
The HTTP payment flow
The client requests a protected resource
The server returns a 402 status with payment instructions
The client submits payment authorization via header and retries
The server verifies and settles the payment with an external facilitator
The resource returns alongside payment outcomes in a response header
A working starter
To see it in action, the x402 AI Starter template demonstrates the protocol across the full AI toolchain: Next.js, AI SDK, AI Elements, and AI Gateway. It includes:
An API route protected by
paymentMiddlewareAn MCP server with a
paidToolAn MCP client extended with
withPaymentA normal page that charges crawlers via x402
Secure server managed wallets through Coinbase's developer platform
A frontend AI Chat + API playground
The template deploys to Vercel with one click and ships with server wallet management and payment facilitator credentials via Coinbase Developer Platform. Install it with npm install x402-mcp, or explore the live example.
Where this leads
For agent developers needing to pay for services—or API providers wanting to charge agents—x402 removes the registration bottleneck. The starter template offers a low-friction entry point for experiments.



