Skip to main content

Official Python SDK for LnBot — Bitcoin for AI Agents. Send and receive sats over Lightning with a few lines of code.

Project description

ln.bot

PyPI version PyPI downloads Python License: MIT

The official Python SDK for ln.bot — Bitcoin for AI Agents.

Give your AI agents, apps, and services access to Bitcoin over the Lightning Network. Create wallets, send and receive sats, and get real-time payment notifications.

from lnbot import LnBot

ln = LnBot(api_key="key_...")

invoice = ln.invoices.create(amount=1000, memo="Coffee")
ln.payments.create(target="alice@ln.bot", amount=500)

ln.bot also ships a TypeScript SDK, Go SDK, Rust SDK, CLI, and MCP server.


Install

pip install lnbot

Quick start

1. Create a wallet

from lnbot import LnBot

ln = LnBot()
wallet = ln.wallets.create(name="my-agent")

print(wallet.primary_key)          # your API key
print(wallet.address)              # your Lightning address
print(wallet.recovery_passphrase)  # back this up!

2. Receive sats

ln = LnBot(api_key=wallet.primary_key)

invoice = ln.invoices.create(amount=1000, memo="Payment for task #42")
print(invoice.bolt11)

3. Wait for payment

for event in ln.invoices.watch(invoice.number):
    if event.event == "settled":
        print("Paid!")

4. Send sats

ln.payments.create(target="alice@ln.bot", amount=500)
ln.payments.create(target="lnbc10u1p...")

5. Check balance

wallet = ln.wallets.current()
print(f"{wallet.available} sats available")

Async support

Every method has an async equivalent via AsyncLnBot:

from lnbot import AsyncLnBot

async with AsyncLnBot(api_key="key_...") as ln:
    wallet = await ln.wallets.current()
    invoice = await ln.invoices.create(amount=1000)

    async for event in ln.invoices.watch(invoice.number):
        if event.event == "settled":
            print("Paid!")

Error handling

from lnbot import LnBot, BadRequestError, UnauthorizedError, NotFoundError, ConflictError, LnBotError

try:
    ln.payments.create(target="invalid", amount=100)
except BadRequestError:
    ...  # 400
except UnauthorizedError:
    ...  # 401
except NotFoundError:
    ...  # 404
except ConflictError:
    ...  # 409
except LnBotError as e:
    print(e.status, e.body)

Configuration

from lnbot import LnBot

ln = LnBot(
    api_key="key_...",            # or set LNBOT_API_KEY env var
    base_url="https://api.ln.bot",  # optional — this is the default
    timeout=30.0,                   # optional — request timeout in seconds
)

The API key can also be provided via the LNBOT_API_KEY environment variable. If both are provided, the constructor argument takes precedence.


L402 paywalls

Monetize APIs with Lightning-native authentication:

# Create a challenge (server side)
challenge = ln.l402.create_challenge(amount=100, description="API access", expiry_seconds=3600)

# Pay the challenge (client side)
result = ln.l402.pay(www_authenticate=challenge.www_authenticate)

# Verify a token (server side, stateless)
v = ln.l402.verify(authorization=result.authorization)
print(v.valid)

API reference

Wallets

Method Description
ln.wallets.create(name=) Create a new wallet (no auth required)
ln.wallets.current() Get current wallet info and balance
ln.wallets.update(name=) Update wallet name

Invoices

Method Description
ln.invoices.create(amount=, memo=, reference=) Create a BOLT11 invoice
ln.invoices.list(limit=, after=) List invoices
ln.invoices.get(number) Get invoice by number
ln.invoices.watch(number, timeout=) SSE stream for settlement/expiry

Payments

Method Description
ln.payments.create(target=, amount=, ...) Send sats to a Lightning address or BOLT11 invoice
ln.payments.list(limit=, after=) List payments
ln.payments.get(number) Get payment by number

Addresses

Method Description
ln.addresses.create(address=) Create a random or vanity Lightning address
ln.addresses.list() List all addresses
ln.addresses.delete(address) Delete an address
ln.addresses.transfer(address, target_wallet_key=) Transfer address to another wallet

Transactions

Method Description
ln.transactions.list(limit=, after=) List credit and debit transactions

Webhooks

Method Description
ln.webhooks.create(url=) Register a webhook endpoint (max 10)
ln.webhooks.list() List all webhooks
ln.webhooks.delete(webhook_id) Delete a webhook

API Keys

Method Description
ln.keys.rotate(slot) Rotate a key (0 = primary, 1 = secondary)

L402

Method Description
ln.l402.create_challenge(amount=, ...) Create an L402 challenge (invoice + macaroon)
ln.l402.verify(authorization=) Verify an L402 token (stateless)
ln.l402.pay(www_authenticate=, ...) Pay an L402 challenge, get Authorization header

Backup & Restore

Method Description
ln.backup.recovery() Generate 12-word BIP-39 recovery passphrase
ln.backup.passkey_begin() Start passkey backup (WebAuthn)
ln.backup.passkey_complete(session_id=, attestation=) Complete passkey backup
ln.restore.recovery(passphrase=) Restore wallet with recovery passphrase
ln.restore.passkey_begin() Start passkey restore (WebAuthn)
ln.restore.passkey_complete(session_id=, assertion=) Complete passkey restore

Requirements

  • Python 3.10+
  • Get your API key at ln.bot

Links

Other SDKs

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

lnbot-0.5.0.tar.gz (15.7 kB view details)

Uploaded Source

Built Distribution

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

lnbot-0.5.0-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file lnbot-0.5.0.tar.gz.

File metadata

  • Download URL: lnbot-0.5.0.tar.gz
  • Upload date:
  • Size: 15.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lnbot-0.5.0.tar.gz
Algorithm Hash digest
SHA256 06a404404a3efcd882e33a568e218267aed56b2e809dd35d53a16653153f0648
MD5 aa7ed046048e49e7e1efe3d90fdc195f
BLAKE2b-256 169289f088a7a4aca6361f8ffded25cf3b864ce9cccca48897888df964d4c686

See more details on using hashes here.

File details

Details for the file lnbot-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: lnbot-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for lnbot-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 805cbd05ddac20f1948de2b99e99483556c99ba0bcb853ddc64c5747b6b8865d
MD5 a0745b924c6db6e138f221ccf53b307a
BLAKE2b-256 8f2d7f8273fe87d1f475e55e74eb0cf380c1f802941b80eb7cea178167a5eff4

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