Skip to main content

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           # base
pip install "x402-anthropic[evm]"    # + Base/Ethereum
pip install "x402-anthropic[svm]"    # + Solana

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

License

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_anthropic-0.1.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

x402_anthropic-0.1.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file x402_anthropic-0.1.0.tar.gz.

File metadata

  • Download URL: x402_anthropic-0.1.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for x402_anthropic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a47f08a7d00c60cbff39bc87a3929b4e6c04f848045d9ba15505365e7fd4b0b
MD5 02aef2cc87659e17b814d88005cc03f2
BLAKE2b-256 7fb105764e00a9362dc8119d28ba5716e4da85243867b5d5ef2549344abaa717

See more details on using hashes here.

File details

Details for the file x402_anthropic-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: x402_anthropic-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for x402_anthropic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2992e7739e7976f5a450d222c01bfe28ae3173872669e9d4a4fc01eb48ca9856
MD5 b49fed0fd58ecc53397923c2b18c2840
BLAKE2b-256 6fd99ca41e6b35ffa170821c1fe096d4a706728a670ebc8b3ccca2902fc3c0c2

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