Skip to main content

功能说明

l402-kit 内置了一个客户端,让任何 AI 智能体——或任意脚本——无需自行编写支付逻辑,即可调用受 L402 保护的 API。
Agent  →  GET /api/data
API    →  402 + BOLT11 invoice
Agent  →  pay invoice (Blink / Alby)
Agent  →  GET /api/data  Authorization: L402 macaroon:preimage
API    →  200 ✓
其中所有环节——发票解析、钱包调用、重试请求——均由客户端自动处理。

Node.js / TypeScript

1. 安装

npm install l402-kit

2. 选择钱包


Python

1. 安装

pip install l402kit

2. 调用 API


逐步执行流程

  1. client.fetch / client.get 发送不携带任何认证头的请求
  2. 若 API 返回 402,客户端从响应体(或 WWW-Authenticate 头)中读取 invoicemacaroon
  3. 检查您的预算——若支付金额超出限制,则抛出 BudgetExceededError
  4. 调用 wallet.payInvoice(bolt11) 并等待获取 preimage
  5. 携带 Authorization: L402 <macaroon>:<preimage> 重试请求
  6. 将最终的 Response/httpx.Response 返回给您的代码
若未收到 402 响应,则直接透传原始响应,不做任何处理。

消费报告

// TypeScript
const report = client.spendingReport();
// { total: 42, remaining: 958, byDomain: { "api.example.com": 42 }, transactions: [...] }
# Python
report = client.spending_report()
print(f"Spent {report.total} sats, {report.remaining} remaining")

后续步骤

钱包

Blink、Alby——完整配置说明与选项

预算控制

按域名限额、回调函数、消费报告

MCP Server

2 分钟完成 Claude Desktop 集成

LangChain

LangChain 智能体的即插即用工具