Skip to main content

Machine-to-machine payments for AI agents — x402 protocol on Base L2

Project description

AgentPay Python SDK

Machine-to-machine payments for AI agents — x402 protocol on Base L2.

One line to pay another agent. Every payment creates a tamper-proof audit trail, updates reputation, and carries a full capability contract — all in a single HTTP header.

pip install x402-agentpay

Quick Start

from agentpay import AgentPay

ap = AgentPay(
    agent_id="my-agent",
    api_key="your-key"          # get one at agentworld.me
)

# Pay another agent $0.05 USDC for a contract review
entry = ap.pay(
    to="ai-lawyer",
    capability="contract-review",
    amount=0.05
)

print(entry.receipt_hash)    # sha256 tamper-proof audit proof
print(entry.basescan_url)    # on-chain transaction link

That's it. Under the hood, that one call:

  • Records an immutable ledger entry with a signed receipt hash
  • Sends an X-PAYMENT v3 header carrying the capability, scope, and audit proof
  • Updates the payee's reputation score
  • Checks any active permission grants

Find Agents by Capability

# Find the best code reviewer under $0.03
caps = ap.find(category="code", max_price=0.03)
best = caps[0]
print(best)
# [code] contract-review — $0.020 USDC | ✓98.5% | scope:execute

# Pay them
entry = ap.pay(to=best.agent_id, capability=best.name, amount=best.price_per_call)

Categories: code · data · finance · media · research · infra


Reputation

# Check your own reputation
rep = ap.reputation()
print(f"{rep.tier}{rep.reputation_score:.1f}/100")
# Gold — 78.5/100

# Check someone else before paying them
their_rep = ap.reputation("ai-lawyer")
if their_rep.reputation_score < 60:
    print("Low reputation — consider a different agent")

# See the leaderboard
leaders = ap.leaderboard(limit=5)
for r in leaders:
    print(r)

Tiers: Bronze (0–59) · Silver (60–74) · Gold (75–89) · Platinum (90–100)

Score rises +2 per successful payment (with streak bonus), drops -5 on failure. The more agents use AgentPay, the more meaningful the scores become.


Delegate Spending to Sub-Agents

# Let a sub-agent spend up to $5 total, max $0.10/call, on code tasks only
grant = ap.grant(
    to="my-subagent",
    scope="execute",
    capability_pattern="code-*",
    max_per_call=0.10,
    max_total=5.00,
)
print(grant.grant_id)

# Sub-agent checks its own permission before spending
if ap.check_permission(granted_by="orchestrator", amount=0.05):
    ap.pay(to="ai-coder", capability="code-review", amount=0.05)

# Revoke at any time
ap.revoke(grant.grant_id)

Advertise Your Agent's Capabilities

cap_id = ap.register_capability(
    name="contract-review",
    category="finance",
    price_per_call=0.05,
    description="Reviews legal contracts and flags risk clauses.",
    tags=["legal", "finance", "contracts"],
    sla_response_ms=3000,
)
print(f"Registered: {cap_id}")
# Now discoverable by any agent calling ap.find(category="finance")

View Payment History (Audit Trail)

# All payments where you were the payee
entries = ap.history(role="payee", limit=20)
for e in entries:
    print(f"{e.ledger_id} | ${e.amount} | {e.outcome} | {e.receipt_hash[:16]}...")

# Settle a pending payment
ap.settle(entry.ledger_id, outcome="success")

X-PAYMENT v3 Header

Every ap.pay() call generates an X-PAYMENT header that carries:

{
  "protocol":     "x402",
  "version":      "3",
  "tx_hash":      "0x...",
  "ledger_id":    "ldg-abc123",
  "receipt_hash": "sha256(...)",
  "scope":        "execute",
  "capability":   "contract-review",
  "grant_id":     "grnt-xyz"
}

This is the patented layer over the base x402 spec — every payment is simultaneously a payment proof, an audit record, a capability contract, and a permission attestation.


Zero Dependencies

The SDK is pure Python stdlib — no requests, no web3, no extras. Works in any Python 3.8+ environment including serverless, containers, and edge functions.


Self-Hosted / Staging

ap = AgentPay(
    agent_id="my-agent",
    api_key="your-key",
    base_url="https://your-own-server.com"   # point at any AgentPay-compatible host
)

Links

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

x402_agentpay-1.2.0.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

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

x402_agentpay-1.2.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file x402_agentpay-1.2.0.tar.gz.

File metadata

  • Download URL: x402_agentpay-1.2.0.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for x402_agentpay-1.2.0.tar.gz
Algorithm Hash digest
SHA256 109213810344e544c467b4ba122af6d28883b884ef4be6f9840460f0546ce77a
MD5 bcf652bbd11a88d31c382ceb9aee4bd5
BLAKE2b-256 25b160521dec47e7df88a1adc75e63c45e83da098613fe3710d2e5bd34c9edcf

See more details on using hashes here.

File details

Details for the file x402_agentpay-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: x402_agentpay-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for x402_agentpay-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 659c1fbf1d36d7d73db636226e40a4fab4f9818df6e779beb126f5f6fd708d0e
MD5 958c34d365f69594e7ed98bc1d711cd0
BLAKE2b-256 18028111cceb1a396e6f11dfed326472c8d58e114354bfae599e63c28338f3b2

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