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-express

v0.1.0

Published

Express middleware for L402 — drop one line into any Express API to charge per-request Lightning payments. Built on top of l402-server.

Readme

l402-express

npm License: MIT

Express middleware for L402. Drop one line into any Express API to charge per-request Lightning payments. Built on l402-server — Lightning Enable handles invoices, macaroons, and payment verification; your API stays where it is.

Install

npm install l402-express l402-server

Node 18+. Express 4 or 5. ESM + CJS dual exports.

30-second example

import express from "express";
import { l402 } from "l402-express";

const app = express();

// Gate everything under /api/premium behind a 100 sat L402 payment.
app.use("/api/premium", l402({
  apiKey: process.env.LIGHTNING_ENABLE_API_KEY!,
  priceSats: 100,
}));

app.get("/api/premium/weather", (_req, res) => {
  res.json({ temp: 72 });
});

app.listen(3000);

That's it. Three lines of integration code. The middleware handles:

  • Issuing a 402 Payment Required with a Lightning invoice on unauthenticated requests
  • Verifying the Authorization: L402 <macaroon>:<preimage> header on retries
  • Passing the verified credential metadata to downstream handlers via res.locals.l402

Variable per-request pricing

Pass a function to priceSats to derive the price from the request:

app.use("/api/llm", l402({
  apiKey: process.env.LIGHTNING_ENABLE_API_KEY!,
  priceSats: (req) => req.query.model === "premium" ? 500 : 100,
}));

Functions can return number or Promise<number>. Same applies to resource and description.

Configuration

| Option | Type | Default | Notes | |---|---|---|---| | apiKey | string | (one of apiKey or client is required) | Your Lightning Enable merchant API key | | client | L402Server | — | Pre-constructed SDK client (use to share across mounts, inject custom fetch, etc.) | | priceSats | number \| (req) => number \| Promise<number> | required | Price in satoshis, ≥ 1 | | resource | string \| (req) => string \| Promise<string> | req.path | Bound as a macaroon caveat | | description | string \| (req) => string \| undefined | none | Shown to the payer in their wallet | | idempotencyKey | (req) => string \| undefined | client IP | Send X-Idempotency-Key to dedup challenges within the invoice expiry window | | baseUrl | string | https://api.lightningenable.com | Override producer API URL (testing) | | onInvalidToken | (req, failure) => void \| Promise<void> | sends 401 | Custom handler for verification failures — useful for sending a fresh 402 instead of 401 |

Verified credential on downstream handlers

After a successful verification the middleware sets res.locals.l402 so downstream handlers can inspect what was paid for:

app.get("/api/premium/weather", (_req, res) => {
  const { resource, amountSats, paymentHash } = res.locals.l402;
  console.log(`Served ${resource} for ${amountSats} sats (${paymentHash})`);
  res.json({ temp: 72 });
});

How the protocol works under the hood

Every paid request takes one round-trip to the Lightning Enable hosted API. The middleware never holds key material, never signs macaroons, never verifies preimages locally. All of that is in the hosted backend so:

  • The L402 root key stays in Lightning Enable's infrastructure
  • Consumed preimages are tracked centrally — replay protection works across all your endpoints automatically
  • Protocol upgrades happen server-side without client updates

What you're paying for with your Lightning Enable subscription: the protocol broker that lets this be one line of middleware. The middleware itself is ~150 lines of HTTP glue.

Two integration modes

Lightning Enable supports two integration shapes:

  • Proxy mode — point Lightning Enable at your API URL; we forward authenticated requests on your behalf. Best for public APIs or quick experiments.
  • Native mode — install this middleware in your existing API. Lightning Enable handles payment; your API handles everything else. Best for commercial APIs with their own auth, observability, or sensitive infrastructure. This middleware is the Native mode for Express.

Documentation site →

Sibling packages

  • l402-server — the SDK this middleware is built on. Use it directly if you're not on Express, or to share a single client across multiple middleware mounts.
  • l402-requests — consumer-side HTTP client. Auto-pays L402 challenges. Use this to call APIs gated by l402-express.

Contributing

Open source under MIT. Issues and pull requests welcome.

License

MIT © Refined Element, LLC