Bridge between Google AP2 Agent Payment Protocol and Coinbase x402. The only open-source AP2↔x402 converter.
Project description
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 | 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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ap2_x402_bridge-0.1.0.tar.gz.
File metadata
- Download URL: ap2_x402_bridge-0.1.0.tar.gz
- Upload date:
- Size: 18.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8fec0ffef2def5dab919e08c2a3fe0ef97dc3df14d8920123224156bf4c0997f
|
|
| MD5 |
793d51291885696dda51661424e692bc
|
|
| BLAKE2b-256 |
3d8b4537907351f8f2b2fdfd0ada68b0122a6a1e52d4e32ff6b274ae1349bfc8
|
File details
Details for the file ap2_x402_bridge-0.1.0-py3-none-any.whl.
File metadata
- Download URL: ap2_x402_bridge-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69e5daa029623a3191ff1f0f5d49fab992eb3b5516bf863c39c721e91954018b
|
|
| MD5 |
7964629845280682ee4083be25fe3dcf
|
|
| BLAKE2b-256 |
4cdec1748ab7931335fddfe7cfba44a9ad8e21059cb72b23ae66182dd7b97d37
|