v0.1.0 · Open Source / MIT

Every 402 response,
handled.

Universal payment SDK for AI agents, MCP servers, and automated pipelines. Bridges L402 (Lightning) and x402 (USDC) transparently.

Why pay402

One SDK for every
paid API call.

HTTP 402 “Payment Required” is how the web signals that an endpoint costs money, but there's no standard way to handle it. pay402 bridges that gap.

Autonomous Payments

AI agents and automated pipelines can pay for API access on their own, no human in the loop. Built-in spend limits keep budgets safe.

Two Protocols, One Interface

L402 (Lightning) and x402 (USDC on Base & Solana) are competing standards. pay402 supports both so your code doesn't need to pick one.

Spend Safety Built In

Per-request caps, hourly rolling windows, and daily budgets. URL allowlists and dry-run mode. Ship agents that spend real money with confidence.

Quick Start

Three lines.
That's the integration.

Configure wallets via environment variables. The client auto-detects available rails, handles 402 challenges, pays, retries, and caches tokens.

Your code never sees the 402
client.ts
import { fromEnv } from "pay402";
const client = fromEnv();
const res = await client.fetch(
"https://api.example.com/premium-data"
);
const data = await res.json();

Capabilities

Everything you need.
Nothing you don't.

Drop-in fetch()

Replace fetch with pay402 and every 402 response is handled automatically. Parse, pay, retry, cache. Zero boilerplate.

Multi-Rail Payments

Lightning Network (L402) and USDC on Base or Solana (x402). One SDK, both protocols, automatic rail selection.

Spend Controls

Per-request, hourly, and daily limits. URL allowlists and denylists. Dry-run mode. Built for autonomous agents that spend real money.

Token Caching

Payment proofs are cached and reused automatically. No double-paying for the same endpoint within the token's TTL.

Express Middleware

Gate your API routes behind 402 payments. Multi-rail challenge headers, proof verification, and payment callbacks built in.

MCP Tool Wrapper

Wrap MCP server tools with payment gates. No changes to tool implementations. The wrapper handles challenges and proof verification.

Under the Hood

How it works

The SDK intercepts 402 responses, selects the optimal payment rail, executes payment, and retries with proof. All in one round-trip.

Request
GET /api/data
402
Payment Required
Pay
L402 or x402
Retry
With proof
200
Access granted

L402 / Lightning

Bitcoin Lightning Network with Macaroon authentication. Pay BOLT11 invoices, receive preimage proofs. Sub-second settlement.

WWW-Authenticate: L402 macaroon="...", invoice="..."
$

x402 / USDC

Stablecoins on Base and Solana. EIP-3009 signed authorizations or direct SPL token transfers. Dollar-denominated pricing.

X-Payment-Required: {"scheme":"exact",...}