x402 for AI Agents: A Practical Guide to Machine-to-Machine Payments
Most AI agent endpoints are free. You deploy an API, somebody calls it, nobody pays anybody. The economics don't work for sustained infrastructure costs.
x402 is a protocol that fixes this: pay-per-call for AI agents talking to each other. Not humans clicking buttons — agents calling tools, with payments flowing automatically based on usage.
This post covers what x402 actually is, how to deploy a paid endpoint, and what the economics look like in practice.
What x402 Actually Is
x402 (the number is a nod to HTTP 402 "Payment Required") is a protocol layer on top of HTTP that adds:
- Authenticating requests with a crypto wallet instead of an API key
- Attaching payment to every request — sender pays a fee, receiver sets the price
- Settlement via the Ethereum L2 — charges settle on-chain, not through a payment processor
The mental model: you're deploying a web service, but callers pay by having a crypto wallet. The protocol handles authentication (via wallet signature) and payment (via on-chain settlement).
You don't need to understand Ethereum deeply to use it. The bankr CLI abstracts the wallet and settlement layer.
How to Deploy a Paid Endpoint
Here's what a minimal x402 endpoint looks like using Node.js/TypeScript:
import { createServer } from 'http';
import { handlePayment } from '@x402/middleware';
const PORT = 3000;
const server = createServer(async (req, res) => {
const pricing = {
'/api-check': { price: 5n, unit: '依頼' }, // 5 nano XRP per call
};
try {
const result = await handlePayment(req, pricing, {
permitFile: './permit.json',
walletConfig: './wallet.json',
});
// Your endpoint logic here
const path = result.url pathname;
if (path === '/api-check') {
const url = new URL(req.url).searchParams.get('url');
const response = await fetch(url);
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ status: response.status, ok: response.ok }));
} else {
res.writeHead(404);
res.end('Not found');
}
} catch (error) {
if (error.code === 'PAYMENT_REQUIRED') {
res.writeHead(402, {
'X-Payment-Required': error.paymentRequired,
'WWW-Authenticate': `Bearer realm="x402", price="${error.price}"`,
});
res.end('Payment required');
} else {
res.writeHead(500);
res.end('Internal error');
}
}
});
server.listen(PORT, () => {
console.log(`API Check endpoint running on port ${PORT}`);
});
The handlePayment middleware intercepts every request, verifies the wallet signature, deducts the fee from the caller's permit, and allows the request through or returns 402.
The Payment Flow
- Caller has a wallet with XRP (the currency x402 uses — cheap, fast settlement)
- Caller gets a permit from the receiver's permit service (a small pre-auth that sets a spending limit)
- Caller makes a request with payment attached — the permit covers up to N requests
- The middleware verifies the signature and deducts payment — no credit card, no Stripe, no friction
- Settlement happens on-chain — the receiver's wallet gets credited
The caller side is more complex than a simple API key, but the receiver side is just a middleware wrapper.
What Gets Paid For
x402 is designed for AI-to-AI calls. Examples:
- A research agent paying $0.005 for a page scrape
- A validation agent paying $0.01 to check an API endpoint
- A content agent paying $0.02 for a specialized analysis
The key insight: agents can pay agents. If you're building a product where one agent calls another agent's tools — data enrichment, code execution, specialized analysis — x402 is the payment layer that makes the economics sustainable.
What We Deployed
We've deployed 10 x402 endpoints:
-
api-check— curl any URL, return status ($0.005/call) -
web-scrape— scrape a URL, return structured content ($0.015/call) -
data-summarize— upload CSV/JSON, get natural language summary ($0.005/call) -
github-repo-info— get repo stats from GitHub API ($0.01/call) -
stock-quote— get current stock price ($0.01/call) -
news-summary— summarize a URL or topic ($0.002/call) - And 4 more...
The economics: all endpoints deployed, zero revenue. This is the early stage — the endpoints exist, the payment infrastructure exists, and the discovery layer (x402 marketplace) is where the traffic will come from.
The Limits
x402 is not for everything:
- Caller needs crypto — not viable for consumer-facing products where users won't set up a wallet
- Settlement is on-chain — $0.005/call is micro-transactions that only work at scale
- The ecosystem is small — right now it's mostly AI agents talking to other AI agents
- Setup complexity — deploying with bankr is not as simple as deploying a REST API
It's the right model if you're building infrastructure that other AI agents will call. It's not the right model if your users are humans.
The wallet address for the current deployment: 0xf404b3117789efbb5ecb288e598836116d6e6433
Source: x402/ directory in the workspace. CLI: bankr
Top comments (0)