x402 Protocol
x402 lets an API charge a small amount for each request. The server answers with HTTP status 402 and the price. The client pays in USDC and sends the request again. You do not need subscriptions or separate billing.
How It Works
A paid request takes three steps over plain HTTP. The payment details travel in the same requests and responses.
Request Resource
The client sends a normal HTTP request to a paid endpoint.
402 + Payment Info
The server answers with status 402. The response headers say what to pay and where.
Pay & Receive Data
The client signs the payment and sends the request again with the proof. The server settles the payment and returns the data.
Core Capabilities
The module covers the server side, the client side and the controls for AI agents.
Payment Gate Middleware
A Laravel middleware puts a price on a route. It answers unpaid requests with a 402 response. It checks that the payment is settled before it returns any data.
- Price set for each route
- 402 responses built for you
- Payment checked before the data is sent
AI Agent Payments
An AI agent can pay for API access on its own through a Model Context Protocol tool. Budget limits cap what each agent can spend. Payments above a set amount wait for approval.
- Model Context Protocol tool for agents
- A budget for each agent
- Approval needed above a set amount
Spending Limits
You can cap spending per day, per transaction and per agent. A payment above the approval amount goes to a person for review.
- Daily and per-transaction limits
- A spending budget for each agent
- Approval amounts you can change
Multi-Network Settlement
Payments settle in USDC on Base, Ethereum, Avalanche or Solana. Base is the default because its fees are low. Each endpoint can use its own network.
- Base by default, for low fees
- Ethereum, Avalanche and Solana also work
- A network chosen for each endpoint
Facilitator Integration
A facilitator is the service that checks and settles each payment. You can use the public x402.org facilitator. You can also run your own facilitator if you want full control.
- Your own facilitator or x402.org
- Payment signature verification
- Settlement confirmed for you
GraphQL & REST APIs
You manage paid endpoints and spending limits through the API. You can also read the payment history and payment statistics. Both REST and GraphQL are available.
- More than 15 REST endpoints
- A GraphQL schema
- Payment history and statistics
Built For
x402 suits any service that wants to charge per request. These are the most common cases.
API Monetization
You can charge for each request to a data, analytics or AI endpoint. Clients pay inside the HTTP request, so you do not run subscriptions or a billing system.
Autonomous AI Agents
AI agents can pay for API access without a person in the loop. The Model Context Protocol tool keeps each agent within its spending limit.
Premium Data Feeds
You can sell market data, research reports or compliance feeds per request. Buyers pay in USDC only for the data they fetch.
Developer Integration
On the server, you add the x402 middleware to a Laravel route. On the client, a few lines of JavaScript handle the 402 response.
S Laravel Middleware on the Server
// routes/api.php
// Put the payment gate on the routes that may charge
Route::middleware(['x402.payment'])->group(function () {
Route::get('/premium/market-data', [MarketDataController::class, 'index']);
Route::get('/premium/analytics', [AnalyticsController::class, 'show']);
});
// Set the price of each route through the API
// POST /api/v1/x402/endpoints
// {"method": "GET", "path": "api/premium/market-data", "price": "0.10"}
C Handling a 402 Response in the Client
const res = await fetch('/v2/premium/data');
if (res.status === 402) {
// Read the payment requirements (base64-encoded JSON)
const required = JSON.parse(atob(res.headers.get('PAYMENT-REQUIRED')));
// Sign a USDC payment authorisation with the wallet
const signature = await signPayment(required.accepts[0]);
// Send the request again with the signed payment
const data = await fetch('/v2/premium/data', {
headers: { 'PAYMENT-SIGNATURE': signature }
});
}
Quick Start
Configure Facilitator
Set X402_FACILITATOR_URL and X402_RECEIVER_ADDRESS in your .env
Add Middleware
Attach the x402.payment middleware to each route that may charge. Then set its price through the x402 API.
Receive Payments
Clients get a 402 response with the price. Their x402 client pays in USDC and repeats the request.
Supported Networks
You can settle on the network that suits your service. Base has the lowest fees for frequent small payments.
| Network | Chain ID | Asset | Status |
|---|---|---|---|
| Base | eip155:8453 | USDC | Primary |
| Ethereum | eip155:1 | USDC | Supported |
| Avalanche | eip155:43114 | USDC | Supported |
| Solana | solana:mainnet | USDC | Supported |
You set the network for each endpoint in the middleware or through the management API.
Error Handling
x402 errors follow the Problem Details for HTTP APIs standard (RFC 9457). Each error has a type for programs, a detail for people and the 402 status code.
Payment Verification Failed
Spending Limit Exceeded
Charge for Your API per Request
Add the x402 middleware to your endpoints, and each request can carry its own payment. You do not need a billing system.
Related: Machine Payments · Zelta CLI · Card-Payment Command Line Tool