Skip to main content

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 the version-correct payment header (PAYMENT-SIGNATURE for V2, X-PAYMENT for V1 legacy gates) → 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
payment_headers Return the (request, response) header pair for V1 vs V2 wire

Constants (DEFAULT_NETWORK, DEFAULT_SCHEME, X_PAYMENT_HEADER, X_PAYMENT_RESPONSE_HEADER, PAYMENT_SIGNATURE_HEADER, 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.

Metadata

Release files for x402-agent 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for x402-agent 0.2.0
File Size Uploaded
x402_agent-0.2.0.tar.gz 124.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for x402-agent 0.2.0
File Interpreter ABI Platform
x402_agent-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 139.0 kB

Release files / x402_agent-0.2.0.tar.gz

Download URL x402_agent-0.2.0.tar.gz
Size 124.5 kB
Tags Source
SHA-256 checksum
How to use checksums
0c151b9183944aa9af3695e3c4d00c885ecf21448dd2e277a0c3a9a3bc1a24bb
BLAKE2b-256 checksum
How to use checksums
abc97977451490f02dea0525b29bf2bd0e5a94da93ea17b7ee18a39a68b437e2
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release files / x402_agent-0.2.0-py3-none-any.whl

Download URL x402_agent-0.2.0-py3-none-any.whl
Size 14.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e2663ddb6df7a1525268111f0039862ac04322d43ffed8f70a579f5253316391
BLAKE2b-256 checksum
How to use checksums
2a4498b4bde8a9e5af7ba02e197c45032fbb6db009a40077c3b22833c6e77676
Upload date
Uploaded using Trusted Publishing?
What is 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}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page