Skip to main content

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, build X-PAYMENT-SECURE from OpenTelemetry context.
  • Seller SDK: call proxy /x402/verify and /x402/settle with a simple verify_then_settle(...) wrapper.

Fail-fast principles:

  • Non-200 responses raise immediately (HTTPStatusError).
  • Invalid or missing traceparent context raises.
  • Header size limit enforced (X-PAYMENT-SECURE ≤ 4096 bytes).

Install

pip install x402-secure

Quickstart

import os, asyncio
from x402_secure_client import BuyerConfig, BuyerClient, RiskClient
from x402_secure_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.3.tar.gz (161.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_secure-0.1.3-py3-none-any.whl (12.0 kB view details)

Uploaded Python 3

File details

Details for the file x402_secure-0.1.3.tar.gz.

File metadata

  • Download URL: x402_secure-0.1.3.tar.gz
  • Upload date:
  • Size: 161.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for x402_secure-0.1.3.tar.gz
Algorithm Hash digest
SHA256 2c4d65b3892eeefee07026e8f801891908ce655007a6b56afddda982fd6ea595
MD5 74043e0da623582d3403e9b2a987e561
BLAKE2b-256 cdd3c5ade76f316cce4939773227d5de2f4e04a79e1cbcb4d09a280e11295776

See more details on using hashes here.

File details

Details for the file x402_secure-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: x402_secure-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.9

File hashes

Hashes for x402_secure-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e6cbf8b54f710413073f201ea7e3b1c1e7dafea91a2a5400411bb017765f76f6
MD5 f25a67c5bd2d3240df10d5d94c272112
BLAKE2b-256 ef13653f57e65bf21ab4ff980a86c1655cc46650a24bb4e5d874d84489113658

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