Skip to main content

bolt402-langchain

LangChain tools for L402 Lightning payments

PyPI PyPI downloads MIT or Apache-2.0 Licensed

Enable AI agents to autonomously pay for L402-gated APIs using the Lightning Network.

Install

pip install bolt402-langchain

Quick Start

from bolt402_langchain import create_l402_client, L402FetchTool, L402BudgetTool

# Create a client backed by LND REST
client = create_l402_client(
    backend="lnd",
    url="https://localhost:8080",
    macaroon="hex-encoded-admin-macaroon",
    budget={"per_request_max": 500, "daily_max": 5000},
)

# Create LangChain tools
fetch_tool = L402FetchTool(client=client)
budget_tool = L402BudgetTool(client=client)

# Use directly
result = fetch_tool.invoke("https://api.example.com/data")
print(result)
# [Paid 100 sats (+0 fee) | status 200]
# {"ok": true}

# Check spending
print(budget_tool.invoke(""))
# Total spent: 100 sats across 1 payment(s).

Tools

L402FetchTool

Makes HTTP requests with automatic L402 payment handling.

Input: URL string (GET) or JSON {"url": "...", "body": "..."} (POST)

# GET
result = fetch_tool.invoke("https://api.example.com/data")

# POST
result = fetch_tool.invoke('{"url": "https://api.example.com/data", "body": "{\"query\": \"BTC\"}"}')

L402BudgetTool

Reports spending: total sats, receipt count, per-endpoint breakdown.

result = budget_tool.invoke("")
# Total spent: 250 sats across 2 payment(s).
#
#   #1: https://api.example.com/weather — 50 sats (+0 fee, status 200, 5ms)
#   #2: https://api.example.com/market — 200 sats (+0 fee, status 200, 3ms)

Callbacks

PaymentCallbackHandler hooks into LangChain's callback system for payment observability:

from bolt402_langchain import PaymentCallbackHandler

handler = PaymentCallbackHandler(
    on_payment=lambda event: print(f"Paid {event.amount_sats} sats"),
    max_payment_alert=500,
    on_alert=lambda event: print(f"ALERT: {event.amount_sats} sats!"),
)

# Pass to LangChain agent
executor.invoke({"input": "..."}, config={"callbacks": [handler]})

Lightning Backends

from bolt402_langchain import create_l402_client

# LND REST
client = create_l402_client(backend="lnd", url="https://localhost:8080", macaroon="...")

# Core Lightning (CLN) REST
client = create_l402_client(backend="cln", url="https://localhost:3010", rune="...")

# SwissKnife
client = create_l402_client(backend="swissknife", url="https://app.numeraire.tech", api_key="sk-...")

With LangChain Agents

from langchain_openai import ChatOpenAI
from langchain.agents import create_tool_calling_agent, AgentExecutor
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from bolt402_langchain import create_l402_client, L402FetchTool, L402BudgetTool

client = create_l402_client(
    backend="lnd",
    url="https://localhost:8080",
    macaroon="deadbeef...",
    budget={"per_request_max": 500},
)

tools = [L402FetchTool(client=client), L402BudgetTool(client=client)]
llm = ChatOpenAI(model="gpt-4o")

prompt = ChatPromptTemplate.from_messages([
    ("system", "You can fetch data from L402-gated APIs. "
               "Use l402_fetch for data, l402_check_budget for spending."),
    ("human", "{input}"),
    MessagesPlaceholder("agent_scratchpad"),
])

agent = create_tool_calling_agent(llm, tools, prompt)
executor = AgentExecutor(agent=agent, tools=tools)
response = executor.invoke({"input": "Get weather data, then report costs."})

License

MIT OR Apache-2.0

Metadata

Release files for bolt402-langchain 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bolt402-langchain 0.1.0
File Size Uploaded
bolt402_langchain-0.1.0.tar.gz 9.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bolt402-langchain 0.1.0
File Interpreter ABI Platform
bolt402_langchain-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 18.4 kB

Release files / bolt402_langchain-0.1.0.tar.gz

Download URL bolt402_langchain-0.1.0.tar.gz
Size 9.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d296cfabc69d92d72c96c2201ef5dea8deb821be5d54866312e06e487df3cc1c
BLAKE2b-256 checksum
How to use checksums
1272b93210e75bf90a6f47349af2c3792490bb0f56a34bb7dec43d5a605cce29
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / bolt402_langchain-0.1.0-py3-none-any.whl

Download URL bolt402_langchain-0.1.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4b94b9cf7f58d38d154283750379d0c4d6f6ed711ddaeceb5468f00fa1c88998
BLAKE2b-256 checksum
How to use checksums
03d47d76bcb3fa82cc71cb29cc26eb22b2d8ef50718c575a83adc177ddb971bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page