Python Buyer SDK for AiPayGen x402 micropayment APIs — auto-402 handling, policy engine, and transaction tracking
Project description
aipaygen-buyer
Python Buyer SDK for AiPayGen x402 micropayment APIs. Automatically handles 402 Payment Required responses with signed USDC payments on Base Mainnet.
Install
pip install aipaygen-buyer
Quick Start
from aipaygen_buyer import AiPayGenBuyer
client = AiPayGenBuyer(
private_key="0xYOUR_PRIVATE_KEY", # or set AIPAYGEN_PRIVATE_KEY env var
max_price=0.05, # max $0.05 per call
daily_budget=5.0, # max $5/day
)
# Call any AiPayGen API — 402s are handled automatically
result = client.call("/ask", prompt="What is the x402 protocol?")
print(result.data)
print(f"Paid: {result.paid}")
# Check spending
print(f"Budget remaining: ${client.budget_remaining:.2f}")
print(f"Total spent: ${client.stats.total_spent_usdc:.4f}")
Features
- Auto-402 handling — Detects 402 responses, signs EIP-3009 payments, retries with X-Payment header
- Policy engine — Per-call price limits, daily/monthly budgets, vendor allowlists/blocklists
- Transaction tracking — Every payment logged with receipts, timestamps, and tx hashes
- Async support —
AsyncAiPayGenBuyerwith identical API usinghttpx.AsyncClient - Discovery — Browse API catalog and x402 protocol metadata
Policy Engine
from aipaygen_buyer import AiPayGenBuyer, SpendingPolicy
policy = SpendingPolicy(
max_price_per_call=0.02, # reject calls over $0.02
daily_budget=2.0, # $2/day cap
monthly_budget=50.0, # $50/month cap
vendor_allowlist={ # only pay these wallets
"0x366D488a48de1B2773F3a21F1A6972715056Cb30",
},
endpoint_allowlist={"/ask", "/summarize", "/translate"},
)
client = AiPayGenBuyer(private_key="0x...", policy=policy)
Async Usage
import asyncio
from aipaygen_buyer import AsyncAiPayGenBuyer
async def main():
async with AsyncAiPayGenBuyer(private_key="0x...") as client:
result = await client.call("/ask", prompt="Hello")
print(result.data)
asyncio.run(main())
API Key Mode
If you have a prepaid API key, you can skip x402 payments entirely:
client = AiPayGenBuyer(api_key="apk_YOUR_KEY")
result = client.call("/ask", prompt="No crypto needed")
Receipts & Tracking
client = AiPayGenBuyer(private_key="0x...")
client.call("/ask", prompt="First call")
client.call("/summarize", text="Some long text...")
for receipt in client.receipts:
print(f"{receipt.endpoint}: ${receipt.price_usdc} at {receipt.timestamp}")
stats = client.stats
print(f"Calls today: {stats.calls_today}")
print(f"Spent today: ${stats.spent_today_usdc:.4f}")
Discovery
client = AiPayGenBuyer()
# x402 protocol metadata
info = client.discover()
print(info["chains"]) # Base, Solana
# Browse API catalog
catalog = client.catalog(search="translate", category="ai")
Environment Variables
| Variable | Description |
|---|---|
AIPAYGEN_PRIVATE_KEY |
Ethereum private key for signing payments |
AIPAYGEN_API_KEY |
Prepaid API key (alternative to x402) |
How It Works
- SDK makes an API request to AiPayGen
- If the endpoint returns 402 Payment Required with x402 headers:
X-Price-USDC: Price for the callX-Pay-To: Vendor wallet addressX-Network: Chain (eip155:8453 = Base Mainnet)X-Facilitator-URL: Payment processor
- SDK checks the payment against your spending policy
- If approved, signs an EIP-3009 TransferWithAuthorization for USDC
- Retries the request with the signed
X-Paymentheader - Server verifies payment via the facilitator and returns the response
- Receipt is logged for tracking
License
MIT
Project details
Release history Release notifications | RSS feed
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 aipaygen_buyer-0.1.0.tar.gz.
File metadata
- Download URL: aipaygen_buyer-0.1.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ef77f9214905fa64c5401b1ba20d6dc845e94d7a08f8493a9e37c3d3a3c58ae8
|
|
| MD5 |
af40b667e8a37a2d4e3d185c50aef3f7
|
|
| BLAKE2b-256 |
e27dd61b37d4aed6c5e20192e1cf736f7c7168da77ab36973b513b1e64b4da5b
|
File details
Details for the file aipaygen_buyer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: aipaygen_buyer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd57fe6be952d2fec01e9dbd8e70ae2777f48c8c51401d9251de0540180c7aba
|
|
| MD5 |
23b8e263a5062a7dd71a1ccdf7c1a6a7
|
|
| BLAKE2b-256 |
edc10ac64a16b86039183dbd20efbf67f72c00001efc36859da987423bc56f29
|