x402 API
x402 is a facilitator API for payment-gated HTTP resources. It helps you return machine-readable payment requirements, verify signatures, and serve paid responses to clients and AI agents in a consistent way.
API builders, platform teams, and agent-first products that need programmable paywalls for data, compute, or premium endpoints.
Monetize requests per call: protect private APIs, meter high-value inference endpoints, and unlock paid content through standard HTTP payment semantics.
Implement the payment flow in four steps. The protocol stays familiar: HTTP request, 402 challenge, signed payment, verification, then resource delivery.
When no payment proof is present, return 402 Payment Required with x402 payment details.
AI agents or HTTP clients sign and submit payment, then retry with the required payment header.
Call the x402 verify endpoint to confirm amount, recipient, and signature integrity.
Serve the protected response after verification and settle the payment in your preferred flow.
Start with a 402 response fallback, then verify incoming payment proof before serving your response.
import { NextRequest, NextResponse } from "next/server";
const FACILITATOR = "https://x402.diegolosramos.com/v2";const FACILITATOR_API_KEY = process.env.X402_FACILITATOR_API_KEY;
export async function GET(req: NextRequest) { if (!FACILITATOR_API_KEY) { throw new Error("Missing X402_FACILITATOR_API_KEY"); }
const encodedPayment = req.headers.get("PAYMENT-SIGNATURE");
if (!encodedPayment) { return NextResponse.json({ error: "Payment required" }, { status: 402 }); }
const paymentPayload = JSON.parse(atob(encodedPayment)); const paymentRequirements = { scheme: "exact", network: "eip155:84532", asset: "0x0000000000000000000000000000000000000000", amount: "1000000", payTo: "0xYourReceivingAddress", maxTimeoutSeconds: 60, extra: {}, };
const facilitatorBody = { x402Version: paymentPayload.x402Version, paymentPayload, paymentRequirements, };
const verify = await fetch(`${FACILITATOR}/verify`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${FACILITATOR_API_KEY}`, }, body: JSON.stringify(facilitatorBody), });
if (!verify.ok) { return NextResponse.json({ error: "Invalid payment" }, { status: 402 }); }
const settle = await fetch(`${FACILITATOR}/settle`, { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${FACILITATOR_API_KEY}`, }, body: JSON.stringify(facilitatorBody), });
if (!settle.ok) { return NextResponse.json({ error: "Unable to settle payment" }, { status: 402 }); }
return NextResponse.json({ data: "protected response" });}Use x402 to monetize API access, automate payment by agent clients, and keep your existing HTTP architecture while introducing on-chain payment rails.