Skip to main content

x402-anthropic-python

PyPI Python License CI

x402 payment transport for the Anthropic Python SDK.

Wrap the standard anthropic.Anthropic client with a crypto wallet. When the server responds with HTTP 402, the library automatically signs a USDC payment and retries — zero code changes needed. Follows the pattern introduced by qntx/x402-openai-python.

Install

pip install "x402-anthropic[evm]"   # EVM (Base, Ethereum)
pip install "x402-anthropic[svm]"   # SVM (Solana)
pip install "x402-anthropic[all]"   # all chains

Quick start

from x402_anthropic import X402Anthropic, EVMWallet

wallet = EVMWallet(private_key="0x...")
client = X402Anthropic(wallet=wallet, base_url="https://your-x402-gateway.example.com")

message = client.messages.create(
    model="claude-opus-4-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Hello!"}],
)
print(message.content[0].text)

Swap EVMWallet for SVMWallet to pay on Solana — the API is identical.

Usage

Async + streaming

from x402_anthropic import AsyncX402Anthropic, EVMWallet
import asyncio

async def main():
    wallet = EVMWallet(private_key="0x...")
    async with AsyncX402Anthropic(wallet=wallet, base_url="...") as client:
        async with client.messages.stream(...) as stream:
            async for text in stream.text_stream:
                print(text, end="", flush=True)

asyncio.run(main())

Payment policies

from x402 import preferNetwork, maxAmount

client = X402Anthropic(
    wallet=wallet,
    policies=[preferNetwork("eip155:8453"), maxAmount(1_000_000)],  # Base, max $1 USDC
    base_url="...",
)

API reference

X402Anthropic / AsyncX402Anthropic

Drop-in replacement for anthropic.Anthropic / anthropic.AsyncAnthropic.

Parameter Type Description
wallet Wallet Wallet adapter for signing payments
policies list[Policy] Payment policies (chain preference, amount cap)
base_url str Gateway URL (required — points at your x402 server)

All standard Anthropic kwargs (timeout, max_retries, api_key, …) are forwarded.

Wallet adapters

Class Chain Extra
EVMWallet(private_key=…) Base, Ethereum, any EVM x402-anthropic[evm]
SVMWallet(private_key=…) Solana x402-anthropic[svm]

Implement the Wallet base class to add a new chain.

Low-level transports

X402Transport / AsyncX402Transport — httpx transports for manual wiring:

import httpx
from x402_anthropic._transport import X402Transport

x402_http = wallet.build_sync()
transport = X402Transport(x402_http, httpx.HTTPTransport())
client = httpx.Client(transport=transport)

Examples

EVM_PRIVATE_KEY="0x..."        python examples/basic.py
EVM_PRIVATE_KEY="0x..."        python examples/streaming.py

Related

License

MIT

Metadata

Release files for x402-anthropic 0.1.2

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-anthropic 0.1.2
File Size Uploaded Provenance Actions
x402_anthropic-0.1.2.tar.gz 11.0 kB Without provenance Details for x402_anthropic-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for x402-anthropic 0.1.2
File Interpreter ABI Platform Actions
x402_anthropic-0.1.2-py3-none-any.whl Python 3 none any Details for x402_anthropic-0.1.2-py3-none-any.whl

Total release size: 17.4 kB

Release files / x402_anthropic-0.1.2.tar.gz

Download URL x402_anthropic-0.1.2.tar.gz
Size 11.0 kB
Tags Source
SHA-256 checksum
How to use checksums
e77e3cf9fbb76aa6fa03266d0d0c9d464cc9cf8037c6f4637727c5be1b4532fc
BLAKE2b-256 checksum
How to use checksums
6d20858908c4de4fd3614119014474faa9f6f17c8e90abccfb2adcdbbabe6db5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / x402_anthropic-0.1.2-py3-none-any.whl

Download URL x402_anthropic-0.1.2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3629ed8149e4854c11ebc8f177727cd5ad07d1f0ead64e81c7f594fdfe44adf2
BLAKE2b-256 checksum
How to use checksums
6e5e743a2e33358598b9bc6bf6a35ea73774250681c353722708064137879160
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for x402-anthropic 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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