Skip to main content

Framework-agnostic payer for the x402 HTTP payment protocol. Works with LangChain, CrewAI, AutoGen, LangGraph, or any other agent framework.

Project description

x402-agent

Framework-agnostic payer for the x402 HTTP payment protocol.

x402-agent is an independent open-source package. It is not affiliated with, endorsed by, or sponsored by the x402 Foundation, LF Projects, LLC, or Coinbase, Inc. "x402" is used nominatively to refer to the open HTTP payment protocol. Maintained by Xenarch.

Zero framework dependencies. Subclass or compose from LangChain, CrewAI, AutoGen, LangGraph, or anything else that wants to let an agent spend USDC against a 402 Payment Required response without rolling its own pay loop.

What it does

from decimal import Decimal
from x402_agent import X402Payer, BudgetPolicy

payer = X402Payer(
    private_key="0x...",
    budget_policy=BudgetPolicy(
        max_per_call=Decimal("0.05"),
        max_per_session=Decimal("1.00"),
    ),
)

result = payer.pay("https://example.com/gated/article")
# {"success": True, "body": "...", "amount_usd": "0.01", ...}

pay(url) walks the full challenge: GET → parse 402 → pick a supported (scheme, network) → enforce your budget → sign an EIP-3009 USDC authorisation → retry with X-PAYMENT → return the unlocked body. Never raises; every failure mode is a dict you can show an LLM.

Async via await payer.pay_async(url).

What's in the package

Symbol Purpose
X402Payer Neutral pay loop with _pre_payment_hook / _post_payment_hook for subclasses
BudgetPolicy Per-call + per-session spend caps, optional human-approval callback, thread-safe
select_accept Pick a supported (scheme, network) from a parsed PaymentRequired
price_usd Atomic-units → Decimal USD using the advertised asset decimals
is_public_host / is_public_host_async SSRF guard for agent-provided URLs
budget_hint_exceeds Early-refusal check against pay.json budget_hints

Constants (DEFAULT_NETWORK, DEFAULT_SCHEME, X_PAYMENT_HEADER, X_PAYMENT_RESPONSE_HEADER) are exported so adapters don't have to hard-code them.

Subclassing

The four hooks are the intended extension point. Override any subset:

class MyPayer(X402Payer):
    def _pre_payment_hook(self, *, url, accept, price):
        # Return an error dict to abort before the budget lock.
        if self._is_suspicious(accept.pay_to):
            return {"error": "refused_by_policy"}
        return None

    def _post_payment_hook(self, result, paid_response):
        # Mutate result in place; post runs after budget.commit().
        result["signed_by"] = "my-facilitator"

Pre-hook runs before the budget lock so a slow external check (reputation lookup, etc.) can't block other concurrent payments. Post-hook runs after the spend is committed so a receipt fetch failure can't revert a paid GET.

Stability

__all__ defines the v0.x stable surface. Anything in x402_agent._helpers or x402_agent._payer is accessible but subject to rename.

Licence

MIT.

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_agent-0.1.1.tar.gz (121.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_agent-0.1.1-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file x402_agent-0.1.1.tar.gz.

File metadata

  • Download URL: x402_agent-0.1.1.tar.gz
  • Upload date:
  • Size: 121.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for x402_agent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d0facf5ab369300f4b55dc1e6b0a2878f9d879c9e99e2bd7eb3b5bf0ca6c0d2e
MD5 8a3e10015342c0f866dd422e3c982a17
BLAKE2b-256 f19824bc45ae0bc82cf1d58116dea6782ccee509394f1619dfeb0c933e1cdeae

See more details on using hashes here.

File details

Details for the file x402_agent-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: x402_agent-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for x402_agent-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1fbab10691c4cf455ce635436935318e3df37845efd8d0544569370b119a3f62
MD5 c017c4964a566b08b58d18607b147e5c
BLAKE2b-256 4221b215731ab8371b2133081246abc33cc40c1e73616b8a3c33b9242ffb61ff

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