Skip to main content

MixrPay Agent SDK - Enable AI agents to make x402 payments with session keys

Project description

MixrPay Agent SDK for Python

Enable AI agents to make autonomous payments.

Installation

pip install mixrpay

Setup

export MIXRPAY_SESSION_KEY=sk_live_...

Session keys are created by wallet owners at mixrpay.com/wallet/sessions.

Basic Usage

from mixrpay import AgentWallet

wallet = AgentWallet(session_key="sk_live_...")

# Call a paid API
response = await wallet.call_merchant_api(
    url="https://api.merchant.com/generate",
    merchant_public_key="pk_live_...",
    price_usd=0.05,
    json={"prompt": "Hello world"}
)

data = response.json()

Pre-Flight Checks

# Check if wallet can afford an amount before calling
check = await wallet.can_afford(0.50)
if check.can_afford:
    response = await wallet.call_merchant_api(...)
else:
    print(f"Insufficient funds. Have: ${check.current_balance}")

# Run diagnostics for debugging
diag = await wallet.run_diagnostics()
print(f"Balance: ${diag.balance_usd}")
print(f"Latency: {diag.latency_ms}ms")
print(f"Recommendations: {diag.recommendations}")

Error Handling

from mixrpay import (
    AgentWallet,
    MixrPayError,
    InsufficientBalanceError,
    SessionLimitExceededError,
    PaymentFailedError,
)

try:
    response = await wallet.call_merchant_api(...)
except InsufficientBalanceError as e:
    print(f"Low balance. Top up: {e.top_up_url}")
except SessionLimitExceededError:
    print("Session limit exceeded")
except MixrPayError as e:
    # Check if error is retryable
    if e.is_retryable():
        delay = e.retry_after_ms / 1000 if e.retry_after_ms else 1
        await asyncio.sleep(delay)
        return await retry()
    raise

Spending Controls

wallet = AgentWallet(
    session_key="sk_live_...",
    max_payment_usd=1.0,  # Client-side safety limit
    on_payment=lambda p: print(f"Paid ${p.amount_usd}"),
)

# Check balance
balance = await wallet.get_balance()
stats = wallet.get_spending_stats()

Context Manager

async with AsyncAgentWallet(session_key="sk_live_...") as wallet:
    response = await wallet.call_merchant_api(...)

LangChain Integration

from langchain.tools import BaseTool
from mixrpay import AgentWallet

class PaidSearchTool(BaseTool):
    name = "paid_search"
    description = "Premium search ($0.05/query)"
    
    def __init__(self, session_key: str):
        super().__init__()
        self.wallet = AgentWallet(session_key=session_key)
    
    async def _arun(self, query: str) -> str:
        response = await self.wallet.call_merchant_api(
            url="https://api.search.com/query",
            merchant_public_key="pk_live_...",
            price_usd=0.05,
            json={"query": query}
        )
        return response.json()["results"]

What's New in v0.6.0

  • can_afford(amount_usd) - Pre-flight balance check
  • is_retryable() on all errors - Determine if operation should be retried
  • retry_after_ms on errors - Suggested retry delay
  • Enhanced run_diagnostics() - Session limits, latency, recommendations
  • request_id and correlation_id in payment events

Documentation

Full documentation at mixrpay.com/docs

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

mixrpay-0.7.0.tar.gz (37.6 kB view details)

Uploaded Source

Built Distribution

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

mixrpay-0.7.0-py3-none-any.whl (35.4 kB view details)

Uploaded Python 3

File details

Details for the file mixrpay-0.7.0.tar.gz.

File metadata

  • Download URL: mixrpay-0.7.0.tar.gz
  • Upload date:
  • Size: 37.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for mixrpay-0.7.0.tar.gz
Algorithm Hash digest
SHA256 9eb45925f5895c3b30f4568e8ec49a0cdb6b67fa1e465e4d6cff29241b0f146b
MD5 13258f2a4326be9ff106e3c10fc61140
BLAKE2b-256 30a3dc09f06653e2d70e03fe9f8a247e17cd38cc156c1eb12172c645a6992848

See more details on using hashes here.

File details

Details for the file mixrpay-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: mixrpay-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 35.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for mixrpay-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e3d878933df7aff45c182b037e239fff5b9dc6b6957ee704fc4954ba8c0064b3
MD5 34f8fe411db9916fe355d55d8d2f9fe1
BLAKE2b-256 218fbd9c358588f4ca7c5a642875a929653c6e8ffc80553d7665f065bb15aad9

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