Skip to main content

Spending firewall meets the HTTP 402 payment protocol. Policy-based spending controls for AI agent payments.

Project description

๐Ÿ’ธ Bonanza x402 Adapter

Spending firewall meets the HTTP 402 payment protocol

PyPI Python License x402

Bonanza x402 brings policy-based spending controls to the x402 payment protocol. Every payment request goes through a firewall that checks budgets, risk scores, vendor allowlists, and approval queues โ€” before money moves.

Why?

AI agents are starting to spend real money via the x402 protocol. But x402 alone doesn't answer:

  • "Is this payment within budget?"
  • "Is this vendor trusted?"
  • "Should a human approve this?"
  • "What's the risk level of this transaction?"

Bonanza x402 adds these guardrails. Think of it as ulimit for agent payments โ€” but with Stripe checkout, crypto wallets, and approval flows.

Quick Start

pip install bonanza-x402

Basic: Wrap any x402 payment with a budget

from bonanza_x402 import Firewall, Policy

# Define your spending policy
policy = Policy(
    max_spend_usd=10.00,          # Hard limit per session
    daily_budget_usd=50.00,       # Daily cap
    allowed_networks=["base"],     # Only Base chain
    allowed_tokens=["usdc"],      # Only USDC
    require_approval_above=5.00,  # Human approval above $5
    trusted_vendors=["api.weather.com", "api.data.gov"],
)

# Create the firewall
firewall = Firewall(policy=policy)

# Check before paying
result = firewall.evaluate(
    amount=3.50,
    vendor="api.weather.com",
    network="base",
    token="usdc",
    description="Weather API call"
)

if result.approved:
    print(f"โœ… Payment approved: {result.reason}")
else:
    print(f"โŒ Payment blocked: {result.reason}")
    if result.requires_approval:
        print(f"โณ Awaiting human approval: {result.approval_url}")

With x402 Protocol Integration

import x402
from bonanza_x402 import Firewall, Policy

# Set up x402 client
client = x402.Client()

# Set up Bonanza firewall
firewall = Firewall(Policy(
    max_spend_usd=25.00,
    trusted_vendors=["api.premium-data.com"],
    require_approval_above=10.00,
))

# Wrap x402 calls with firewall
response = client.get(
    "https://api.premium-data.com/v1/analysis",
    payment_config=x402.PaymentConfig(
        max_amount=5.00,
        network="base",
    ),
    # Bonanza intercepts before payment is sent
    pre_payment_hook=firewall.x402_hook(),
)

Stripe Checkout Fallback

For payments that need human approval, Bonanza creates a Stripe Checkout session:

from bonanza_x402 import Firewall, Policy, StripeConfig

firewall = Firewall(
    policy=Policy(
        max_spend_usd=100.00,
        require_approval_above=25.00,
    ),
    stripe=StripeConfig(
        api_key="sk_live_...",
        success_url="https://your-app.com/success",
        cancel_url="https://your-app.com/cancel",
    ),
)

result = firewall.evaluate(amount=50.00, vendor="api.expensive-ai.com")
# result.approved = False
# result.checkout_url = "https://checkout.stripe.com/pay/cs_live_..."

Policy Rules

Rule Type Default Description
max_spend_usd float โˆž Hard limit per session
daily_budget_usd float โˆž Daily spending cap
per_transaction_limit_usd float โˆž Max single transaction
allowed_networks list ["*"] Allowed blockchain networks
allowed_tokens list ["*"] Allowed payment tokens
trusted_vendors list [] Auto-approved vendor domains
blocked_vendors list [] Always-blocked vendor domains
require_approval_above float 0 Threshold for human approval
risk_threshold float 1.0 0-1 scale, block above this

Firewall Result

@dataclass
class FirewallResult:
    approved: bool           # Whether the payment is allowed
    reason: str              # Human-readable explanation
    risk_score: float        # 0-1 risk assessment
    requires_approval: bool  # Whether human approval is needed
    approval_url: str | None # Stripe checkout URL if approval needed
    transaction_id: str      # Unique ID for audit trail
    metadata: dict           # Additional context

Audit Trail

Every evaluation is logged:

# Get audit log
entries = firewall.audit_log()

for entry in entries:
    print(f"{entry.timestamp} | {entry.amount} | {entry.vendor} | {entry.decision} | {entry.reason}")

CLI

# Evaluate a payment
bonanza-x402 evaluate --amount 5.00 --vendor api.weather.com --network base

# Check policy status
bonanza-x402 policy show

# View audit log
bonanza-x402 audit log

# Create a test checkout
bonanza-x402 checkout --amount 10.00 --stripe-key sk_test_...

Architecture

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  AI Agent   โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  Bonanza Firewall    โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  x402 Proto  โ”‚
โ”‚             โ”‚     โ”‚                      โ”‚     โ”‚  (Payment)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚  โ”‚ Policy Check  โ”‚   โ”‚
                    โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค   โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚  โ”‚ Risk Score    โ”‚โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ–ถโ”‚  Stripe      โ”‚
                    โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค   โ”‚     โ”‚  (Approval)  โ”‚
                    โ”‚  โ”‚ Vendor List   โ”‚   โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                    โ”‚  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค   โ”‚
                    โ”‚  โ”‚ Budget Check  โ”‚   โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                    โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚โ”€โ”€โ”€โ”€โ–ถโ”‚  Audit Log   โ”‚
                    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Comparison

Feature x402 alone AgentBudget Bonanza x402
Budget limits โŒ โœ… โœ…
Vendor allowlist โŒ โŒ โœ…
Risk scoring โŒ โŒ โœ…
Human approval queue โŒ โŒ โœ…
Stripe checkout โŒ โŒ โœ…
Crypto (USDC/SOL) โœ… โŒ โœ…
x402 native โœ… โŒ โœ…
Audit trail โŒ โœ… โœ…

Requirements

  • Python 3.10+
  • x402>=2.0 (optional, for protocol integration)
  • stripe>=5.0 (optional, for checkout fallback)
  • solana>=0.30 (optional, for Solana payments)

License

Apache License 2.0 โ€” see LICENSE for details.

Links


Built by Bonanza Labs ๐Ÿงจ

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

bonanza_x402-0.1.0.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

bonanza_x402-0.1.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file bonanza_x402-0.1.0.tar.gz.

File metadata

  • Download URL: bonanza_x402-0.1.0.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for bonanza_x402-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f3942f3e5bb453f5548dfdb51eea97127b0d56d4ec13fbd7e25caddbcbf1f92b
MD5 66a4934ec62078f7302fb02479d30428
BLAKE2b-256 ce2dfdb2c05b61a541e5b9e0f1425384fd6de63e2cb570ae72abad17d6117315

See more details on using hashes here.

File details

Details for the file bonanza_x402-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: bonanza_x402-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for bonanza_x402-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42f45cbbb0cceabe66912a4dd1262d823dbb0335acb0333dfeccd0f98ae43d68
MD5 00223ae466cb1cba178290de0dd2652e
BLAKE2b-256 16da0ad80a106e1d872361e588f125ba94634a360b1918376b224be4ff32048c

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