Skip to main content

Installation

Requirements: Node.js 18+, Express 4+

Quickstart


l402(options) — middleware

Returns an Express RequestHandler that enforces L402 payment on the route.

Options

Behavior

402 response body

WWW-Authenticate header


Providers

Alby — self-custodial wallet. You control the keys.

BTCPayProvider — self-hosted, zero trust

Run your own BTCPay Server. Full soberanoty.

BlinkProvider — custodial, easiest start

Blink — free, no KYC for small amounts.

LNbitsProvider

Self-hosted or legend.lnbits.com.

OpenNodeProvider

ManagedProvider — cloud mode (0.3% fee)

l402kit.com hosts the Lightning node. You receive 99.7% of each payment. Explicit opt-in.
Registration fires once at startup (fire-and-forget, errors are silent). The API appears at l402kit.com/apis.json so agents can discover it automatically.

Replay protection

Default — in-memory (development)

Built-in. Resets on restart. Suitable for single-process deployments.

Redis (production — multi-instance)

RedisReplayAdapter uses SET key 1 NX EX ttl — atomic, race-condition free.

Payment webhooks

Receive a signed event after each payment.
Webhook payload:

onPayment callback

Synchronous hook called after each verified payment, before next():

Supabase payment logging

Set SUPABASE_URL + SUPABASE_ANON_KEY in your environment to log payments automatically.
Payments table schema (payments):
payment_hash stores SHA256(preimage), not the raw preimage. The preimage is the 32-byte Lightning payment secret — its hash is already public in the BOLT11 invoice.

Standalone utilities


Types


Verification timing

Token verification runs SHA256(preimage) == paymentHash in memory — sub-millisecond, no network call on the hot path. The in-memory ReplayAdapter (default) also runs synchronously. If you use RedisReplayAdapter, add a 5–50 ms Redis round-trip per request. Plan capacity accordingly for high-frequency endpoints.

x402 compatibility (X-Payment header)

The middleware silently accepts the X-Payment header (used by Coinbase’s x402 protocol) in addition to the standard Authorization: L402 … header. Both are treated identically — useful if you want to serve clients that speak either protocol.
No configuration needed; it is always enabled.

Migration guide

v1.1 → v1.2

Rename column in your payments table: