Skip to content

kinance/x402-anthropic-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 prefer_network, max_amount

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

Safety: use a dedicated wallet with limited funds and always set a max_amount policy before pointing this at an untrusted gateway.

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

About

x402 payment transport for the Anthropic Python SDK

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages