npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

l402-js

v0.3.0

Published

Lightning L402 protocol for Express and Node.js — paywall any API with Bitcoin micropayments

Readme

l402-js

Lightning L402 protocol for Express and Node.js. Paywall any API with Bitcoin micropayments in three lines of code.

npm install l402-js

What is L402?

L402 uses the HTTP 402 Payment Required status code to create pay-per-request APIs. When a client hits a protected endpoint, the server returns a Lightning invoice. The client pays, gets a cryptographic proof (preimage), and retries with that proof. No accounts, no API keys, no credit cards.

This is how AI agents will pay for services.


Server — Paywall any Express route

With LND

import express from 'express';
import { l402 } from 'l402-js';

const app = express();

app.get('/api/data', l402({
  node: {
    restHost: 'https://127.0.0.1:8082',
    macaroon: process.env.LND_MACAROON!,
    skipTlsVerify: true, // dev only
  },
  price: 100,
}), (req, res) => {
  res.json({ secret: 'You paid 100 sats for this.' });
});

app.listen(3000);

With Alby Hub or any NWC wallet

No LND node required. Use any NWC-compatible wallet (Alby Hub, Zeus, etc.):

app.get('/api/data', l402({
  nwc: { connectionString: process.env.NWC_CONNECTION_STRING! },
  price: 100,
}), handler);

Any request without a valid L402 token gets a 402 response with a Lightning invoice. Pay the invoice, retry with the proof, get the data.


Client — Auto-pay L402 invoices

import { createL402Client } from 'l402-js';

const client = createL402Client({
  node: {
    restHost: 'https://127.0.0.1:8081',
    macaroon: process.env.LND_MACAROON!,
  },
  maxAutoPaySats: 1000, // safety limit
});

const result = await client.fetch('https://api.example.com/data');
console.log(result.data);      // { secret: 'You paid 100 sats for this.' }
console.log(result.paid);      // true
console.log(result.price);     // 100
console.log(result.preimage);  // 'a1b2c3d4...'

client.fetch works like regular fetch but automatically detects 402 responses, pays the Lightning invoice, and retries with proof. Tokens are cached per URL.


Proxy — Paywall any existing HTTP backend

Zero changes to your backend. Point the proxy at it and every request requires a Lightning payment:

import { createL402Proxy } from 'l402-js';

createL402Proxy({
  target: 'http://localhost:3000',
  price: 10,
  node: { restHost: '...', macaroon: '...' },
  // or: nwc: { connectionString: '...' }
}).listen(8402);

Or use the CLI binary — no code required:

npx l402-proxy \
  --target http://localhost:3000 \
  --price 10 \
  --lnd-host https://127.0.0.1:8082 \
  --macaroon <hex>

Dynamic Pricing

app.post('/api/compute', l402({
  node,
  price: 50,                              // fallback
  priceFn: (req) => req.body.tokens * 2, // 2 sats per token
}), handler);

How It Works

Client                          Server                         Wallet
  |                                |                              |
  |  GET /api/data                 |                              |
  |------------------------------->|                              |
  |                                |  Create invoice (100 sats)   |
  |                                |----------------------------->|
  |                                |  invoice + payment_hash      |
  |                                |<-----------------------------|
  |  402 + invoice + macaroon      |                              |
  |<-------------------------------|                              |
  |                                                               |
  |  Pay invoice                                                  |
  |-------------------------------------------------------------->|
  |  preimage (proof of payment)                                  |
  |<--------------------------------------------------------------|
  |                                                               |
  |  GET /api/data                 |                              |
  |  Authorization: L402 mac:pre   |                              |
  |------------------------------->|                              |
  |                                |  sha256(preimage) == hash?   |
  |                                |  ✓ Verified (pure math)      |
  |  200 + data                    |                              |
  |<-------------------------------|                              |

Verification is cryptographic, not database-driven. The server checks sha256(preimage) === payment_hash — pure math, no payment lookups, no server-side state.


API Reference

l402(config) — middleware

| Option | Type | Required | Description | |--------|------|----------|-------------| | node | LndConfig | One of node/nwc | LND REST API connection | | nwc | NwcConfig | One of node/nwc | NWC wallet connection | | price | number | ✓ | Price in satoshis | | description | string | | Human-readable description shown in 402 | | priceFn | (req) => number \| Promise<number> | | Dynamic pricing (overrides price) |

createL402Client(config) — auto-paying client

| Option | Type | Description | |--------|------|-------------| | node | LndConfig | LND connection for paying invoices | | maxAutoPaySats | number | Max auto-pay limit in sats (default: 10000) |

Returns { fetch, clearCache, cacheSize }.

createL402Proxy(config) — reverse proxy

| Option | Type | Description | |--------|------|-------------| | target | string | Backend URL to proxy to | | node | LndConfig | LND connection | | nwc | NwcConfig | NWC connection | | price | number | Price per request in sats | | description | string | Description shown in 402 challenge | | priceFn | (req) => number | Dynamic pricing |

LndConfig

| Field | Type | Description | |-------|------|-------------| | restHost | string | LND REST API URL, e.g. https://127.0.0.1:8082 | | macaroon | string | Admin macaroon in hex | | skipTlsVerify | boolean | Skip TLS verification — dev only |

NwcConfig

| Field | Type | Description | |-------|------|-------------| | connectionString | string | nostr+walletconnect:// connection string from your NWC wallet |


Development

git clone https://github.com/smitsyaboi/l402-js
cd l402-js && npm install
npm test

License

MIT