x402 payment transport for the Anthropic Python SDK
Project description
x402-anthropic-python
x402 payment transport for the Anthropic Python SDK.
Server returns 402 → client signs a USDC authorization → retries with payment header. Your code doesn't change. 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
Usage
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, Claude!"}],
)
print(message.content[0].text)
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())
Policy configuration
from x402 import preferNetwork, maxAmount
client = X402Anthropic(
wallet=wallet,
policies=[preferNetwork("eip155:8453"), maxAmount(1_000_000)], # Base, max $1 USDC
base_url="...",
)
Related
- qntx/x402-openai-python — OpenAI SDK equivalent
- coinbase/x402 — x402 protocol spec and reference implementation
- @x402/fetch — TypeScript equivalent
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file x402_anthropic-0.1.1.tar.gz.
File metadata
- Download URL: x402_anthropic-0.1.1.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86c4cd56287e329efa61af0644aaa84c821b6c497c3bd0e1eb71f3ec56526a07
|
|
| MD5 |
77691049763638f54df8d8d39d00df7e
|
|
| BLAKE2b-256 |
f00583a0aecfee4779888406dddee2456d867943e1adf51d90c4f4a8ef5fb651
|
File details
Details for the file x402_anthropic-0.1.1-py3-none-any.whl.
File metadata
- Download URL: x402_anthropic-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e5cb927f3a54e99e0c29f0f3c0d8ff97799d13b746bdf58e8a4acde07551040f
|
|
| MD5 |
94a01dd570845fe2ddf4d9f7447323db
|
|
| BLAKE2b-256 |
9f85865b3902f378eae825e98e4392624e4f9356f956020f4852fe8d3b59d22d
|