Skip to main content

LangChain Integration

Add Lightning payment capability to any LangChain agent. Your agent can access L402-protected APIs with automatic micropayments.

Install

pip install l402-requests[langchain]

Set your wallet:

export STRIKE_API_KEY="your-strike-api-key"

Quick Start

from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent

from l402_requests.integrations.langchain import L402FetchTool, L402SpendingTool

# Create tools
tools = [L402FetchTool(), L402SpendingTool()]

# Create agent
llm = ChatOpenAI(model="gpt-4o")
agent = create_react_agent(llm, tools)

# Run
result = agent.invoke({
"messages": [("user", "Get the weather forecast for NYC from agent-commerce.store")]
})

The agent will call l402_fetch, automatically pay the Lightning invoice when it gets a 402, and return structured data.

Tools

L402FetchTool

HTTP GET or POST with automatic L402 payment handling.

from l402_requests import L402Client, BudgetController
from l402_requests.integrations.langchain import L402FetchTool

# Custom budget
client = L402Client(
budget=BudgetController(
max_sats_per_request=500,
max_sats_per_hour=5000,
)
)

tool = L402FetchTool(client=client)

Parameters:

  • url (str) — The full URL to request
  • method (str) — GET or POST (default: GET)
  • body (str, optional) — JSON string body for POST requests

L402SpendingTool

Check how many sats have been spent in this session.

from l402_requests.integrations.langchain import L402SpendingTool

tool = L402SpendingTool(client=client) # Share the same client

Shared Client

Pass the same L402Client to all tools so they share credential cache and budget:

from l402_requests import L402Client, BudgetController
from l402_requests.integrations.langchain import L402FetchTool, L402SpendingTool

client = L402Client(
budget=BudgetController(max_sats_per_request=1000),
)

tools = [
L402FetchTool(client=client),
L402SpendingTool(client=client),
]

Wallet Options

The wallet is auto-detected from environment variables:

PriorityWalletEnvironment Variable
1LNDLND_REST_HOST + LND_MACAROON_HEX
2NWCNWC_CONNECTION_STRING
3StrikeSTRIKE_API_KEY
4OpenNodeOPENNODE_API_KEY

Recommended: Strike — full L402 support, no infrastructure required.