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.6.1.tar.gz (34.3 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.6.1-py3-none-any.whl (32.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mixrpay-0.6.1.tar.gz
Algorithm Hash digest
SHA256 382b3a2414c6fa63a0012d291c12c7c8d5494aa7a34ca889c5bf93b8cc7c7775
MD5 4221a45c49e7d6131552e6a0038c9f6a
BLAKE2b-256 720ccc21d418e11ae792add6a7ff374941ecbad27b71082f898b36595c755998

See more details on using hashes here.

File details

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

File metadata

  • Download URL: mixrpay-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 32.0 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.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b716ed16de695236f629283e9e05256e93182e765d1f0c7ce3072b53eb0d8a73
MD5 a40d5883c0b2f7b200ba472ec9acd849
BLAKE2b-256 565f9cae355141889e33aee65db317fb5b1ef6e3479535ce387db18e3cda950c

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