Skip to main content

ap2-x402-bridge

The only open-source bridge between Google AP2 (Agent Payment Protocol) and Coinbase x402.

pip install ap2-x402-bridge

Two dominant agent payment protocols emerged in 2026 — and nothing bridged them. Until now.


Why this exists

Protocol Backer Mechanism Agents
AP2 Google Signed mandate objects Google Vertex AI agents
x402 Linux Foundation / Coinbase HTTP 402 + on-chain payment Claude, OpenAI agents, Hermes

An AP2 agent can't pay an x402 endpoint. An x402 agent can't submit to an AP2 endpoint. This bridge fixes both directions.


Quickstart

AP2 agent → x402 endpoint

Your agent has an AP2 mandate. The target service speaks x402. Bridge it:

from ap2_x402_bridge import AP2Mandate, BridgeServer

# Create and sign a mandate
mandate = AP2Mandate.create(
    agent_id="my-agent",
    payee_url="https://data.example.com/report",
    max_amount_usd=5.00,
    signing_key="my-signing-key",
    chain="base",
)

# Bridge it to an x402 endpoint
bridge = BridgeServer(signing_key="my-signing-key")
result = bridge.ap2_to_x402(
    mandate=mandate,
    x402_url="https://data.example.com/report",
)

if result.success:
    print(f"Paid ${result.amount_usd:.2f}")
    print(result.response_body)

x402 agent → AP2 endpoint

You have an x402 challenge (from a 402 response). The payee expects AP2. Convert it:

from ap2_x402_bridge import BridgeServer, X402Challenge

# Challenge from an HTTP 402 response
challenge = X402Challenge.from_json(response_body)

bridge = BridgeServer(signing_key="my-signing-key")
result = bridge.x402_to_ap2(
    challenge=challenge,
    ap2_endpoint="https://ap2.example.com/pay",
    agent_id="my-agent",
)

Auto-detect

bridge = BridgeServer(signing_key="my-key", simulate_payments=True)

# With mandate: uses AP2→x402 flow
result = bridge.pay("https://api.example.com", mandate=mandate)

# Without mandate: probes URL, detects protocol, bridges automatically
result = bridge.pay("https://api.example.com", agent_id="my-agent")

AP2 Mandate

from ap2_x402_bridge import AP2Mandate

# Create and sign
mandate = AP2Mandate.create(
    agent_id="agent-007",
    payee_url="https://data.io/v1/report",
    max_amount_usd=10.00,
    signing_key="agent-signing-key",
    chain="base",           # "base", "ethereum", "polygon", ...
    currency="USD",
    ttl_seconds=300,        # expires in 5 minutes
    purpose="Download quarterly report",
)

# Verify
assert mandate.verify("agent-signing-key") is True
assert mandate.is_expired() is False

# Serialize / deserialize
d = mandate.to_dict()
mandate2 = AP2Mandate.from_dict(d)

x402 Challenge

from ap2_x402_bridge import X402Challenge

# Parse from HTTP 402 response body
challenge = X402Challenge.from_json(response.text)

# Or construct manually
challenge = X402Challenge.simple(
    resource="https://api.example.com/data",
    amount_usd=2.50,
    network="base",
    description="API call",
)

# Inspect
print(f"Cost: ${challenge.amount_usd('base'):.2f}")
print(f"Network: {challenge.preferred.network}")

# To wire format
header = challenge.to_dict()

BridgeServer options

bridge = BridgeServer(
    signing_key="bridge-signing-key",    # used for bridge receipts and AP2 signing
    ap2_signing_key="ap2-specific-key",  # separate key for AP2 mandates (optional)
    timeout_seconds=30.0,                # HTTP timeout
    simulate_payments=False,             # True = no real HTTP calls (for tests)
    on_payment=lambda result: log(result),  # callback after each payment
)

Supported networks

AP2 name x402 name Chain
base base Base (Coinbase L2)
ethereum / eth ethereum Ethereum mainnet
polygon / matic polygon Polygon
arbitrum / arb arbitrum Arbitrum One
optimism / op optimism Optimism
solana / sol solana Solana

Zero dependencies

ap2-x402-bridge uses only Python stdlib (hashlib, hmac, base64, json, uuid, urllib). No external packages required.


Testing

cd ap2-x402-bridge
python3 tests/test_ap2.py
python3 tests/test_x402.py
python3 tests/test_converter.py
python3 tests/test_bridge.py

Or with pytest:

pip install pytest && pytest tests/ -v

License

Apache 2.0. Built by Bonanza Labs.

For a managed payment firewall with approval UI, policy editor, and audit log: → bonanza-labs.com/firewall

Metadata

Release files for ap2-x402-bridge 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ap2-x402-bridge 0.1.0
File Size Uploaded Provenance
ap2_x402_bridge-0.1.0.tar.gz 18.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ap2-x402-bridge 0.1.0
File Interpreter ABI Platform
ap2_x402_bridge-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 33.7 kB

Release files / ap2_x402_bridge-0.1.0.tar.gz

Download URL ap2_x402_bridge-0.1.0.tar.gz
Size 18.7 kB
Tags Source
SHA-256 checksum
How to use checksums
8fec0ffef2def5dab919e08c2a3fe0ef97dc3df14d8920123224156bf4c0997f
BLAKE2b-256 checksum
How to use checksums
3d8b4537907351f8f2b2fdfd0ada68b0122a6a1e52d4e32ff6b274ae1349bfc8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Release files / ap2_x402_bridge-0.1.0-py3-none-any.whl

Download URL ap2_x402_bridge-0.1.0-py3-none-any.whl
Size 15.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
69e5daa029623a3191ff1f0f5d49fab992eb3b5516bf863c39c721e91954018b
BLAKE2b-256 checksum
How to use checksums
4cdec1748ab7931335fddfe7cfba44a9ad8e21059cb72b23ae66182dd7b97d37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.5

Security for ap2-x402-bridge 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page