How AI Agents Pay Each Other
A Practical Guide to the x402 Protocol
AI agents are starting to call APIs, use tools, and consume services autonomously. But there's a missing piece: how do they pay? An agent can't fill out a credit card form, can't pass a CAPTCHA, can't sign up for a SaaS trial. It needs a machine-native payment protocol.
That's what x402 solves. It extends HTTP 402 Payment Required β a status code that's been reserved since 1997 but never used β into a working protocol for agent micropayments. An agent calls an API, gets back a 402 Payment Required with a payment invoice, pays it in USDC, and retries. All automated. No human in the loop.
This guide walks through the protocol, shows working code in three languages, and demonstrates how to use minia2a.uk β a marketplace of 154 x402-payable services β to discover, test, and pay for agent APIs.
1. The Problem: Why HTTP 402 Now?
Agents need APIs. APIs need payment. The existing options all break for autonomous agents:
| Method | Why It Fails for Agents |
|---|---|
| API Keys (stripe, etc.) | Requires human signup, billing setup, key management |
| OAuth / Credit Cards | Requires browser interaction, human approval |
| Monthly Subscriptions | Agents make 10 calls, not 10,000. Per-call pricing needed. |
| Crypto (direct) | No standard way to discover payment address, verify receipt |
x402 fills this gap by using HTTP itself as the payment channel. The server says "pay me X USDC to this address" in a standard HTTP header. The client pays on-chain and retries. No signup, no KYC, no API keys.
2. How x402 Works: The Flow
The protocol is simple. Here's what happens when an agent calls an x402-protected endpoint:
Step-by-step
- Agent makes a request β standard HTTP call to an API endpoint
- Server returns 402 β with
X-402-Paymentheader containing price, network, recipient address - Agent's wallet pays β sends the USDC amount to the specified address on Base
- Agent retries β includes transaction hash in
X-402-Paymentheader - Server verifies β checks on-chain payment, returns the actual response
Here's what the HTTP exchange looks like at the wire level:
# Step 1: Agent calls the API
GET /api/v1/gas-price HTTP/1.1
Host: api.minia2a.uk
# Step 2: Server demands payment
HTTP/1.1 402 Payment Required
X-402-Payment: network=base, token=USDC, amount=500000,
address=0xf16F0882de08315B438E9f3a2Abfb2d2E5d94ECA,
chainId=8453, maxAge=300
# Step 3 & 4: Agent pays, retries with proof
GET /api/v1/gas-price HTTP/1.1
Host: api.minia2a.uk
X-402-Payment: tx=0xabcd1234...def789
# Step 5: Payment verified, here's your data
HTTP/1.1 200 OK
Content-Type: application/json
{"fast": 0.53, "standard": 0.51, "slow": 0.49, "unit": "gwei"}
The X-402-Payment header is the core of the protocol. In the 402 response it carries the invoice. In the retry request it carries the payment proof. That's it β the entire protocol fits in one HTTP header.
3. Using the x402 SDKs
The x402 Foundation (now part of the Linux Foundation with 40 member organizations) maintains SDKs in multiple languages. Here's how to use them:
TypeScript / Node.js
import { x402 } from '@x402/core';
import { ethers } from 'ethers';
// 1. Set up wallet with some USDC on Base
const provider = new ethers.providers.JsonRpcProvider(
'https://mainnet.base.org'
);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
// 2. Create an x402-enabled fetch
const payedFetch = x402.fetch(wallet);
// 3. Call any x402-protected endpoint
const gasPrice = await payedFetch(
'https://api.minia2a.uk/api/v1/gas-price'
).then(r => r.json());
console.log(gasPrice);
// { fast: 0.53, standard: 0.51, slow: 0.49, unit: 'gwei' }
Python
from x402 import X402Client
from web3 import Web3
# 1. Initialize wallet
w3 = Web3(Web3.HTTPProvider('https://mainnet.base.org'))
account = w3.eth.account.from_key(os.environ['PRIVATE_KEY'])
# 2. Create client with spend limit (safety!)
client = X402Client(
wallet=account,
max_spend_per_call=0.10, # $0.10 USDC max per call
max_spend_per_session=5.00 # $5.00 USDC max total
)
# 3. Call any x402 endpoint
response = client.get('https://api.minia2a.uk/api/v1/gas-price')
print(response.json())
Go
package main
import (
"fmt"
"github.com/x402-foundation/x402-go"
)
func main() {
// 1. Create client with wallet
client := x402.NewClient(x402.Config{
PrivateKey: os.Getenv("PRIVATE_KEY"),
RPCURL: "https://mainnet.base.org",
MaxSpend: x402.MustParseUSDC("0.50"),
})
// 2. Make payment-enabled HTTP request
resp, err := client.Get("https://api.minia2a.uk/api/v1/gas-price")
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Println(resp.Status) // 200 OK
}
@x402/core package (v2.20.0, by Coinbase) is the canonical TypeScript implementation. Python and Go SDKs are maintained by the x402 Foundation on github.com/x402-foundation/x402. All three are open-source (Apache 2.0).
4. The minia2a.uk Marketplace
minia2a.uk is a live marketplace with 154 x402-payable services and 36 registered agents. It runs on Base L2 with USDC β transactions settle in ~2 seconds for fractions of a cent.
What's Available
The marketplace has services across these categories:
| Category | Example Endpoints | Typical Price |
|---|---|---|
| π Web Intelligence | web-scrape, web-retrieve, screenshot, http-analyze | $0.01β0.05 |
| β½ Blockchain Data | gas-price, token-security, tx-decode, wallet-intel, price-oracle | $0.005β0.02 |
| π Data Processing | summarize, sentiment, keywords, language-detect, text-to-json | $0.005β0.03 |
| π οΈ Developer Tools | uuid, hash, base64, jwt-decode, markdown, qr, csv-json | $0.001β0.01 |
| π§ Communication | email, email-verify, captcha-solve | $0.01β0.03 |
| π Security | address-parse, domain-intel, npm-audit, pip-audit, liveness-check | $0.005β0.02 |
All endpoints follow the same pattern: HTTP request β 402 Payment Required β pay USDC β get result. No signup. No API keys. No KYC. The marketplace takes a 5% fee on transactions.
Try It: Your First x402 Call in 30 Seconds
# 1. Register an agent (name + wallet address)
curl -X POST https://minia2a.uk/api/register \
-H "Content-Type: application/json" \
-d '{"name":"my-first-agent","wallet":"0xYOUR_WALLET"}'
# 2. Get an x402 endpoint URL β any service on the marketplace
# 3. Call it with your x402-enabled client
# 4. Get back results. Pay in USDC. Done.
5. Pricing: What Do Things Cost?
Services set their own prices in USDC (denominated in cents). Here's the real distribution from the live marketplace:
| Price Range | Services | Examples |
|---|---|---|
| $0.001β0.01 (freeβ1Β’) | ~30% | uuid, hash, base64, qr, emoji, random |
| $0.01β0.03 (1β3Β’) | ~35% | time, dns, ip-lookup, date-math, color |
| $0.03β0.10 (3β10Β’) | ~25% | web-scrape, sentiment, token-security, email |
| $0.10β0.50 (10β50Β’) | ~10% | screenshot, captcha-solve, pdf-text, liveness-check |
Transaction fees on Base are ~$0.001 per transfer. At these prices, an agent with $5 of USDC can make hundreds of API calls.
6. Safety: Spend Limits and Budget Control
Letting an AI agent spend real money sounds scary. The x402 ecosystem addresses this with several safeguards:
Per-Agent Budget Controls
- Per-call caps: Never pay more than $X for a single API call
- Session limits: Max total spend per agent session/task
- Daily limits: Hard ceiling on total daily spend
- Non-custodial wallets: You hold the keys. No exchange or platform holds your funds.
- Payment visibility: Every payment is an on-chain USDC transfer β fully auditable
Here's how to set spend limits in each SDK:
// TypeScript β agentwallet-sdk
import { AgentWallet } from '@x402/agentwallet';
const wallet = new AgentWallet({
privateKey: process.env.PRIVATE_KEY,
budgets: {
perCall: '0.10', // max $0.10 per call
perSession: '3.00', // max $3.00 per session
perDay: '20.00', // max $20.00 per day
}
});
// Python
client = X402Client(
wallet=account,
max_spend_per_call=0.10,
max_spend_per_session=5.00
)
// Go
client := x402.NewClient(x402.Config{
MaxSpend: x402.MustParseUSDC("0.50"),
DailyCap: x402.MustParseUSDC("10.00"),
})
7. Publishing Your Own x402 Service
Have an API? Want agents to pay you for it? Publishing on minia2a.uk takes one HTTP request:
# Publish a new service to the marketplace
curl -X POST https://minia2a.uk/api/publish \
-H "Content-Type: application/json" \
-d '{
"name": "my-awesome-api",
"description": "Does something agents need",
"category": "developer-tools",
"priceCents": 3,
"endpoint": "https://my-api.com/v1/awesome",
"wallet": "0xYOUR_USDC_WALLET"
}'
Once published, your service appears in the marketplace. Agents can discover it, call it, and pay you β all automatically. You receive 95% of the payment (5% marketplace fee).
8. The Bigger Picture: M2M Economy
Machine-to-machine payments aren't science fiction. They're happening now:
- Coinbase Agentic.Market: 480K+ agents with auto-indexing on payment β showing there's massive demand for agent-to-agent commerce
- x402 Foundation (Linux Foundation): 40 member organizations building the protocol standard β it's not a single-company effort
- agent-tools.cloud: 20K+ MCP servers and 400+ x402 APIs β the tool ecosystem is growing fast
- minia2a.uk: 154 services, 289K+ requests processed β real usage, not just theory
As AI agents become more autonomous β researching, coding, transacting, negotiating β they need a payment rail that's as automated as they are. x402 on Base provides that: instant settlement, negligible fees, no human checkpoints.
9. Getting Started Checklist
- Get a wallet with USDC on Base β Coinbase Wallet or any Ethereum wallet. Fund it with $5-10 of USDC.
- Install an x402 SDK β
npm install @x402/coreor your language's equivalent. - Register on minia2a.uk β one curl call:
POST /api/registerwith your wallet address. - Browse the marketplace β visit minia2a.uk to see 154 available services.
- Make your first paid call β pick any endpoint, call it with your x402 client, see the payment flow work.
- Publish your own service β turn your API into a revenue stream for agents.
Resources
- minia2a.uk β Live x402 marketplace. 154 services. No signup.
- Live Stats API β Real-time marketplace statistics
- llms-full.txt β Complete marketplace data for AI agents
- x402.org β Official x402 Foundation website
- github.com/x402-foundation/x402 β Protocol spec + SDKs
- @x402/core on npm β Coinbase's TypeScript SDK (v2.20.0)
- Agentic.Market β Coinbase's agent marketplace (480K+ agents)
- agent-tools.cloud β 20K+ MCP servers + 400+ x402 APIs