Skip to main content

L402 client for AI agents — pay Lightning invoices automatically to access paywalled APIs

Project description

bolthub

L402 client for AI agents. Automatically handles 402 Payment Required challenges, pays Lightning invoices, and retries requests with proof of payment.

Install

pip install bolthub

Quick Start

from bolthub import L402Client, PhoenixdWallet

wallet = PhoenixdWallet(
    url="https://your-phoenixd:9740",
    password="your-phoenixd-password",
)

client = L402Client(wallet, budget_sats=10_000)

resp = client.get(
    "https://acme.gw.bolthub.ai/v1/market-data",
    params={"symbol": "BTC"},
)
data = resp.json()

Wallet Adapters

Phoenixd (recommended)

Fastest payment speed (<200ms). Get a hosted instance at nodana.io or self-host from ACINQ.

from bolthub import PhoenixdWallet

wallet = PhoenixdWallet(
    url="https://your-phoenixd:9740",
    password="your-phoenixd-password",
    timeout_seconds=35,
)

LND

Full Lightning node. Self-host or use Umbrel / Start9.

from bolthub import LndWallet

wallet = LndWallet(
    host="https://your-lnd-node:8080",
    macaroon="admin-macaroon-hex",
    timeout_seconds=30,
)

For agent deployments, use a scoped pay-only macaroon instead of admin.macaroon:

lncli bakemacaroon uri:/lnrpc.Lightning/SendPaymentSync \
  uri:/lnrpc.Lightning/DecodePayReq \
  --save_to=pay-only.macaroon

LNbits

Lightweight Lightning wallet with multi-wallet support. Create a dedicated wallet for your agent.

from bolthub import LnbitsWallet

wallet = LnbitsWallet(
    url="https://lnbits.example.com",
    admin_key="your-admin-key",
)

NWC (Nostr Wallet Connect)

Easiest to set up but slower (1-3s per payment). Get a free NWC connection from CoinOS or use Alby Hub.

from bolthub import NwcWallet

# Provide a pay function that handles the NWC protocol.
# With pynostr or another NWC library:
def pay_via_nwc(bolt11: str) -> str:
    # your NWC payment logic here
    return preimage_hex

wallet = NwcWallet(pay_fn=pay_via_nwc)

Custom Wallet

Implement the WalletAdapter protocol:

class MyWallet:
    def pay_invoice(self, bolt11: str) -> str:
        preimage = my_payment_logic(bolt11)
        return preimage

Budget Guards

client = L402Client(
    wallet,
    max_per_request_sats=100,  # reject invoices over 100 sats
    budget_sats=10_000,         # total spending cap
)

print(client.total_spent)       # sats spent so far
print(client.remaining_budget)  # sats remaining

Session Persistence

By default sessions are kept in memory. Use FileSessionStore to persist tokens across process restarts (stored in ~/.bolthub/sessions.json):

from bolthub import L402Client, PhoenixdWallet, FileSessionStore

client = L402Client(
    PhoenixdWallet(url=url, password=pw),
    session_store=FileSessionStore(),
)

API Reference

Export Description
L402Client HTTP client with automatic L402 challenge handling
LndWallet Wallet adapter for LND REST API
LnbitsWallet Wallet adapter for LNbits
PhoenixdWallet Wallet adapter for Phoenixd
NwcWallet Wallet adapter accepting a custom pay callback
WalletAdapter Protocol to implement for custom wallets
FileSessionStore Disk-backed session token persistence
SessionStore Protocol for custom session storage
L402Error Base exception for L402 failures
L402BudgetError Raised when budget limits are exceeded

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bolthub-0.1.0.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bolthub-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file bolthub-0.1.0.tar.gz.

File metadata

  • Download URL: bolthub-0.1.0.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for bolthub-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8a7f8e40dcb056b0e3876c980b36bcf1f2ffcc476f79dc0eb9d0b08805f623fc
MD5 afd631f806498d582cc05eaa3aecebc2
BLAKE2b-256 acc2ecfd05a1e2c89c0079590a99d0f6e62e37229d83204f298c71c242212028

See more details on using hashes here.

File details

Details for the file bolthub-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bolthub-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for bolthub-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f407fd5bf8af3a9fe90ac8a47a0f372feca4ee557bd80584f82b8ee22b5d657
MD5 e6624788f756fea744e5075ed0383518
BLAKE2b-256 d57432c6f5c14ed34ba442c99fc8654915fd75e61e42371463c36eab551b02ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page