Python SDK for SatGate - Automatic L402 payments for AI Agents. The Stripe Moment.
Project description
SatGate Python SDK
Give your AI agents a Lightning wallet in 2 lines of code.
Automatic L402 payment handling — the "Stripe Moment" for autonomous agents.
Installation
pip install satgate
Quick Start
from satgate import SatGateClient, LNBitsWallet
# 1. Connect your wallet
wallet = LNBitsWallet(
url="https://legend.lnbits.com",
admin_key="your-admin-key"
)
# 2. Create client
client = SatGateClient(wallet)
# 3. That's it. 402 → Pay → Retry happens automatically.
response = client.get("https://api.example.com/premium/data")
print(response.json())
What Happens Under the Hood
1. GET /premium/data
↓
2. Server returns 402 + Lightning Invoice
↓
3. SDK automatically pays invoice
↓
4. SDK retries with L402 token
↓
5. You get the response ✓
Wallet Options
LNBits
from satgate import LNBitsWallet
wallet = LNBitsWallet(
url="https://legend.lnbits.com", # or your own instance
admin_key="your-admin-key"
)
Alby
from satgate import AlbyWallet
wallet = AlbyWallet(access_token="your-alby-token")
Custom Wallet
Implement the LightningWallet interface:
from satgate import LightningWallet
class MyWallet(LightningWallet):
def pay_invoice(self, invoice: str) -> str:
# Connect to your LND, CLN, etc.
preimage = my_node.pay(invoice)
return preimage # hex string
Features
Token Caching
Tokens are cached by default to avoid paying twice:
client = SatGateClient(wallet, cache_tokens=True, cache_ttl=3600)
# First call: pays invoice
client.get("/premium")
# Second call: uses cached token (no payment)
client.get("/premium")
Payment Callbacks
Track payments in real-time:
def on_payment(info):
print(f"Paid {info.amount_sats} sats for {info.endpoint}")
# Log to your analytics, update UI, etc.
client = SatGateClient(wallet, on_payment=on_payment)
Session Tracking
client = SatGateClient(wallet)
# Make some requests...
client.get("/endpoint1")
client.get("/endpoint2")
print(f"Total spent: {client.total_paid_sats} sats")
Quiet Mode
Disable console output:
client = SatGateClient(wallet, verbose=False)
LangChain Integration
SatGate includes a ready-to-use LangChain tool for AI agents.
Installation
pip install satgate langchain langchain-openai
Basic Usage
from satgate import SatGateTool, LNBitsWallet
from langchain.agents import initialize_agent, AgentType
from langchain_openai import ChatOpenAI
# 1. Configure wallet
wallet = LNBitsWallet(
url="https://legend.lnbits.com",
admin_key="your-admin-key"
)
# 2. Create the tool
satgate_tool = SatGateTool(wallet=wallet)
# 3. Build agent
llm = ChatOpenAI(model="gpt-4", temperature=0)
agent = initialize_agent(
tools=[satgate_tool],
llm=llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True
)
# 4. Let it pay for premium data
result = agent.run("Fetch the premium market analysis from the paid API at https://api.example.com/premium/report")
How It Works
The SatGateTool gives your AI agent the ability to:
- Access any L402-protected API
- Automatically pay Lightning invoices
- Cache tokens for repeated access
- Handle the entire 402 → Pay → Retry flow
Tool Description (for the LLM)
Tool: satgate_api_browser
Description: Useful for fetching data from paid/premium APIs that require
Lightning Network payments. Use this tool when you need to access high-value
data, reports, or analytics that are behind a paywall. The tool handles
payment automatically.
Input: The full URL of the premium API endpoint to fetch data from.
Example Agent Prompt
agent.run("""
You are a financial analyst assistant.
Use the satgate_api_browser tool to fetch premium market data.
Then summarize the key insights.
""")
Error Handling
from satgate import PaymentError, L402ParseError
try:
response = client.get("/premium")
except PaymentError as e:
print(f"Payment failed: {e}")
except L402ParseError as e:
print(f"Invalid L402 response: {e}")
API Reference
SatGateClient / SatGateSession
SatGateClient(
wallet: LightningWallet, # Required: wallet for payments
cache_tokens: bool = True, # Cache L402 tokens
cache_ttl: int = 3600, # Cache TTL in seconds
on_payment: Callable = None, # Payment callback
verbose: bool = True # Print progress
)
PaymentInfo
@dataclass
class PaymentInfo:
invoice: str # BOLT11 invoice
preimage: str # Payment proof
macaroon: str # L402 macaroon
amount_sats: int # Amount paid
endpoint: str # URL accessed
timestamp: float # Unix timestamp
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file satgate-0.3.1.tar.gz.
File metadata
- Download URL: satgate-0.3.1.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb5bd349e977d0a56fa006ea29dee42d507347871deef62f3927f11a1de52558
|
|
| MD5 |
92646d38b640c309d6d58107c867a5aa
|
|
| BLAKE2b-256 |
78670c5dbf7a3edc8718083928ff557e69b4c02471adb6082aa57de872a41ac2
|
File details
Details for the file satgate-0.3.1-py3-none-any.whl.
File metadata
- Download URL: satgate-0.3.1-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
280679cce7c09af329a794c34884f69a083a60a22676e2ac9de981b66cf49dfd
|
|
| MD5 |
4d8254ba0ced566235c72a899ed78850
|
|
| BLAKE2b-256 |
614704d3f77f3c66a85d4f46b00a3b1be3f134352664a70ced8f159ef5177988
|