Building AI Agents That Pay for Their Own Data
The autonomous data problem
You're building an AI agent. It needs data from external APIs: market prices, weather, legal documents, satellite imagery. Each API has its own signup process, API key format, and billing model.
Managing 20 different API accounts is annoying for a human developer. For an autonomous agent, it's a showstopper.
L402 as the universal payment layer
With L402, your agent needs exactly one thing: a Lightning wallet. Every L402-enabled API works the same way:
- Request data
- Receive 402 + Lightning invoice
- Pay invoice
- Retry with proof of payment
- Receive data
No signups. No API keys. No billing accounts.
Setting up your agent
Install the bolthub Python client:
pip install bolthubInitialize the client with a Lightning wallet and a budget:
from bolthub import L402Client, LndWallet
wallet = LndWallet(host="https://your-node:8080", macaroon="...")client = L402Client(wallet, budget_sats=50_000)The budget_sats parameter prevents runaway spending. The client will refuse to pay once the budget is exhausted.
Fetching paid data
Use the client exactly like requests:
resp = client.get( "https://acme.gw.bolthub.ai/v1/market-data", params={"symbol": "BTC"})data = resp.json()print(f"BTC price: {data['price']}")# Budget remaining: client.budget_remaining_satsThe client automatically:
- Detects 402 responses
- Parses the Lightning invoice from the WWW-Authenticate header
- Pays the invoice via your wallet
- Retries the request with the L402 token
- Tracks spending against your budget
Multi-source aggregation
The real power emerges when your agent needs data from multiple sources:
sources = [ "https://weather.gw.bolthub.ai/v1/forecast?city=NYC", "https://finance.gw.bolthub.ai/v1/stock?ticker=AAPL", "https://news.gw.bolthub.ai/v1/headlines?topic=tech",]
results = []for url in sources: resp = client.get(url) results.append(resp.json())One wallet. Three different APIs. Zero signups.
Session-based access
For APIs using token buckets or time passes, the client manages sessions automatically:
# First request pays and creates a sessionresp = client.get("https://data.gw.bolthub.ai/v1/stream")
# Subsequent requests reuse the session tokenfor i in range(49): resp = client.get("https://data.gw.bolthub.ai/v1/stream")# Session token is sent via X-Session-Token header automaticallyIntegration with LangChain
Use L402 as a tool in your LangChain agent:
from langchain.tools import Toolfrom bolthub import L402Client, LndWallet
wallet = LndWallet(host="https://your-node:8080", macaroon="...")client = L402Client(wallet, budget_sats=10_000)
market_data_tool = Tool( name="market_data", description="Fetch real-time market data for a given symbol", func=lambda symbol: client.get( f"https://acme.gw.bolthub.ai/v1/market-data?symbol={symbol}" ).json())Your LangChain agent can now autonomously fetch and pay for market data as part of its reasoning chain.
What's next
- Browse the API Hub to discover available L402 endpoints
- Read the SDK documentation for advanced configuration
- Start your free trial to list your own API