Python SDK for MoltsPay - Agent-to-Agent Payments. LangChain compatible.
Project description
MoltsPay Python SDK
Python SDK for MoltsPay - Agent-to-Agent Payments.
MoltsPay enables AI agents to pay each other for services using the x402 protocol - HTTP-native payments with USDC stablecoins. No gas fees, no complex wallet management.
What is MoltsPay?
MoltsPay is blockchain payment infrastructure designed for AI agents. It solves a fundamental problem: how do autonomous AI agents pay for services?
- ๐ค Agent-to-Agent Commerce - AI agents can autonomously discover, pay for, and use services
- ๐จ Gasless Payments - Uses EIP-2612 permits, no ETH needed
- ๐ x402 Protocol - HTTP 402 Payment Required - payments as native HTTP flow
- ๐ Spending Limits - Set per-transaction and daily limits for safety
- ๐ Multi-Chain - Base and Polygon supported
- ๐ฆ LangChain Ready - Drop-in tools for LangChain agents
Installation
pip install moltspay
For LangChain integration:
pip install moltspay[langchain]
Quick Start
from moltspay import MoltsPay
# Initialize (auto-creates wallet if not exists)
client = MoltsPay()
print(f"Wallet address: {client.address}")
# Discover services from a provider
services = client.discover("https://juai8.com/zen7")
for svc in services:
print(f"{svc.id}: {svc.price} {svc.currency}")
# Pay for a service
result = client.pay(
"https://juai8.com/zen7",
"text-to-video",
prompt="a cat dancing on the beach"
)
print(result.result)
Features
Auto Wallet Management
Wallet is automatically created on first run and stored at ~/.moltspay/wallet.json. Compatible with Node.js CLI.
from moltspay import MoltsPay
client = MoltsPay()
print(f"Address: {client.address}")
print(f"Balance: {client.balance()} USDC")
Spending Limits
Control your agent's spending with built-in limits:
from moltspay import MoltsPay
client = MoltsPay()
# Check current limits
limits = client.limits()
print(f"Max per tx: {limits.max_per_tx}")
print(f"Max per day: {limits.max_per_day}")
print(f"Spent today: {limits.spent_today}")
# Update limits
client.set_limits(max_per_tx=20, max_per_day=200)
Multi-Chain Support
MoltsPay supports multiple chains. Default is Base, but you can use Polygon:
from moltspay import MoltsPay
# Default: Base
client = MoltsPay()
# Use Polygon
client = MoltsPay(chain='polygon')
# Pay on Polygon
result = client.pay(
"https://juai8.com/zen7",
"text-to-video",
prompt="a cat dancing"
)
Supported Chains:
| Chain | Network ID | Token |
|---|---|---|
| Base | eip155:8453 | USDC |
| Polygon | eip155:137 | USDC |
Both chains are gasless - the CDP facilitator handles all on-chain settlement.
Async Support
Full async/await support for high-performance applications:
import asyncio
from moltspay import AsyncMoltsPay
async def main():
async with AsyncMoltsPay() as client:
result = await client.pay(
"https://juai8.com/zen7",
"text-to-video",
prompt="a cat dancing"
)
print(result.result)
asyncio.run(main())
Error Handling
Comprehensive exception types for robust error handling:
from moltspay import MoltsPay, InsufficientFunds, LimitExceeded, PaymentError
client = MoltsPay()
try:
result = client.pay(...)
except InsufficientFunds as e:
print(f"Need {e.required} USDC, have {e.balance}")
except LimitExceeded as e:
print(f"Exceeds {e.limit_type} limit: {e.amount} > {e.limit}")
except PaymentError as e:
print(f"Payment failed: {e}")
LangChain Integration
Use MoltsPay as tools in your LangChain agents - let your AI autonomously pay for services!
from langchain.agents import initialize_agent, AgentType
from langchain_openai import ChatOpenAI
from moltspay.integrations.langchain import MoltsPayTool
llm = ChatOpenAI(model="gpt-4")
tools = [MoltsPayTool()]
agent = initialize_agent(
tools,
llm,
agent=AgentType.OPENAI_FUNCTIONS,
verbose=True
)
# Agent can now pay for AI services!
result = agent.run("Generate a video of a cat dancing on the beach")
Available Tools
Two tools available for different use cases:
from moltspay.integrations.langchain import get_moltspay_tools
tools = get_moltspay_tools() # Returns both tools
| Tool | Description |
|---|---|
MoltsPayTool |
Pay for and execute services |
MoltsPayDiscoverTool |
Discover available services and prices |
CLI Compatibility
Wallet format is fully compatible with the Node.js CLI:
# Create wallet with Node CLI
npx moltspay init --chain base
# Use same wallet in Python
python -c "from moltspay import MoltsPay; print(MoltsPay().address)"
How x402 Works
Your Agent Service Provider Blockchain
โ โ โ
โ Request service โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโ> โ โ
โ โ โ
โ 402 + price + wallet โ โ
โ <โโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ
โ [Sign payment - NO GAS] โ โ
โ โ โ
โ Request + signed payment โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโ> โ Verify & settle โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโ>โ
โ โ โ
โ 200 OK + result โ โ
โ <โโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
Your agent never pays gas - the CDP facilitator handles all on-chain settlement.
Use Cases
- AI Assistants - Let your assistant pay for premium APIs
- Autonomous Agents - Agents that can spend within limits
- Multi-Agent Systems - Agents paying other agents for services
- AI Pipelines - Pay-per-use for expensive compute steps
Running a Server (Accepting Payments)
Want to accept payments for your AI services? See the Server Guide.
Quick start:
# Install
pip install moltspay
# Create skill structure
mkdir my_skill && cd my_skill
# Add moltspay.services.json and __init__.py (see docs)
# Start server
moltspay-server ./my_skill --port 8402
Related Projects
- moltspay (Node.js) - Node.js SDK and CLI
- x402 Protocol - The HTTP payment standard
Links
- Website: https://moltspay.com
- PyPI: https://pypi.org/project/moltspay/
- npm (Node.js): https://www.npmjs.com/package/moltspay
- GitHub: https://github.com/Yaqing2023/moltspay-python
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 moltspay-0.4.0.tar.gz.
File metadata
- Download URL: moltspay-0.4.0.tar.gz
- Upload date:
- Size: 35.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9fe621ae614397fac318f6e69dc679a438d30416569686d226b2ffd4032e9f00
|
|
| MD5 |
bab0c34ad2db1d345823c642337d1dc0
|
|
| BLAKE2b-256 |
67a1ebc4cb141e8417b587073711d8f74cb60fb07f6656e8588cbf1073be135f
|
File details
Details for the file moltspay-0.4.0-py3-none-any.whl.
File metadata
- Download URL: moltspay-0.4.0-py3-none-any.whl
- Upload date:
- Size: 30.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
13308c61e7580827da8dd112d95ea808d1aeed3b59f96ce42562984cd9b04043
|
|
| MD5 |
f91be7346eb496f4ba999da657676a43
|
|
| BLAKE2b-256 |
0e41e79630e31d5d353bdd3a80f8dced8d24ae8723c2a407259e7f8abe9015eb
|