DEV Community

Rod Millz
Rod Millz

Posted on

Building an npm Package with Lightning Network Micropayments (L402)

I just published my first npm package that uses the Lightning Network for micropayments: lightning-bitcoin-query.

The Problem

Traditional API services charge $29-199/month for unlimited requests. But most developers only need a few API calls per day. Why pay for unused quota?

The Solution

Pay-per-query with Lightning Network micropayments:

  • 10 sats (~$0.01) per mempool check
  • 20 sats (~$0.02) per transaction lookup
  • No API keys
  • No rate limits
  • No subscriptions

How It Works

The package uses the L402 protocol (HTTP 402 Payment Required + Lightning):

const BitcoinQuery = require('lightning-bitcoin-query');

const bitcoin = new BitcoinQuery({
  nwcUri: process.env.NWC_URI // From your Lightning wallet
});

// Makes HTTP request → Gets 402 → Pays invoice → Returns data
const mempool = await bitcoin.getMempool();
Enter fullscreen mode Exit fullscreen mode

Behind the scenes:

  1. Try API request
  2. Receive 402 Payment Required with Lightning invoice
  3. Pay invoice automatically via NWC wallet
  4. Cache payment hash for 5 minutes
  5. Retry request with payment proof
  6. Return blockchain data

Why Lightning?

  • Instant: Payments confirm in <1 second
  • Cheap: Network fees are <1 sat
  • Global: Works worldwide, no credit cards
  • Private: No personal info required

Use Cases

Perfect for:

  • Trading bots needing real-time fee data
  • Hobby projects with occasional queries
  • Startups testing blockchain integrations
  • Anyone who wants pay-as-you-go APIs

Try It

npm install lightning-bitcoin-query
Enter fullscreen mode Exit fullscreen mode

Full docs: https://www.npmjs.com/package/lightning-bitcoin-query

What other APIs would benefit from micropayments? Let me know in the comments!

Top comments (0)