import { BTCPayProvider } from 'l402-kit';const lightning = new BTCPayProvider( process.env.BTCPAY_URL!, // https://your-btcpay.com process.env.BTCPAY_API_KEY!, // store API key process.env.BTCPAY_STORE_ID!, // store ID);
import { OpenNodeProvider } from 'l402-kit';const lightning = new OpenNodeProvider( process.env.OPENNODE_API_KEY!, false, // testMode — set to true for sandbox);
l402kit.com hosts the Lightning node. You receive 99.7% of each payment. Explicit opt-in.
import { ManagedProvider } from 'l402-kit';const lightning = ManagedProvider.fromAddress('you@yourdomain.com');// Optional: auto-register in the public API directoryconst lightning = ManagedProvider.fromAddress('you@yourdomain.com', { registerDirectory: { url: 'https://api.you.com/v1/data', name: 'My Data API', priceSats: 10, category: 'data', // data | ai | finance | weather | compute | storage | other description: 'Optional', },});
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.
Set SUPABASE_URL + SUPABASE_ANON_KEY in your environment to log payments automatically.
app.get('/api', l402({ priceSats: 10, lightning }), handler);// SUPABASE_URL and SUPABASE_ANON_KEY are read from process.env automatically
Payments table schema (payments):
create table payments ( id uuid primary key default gen_random_uuid(), payment_hash text unique not null, -- SHA256(preimage) — safe to store endpoint text, amount_sats integer, paid_at timestamptz default now());
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.
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.
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.
// Client using x402 header — handled transparently// X-Payment: <macaroon>:<preimage>