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.2.tar.gz (123.3 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.2-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: x402_agent-0.1.2.tar.gz
  • Upload date:
  • Size: 123.3 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.2.tar.gz
Algorithm Hash digest
SHA256 c6a50aecac8d47f055aeed0373eaf1b0df3c6c07f197ab41204b4597af2b8c30
MD5 1ac93257b00d74628b38bbf9c6c07c1a
BLAKE2b-256 e81f061a88851dbb30a4bebb4902380d25f9830774a8428b3630f1a560eb16d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: x402_agent-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f1775285c64079283e02c492ed57b59d2576ae387eadc0b78d518e8bd94233f0
MD5 94cd57ccc368c2280ef0eb662c9dfb21
BLAKE2b-256 987e6897b07829cea7124258eca773f351a75b07d2c45250f0d18ba8a0c88bad

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