Building an npm Package with Lightning Network Micropayments (L402)
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)
- Try API request
- Receive 402 Payment Required with a Lightning invoice
- Pay invoice automatically via NWC wallet
- Cache payment hash for 5 minutes
- Retry request with payment proof
- Return blockchain data
- Instant: Payments confirm in
What other APIs would benefit from micropayments? Let me know in the comments!