402 Payment Required status code into a fully functional, standards-compatible micropayment handshake. No new infrastructure. No new wallets. Just headers.// Client signs a payment authorization (off-chain)
const payment = {
from: client.address,
to: server.address,
value: parseUnits("0.01", 6), // $0.01 USDC
validAfter: 0,
validBefore: Math.floor(Date.now() / 1000) + 3600,
nonce: randomBytes(32)
};
// EIP-712 typed signature — no gas!
const signature = await wallet.signTypedData(
domain, types, payment
);
// Attach to HTTP request
fetch("/api/data", {
headers: { "X-PAYMENT": encode(payment, signature) }
});validBefore ensures signatures expire, preventing replay attacks.// Express middleware — 10 lines to monetize any endpoint
async function x402Middleware(req, res, next) {
const payment = req.headers["x-payment"];
if (!payment) {
return res.status(402).json({
scheme: "exact",
network: "base",
token: USDC_ADDRESS,
amount: "10000", // $0.01
receiver: SERVER_WALLET
});
}
// Verify signature + settle on-chain
const result = await verifyAndSettle(payment);
if (result.valid) next();
else res.status(402).json({ error: "invalid payment" });
}transferWithAuthorization on-chain to claim funds atomicallynext() and return the protected resourceimport { createX402Server } from "@perkos/x402";
const app = createX402Server({
wallet: SERVER_WALLET,
network: "base",
token: "USDC"
});
// Free endpoint
app.get("/health", (req, res) =>
res.json({ ok: true })
);
// $0.001 per call — community market data
app.get("/api/markets", x402("0.001"),
async (req, res) => {
const data = await getMarketData();
res.json(data);
});
// $0.01 per call — premium analytics
app.get("/api/analytics", x402("0.01"),
async (req, res) => {
const report = await generateReport(req.query);
res.json(report);
});
app.listen(3000);x402() wrapper handles the entire challenge-verify-settle lifecycle. Drop it onto any Express route in seconds.SERVER_WALLET on-chain with every successful request.Any HTTP endpoint. Any community. 10 lines of code. Micropayments for everyone.