Building an npm Package with Lightning Network Micropayments (L402)

Published: (December 11, 2025 at 09:17 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

Overview

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

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?

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

Installation

npm install lightning-bitcoin-query

Usage

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();

How It Works (L402 Protocol)

  1. Try API request
  2. Receive 402 Payment Required with a 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
  • Instant: Payments confirm in

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

Back to Blog

Related posts

Read more »