Secure x402 client SDK with agentic payments for buyers and sellers
Project description
x402-secure
Open-source x402 client SDKs for buyers and sellers that integrate with a unified gateway for risk session/trace and a seller proxy for verify/settle.
- Buyer SDK: create risk session/trace via
AGENT_GATEWAY_URL, buildX-PAYMENT-SECUREfrom OpenTelemetry context. - Seller SDK: call proxy
/x402/verifyand/x402/settlewith a simpleverify_then_settle(...)wrapper.
Fail-fast principles:
- Non-200 responses raise immediately (
HTTPStatusError). - Invalid or missing
traceparentcontext raises. - Header size limit enforced (
X-PAYMENT-SECURE≤ 4096 bytes).
Install
pip install x402-secure
Quickstart
import os, asyncio
from x402_client import BuyerConfig, BuyerClient, RiskClient
from x402_client import build_payment_secure_header, start_client_span
async def main():
buyer = BuyerClient(BuyerConfig(
seller_base_url=os.getenv("SELLER_BASE_URL", "http://localhost:8010"),
agent_gateway_url=os.getenv("AGENT_GATEWAY_URL", "http://localhost:8000"),
network=os.getenv("NETWORK", "base-sepolia"),
buyer_private_key=os.getenv("BUYER_PRIVATE_KEY"),
))
# Create risk session + trace
rc = RiskClient(os.getenv("AGENT_GATEWAY_URL", "http://localhost:8000"))
# agent_did: currently wallet address, future: EIP-8004 DID (did:eip8004:chain:contract:tokenId)
sid = (await rc.create_session(agent_did=buyer.address, app_id=None, device={"ua": "oss-example"}))['sid']
tid = (await rc.create_trace(sid=sid, agent_trace={"task": "buy", "parameters": {"symbol": "BTC/USD"}}))['tid']
# Build secure header from OTEL context
with start_client_span("buyer.payment"):
xps = build_payment_secure_header(agent_trace_context={"tid": tid})
res = await buyer.execute_paid_request(
endpoint="/api/market-data",
task="Buy BTC price",
params={"symbol": "BTC/USD"},
risk_sid=sid,
extra_headers=xps,
)
print(res)
asyncio.run(main())
See examples/ for runnable scripts.
License
Apache-2.0
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_secure-0.1.1.tar.gz
(161.2 kB
view details)
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 x402_secure-0.1.1.tar.gz.
File metadata
- Download URL: x402_secure-0.1.1.tar.gz
- Upload date:
- Size: 161.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
718b22887ff6a8e3573b5375e55855709b6b22c938c0eb0308011ac64b9f640d
|
|
| MD5 |
bfbd1112601cf85a8643fcbd54519ad4
|
|
| BLAKE2b-256 |
d4c261a0e2211ce7d8e2676f544134785204a7882a61b5add5de45be3eb08440
|
File details
Details for the file x402_secure-0.1.1-py3-none-any.whl.
File metadata
- Download URL: x402_secure-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48b6def63df6fd0ac44272f6d49b331b6aff188c94856f038f17ffb7c772dab4
|
|
| MD5 |
d78c4f643690943817648aa834a9c8c7
|
|
| BLAKE2b-256 |
53bc308806ff9c0f36cce05f8e9326ab4b5f769b3563e4624db73deab27b7fcd
|