Skip to main content

What this does

l402-kit ships a built-in client that lets any AI agent — or any script — call L402-protected APIs without writing the payment loop yourself.
Agent  →  GET /api/data
API    →  402 + BOLT11 invoice
Agent  →  pay invoice (Blink / Alby)
Agent  →  GET /api/data  Authorization: L402 macaroon:preimage
API    →  200 ✓
Everything in between — invoice parsing, wallet call, retry — is handled automatically.

Node.js / TypeScript

1. Install

npm install l402-kit

2. Pick a wallet


Python

1. Install

pip install l402kit

2. Call the API


What happens step by step

  1. client.fetch / client.get sends the request without any auth header
  2. If the API returns 402, the client reads the invoice and macaroon from the response body (or WWW-Authenticate header)
  3. It checks your budget — throws BudgetExceededError if the price would exceed the limit
  4. It calls wallet.payInvoice(bolt11) and waits for the preimage
  5. It retries the request with Authorization: L402 <macaroon>:<preimage>
  6. Returns the final Response/httpx.Response to your code
No 402? The response is passed through untouched.

Spending report

// 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")

Next steps

Wallets

Blink, Alby — full setup and options

Budget control

Per-domain limits, callbacks, reports

MCP Server

Claude Desktop integration in 2 minutes

LangChain

Drop-in tool for LangChain agents