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サーバー

2分でClaude Desktopと連携

LangChain

LangChainエージェント向けのドロップインツール