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.1.2

Published

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

Downloads

103

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. See Lightning Labs' announcement for context.

Server — Paywall any Express route

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

const app = express();

const node = {
  restHost: 'https://127.0.0.1:8082',
  macaroon: process.env.LND_MACAROON!,
  skipTlsVerify: true, // dev only
};

// This route now requires a Lightning payment of 100 sats
app.get('/api/data', l402({ node, price: 100 }), (req, res) => {
  res.json({ secret: 'You paid 100 sats for this.' });
});

app.listen(3000);

That's it. 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!,
    skipTlsVerify: true,
  },
  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 for reuse.

Dynamic Pricing

Price requests based on content, user, or complexity:

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                         LND Node
  |                                |                              |
  |  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                    |                              |
  |<-------------------------------|                              |

The key insight: verification is cryptographic, not database-driven. The server checks sha256(preimage) === payment_hash — if it matches, payment is mathematically proven. No server-side state. No payment lookups. This is what makes L402 work for distributed systems and AI agents.

For AI Agent Developers

L402 is the emerging standard for machine-to-machine payments. This package gives your agents the ability to:

  • Sell services: Wrap your agent's capabilities in an Express API, paywall it with l402(), and any other agent can pay to use it.
  • Buy services: Use createL402Client to give your agent a wallet that auto-pays for resources it discovers.
  • Interoperate: Compatible with Lightning Labs' lightning-agent-tools and the broader L402 ecosystem.

API Reference

l402(config)

Express middleware that paywalls a route.

| Option | Type | Description | |--------|------|-------------| | node | LndConfig | LND connection (restHost, macaroon) | | price | number | Price in satoshis | | description | string? | Human-readable description | | priceFn | (req) => number | Dynamic pricing function |

createL402Client(config)

Creates an L402-aware HTTP client.

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

Returns { fetch, clearCache, cacheSize }.

LndConfig

| Field | Type | Description | |-------|------|-------------| | restHost | string | LND REST API URL | | macaroon | string | Admin macaroon (hex) | | skipTlsVerify | boolean? | Skip TLS verification (dev only) |

Development

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

# Use Polar (https://lightningpolar.com) for local Lightning Network
# Start a network with 2+ LND nodes

# Run the example server
npm run dev:server

# Run the example client (separate terminal)
npm run dev:client

License

MIT