构建支持 Lightning Network 微支付 (L402) 的 npm 包

发布: (2025年12月12日 GMT+8 10:17)
2 min read
原文: Dev.to

Source: Dev.to

Overview

我刚刚发布了第一个使用闪电网络进行微支付的 npm 包:lightning-bitcoin-query

传统的 API 服务每月收费 $29‑199,提供无限请求,但大多数开发者每天只需要几次 API 调用。为何要为未使用的配额付费?

Pay‑per‑query with Lightning Network micropayments

  • 10 sats(约 $0.01)每次 mempool 检查
  • 20 sats(约 $0.02)每次交易查询
  • 无需 API 密钥
  • 无速率限制
  • 无订阅费用

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. 尝试 API 请求
  2. 收到 402 Payment Required,其中包含闪电发票
  3. 通过 NWC 钱包自动支付发票
  4. 缓存支付哈希,有效期 5 分钟
  5. 携带支付凭证重新请求
  6. 返回区块链数据
  • 即时:支付在

还有哪些 API 可以受益于微支付?在评论区告诉我吧!

Back to Blog

相关文章

阅读更多 »