Skip to main content

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:

  1. Access any L402-protected API
  2. Automatically pay Lightning invoices
  3. Cache tokens for repeated access
  4. 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

satgate-0.3.0.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

satgate-0.3.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file satgate-0.3.0.tar.gz.

File metadata

  • Download URL: satgate-0.3.0.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

Hashes for satgate-0.3.0.tar.gz
Algorithm Hash digest
SHA256 53727cd2df5fee3ec9f55fde052a419b8b69fac5ab5866ad587581437e21d47a
MD5 9a2c6eb4763d972047156cdbb0821784
BLAKE2b-256 e9d5ccc261461a6c689cd3bc94b93f7b79130714bf22d53e8ed19c1e73fe11f3

See more details on using hashes here.

File details

Details for the file satgate-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: satgate-0.3.0-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

Hashes for satgate-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4803a87c2728bbc992c529b6d7300a2af2987f84970df7275817276171c12ad
MD5 3e076063176e3e8c72d7c4a350164a26
BLAKE2b-256 8515942da0f9e6af2638802f884fb47066d750a9957a1cfc0226e6df25bbc333

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