Skip to main content

x402 Payment Protocol SDK for Python

Project description

x402 Python SDK

Core implementation of the x402 payment protocol. Provides transport-agnostic client, server, and facilitator components with both async and sync variants.

Installation

Install the core package with your preferred framework/client:

# HTTP clients (pick one)
uv add x402[httpx]      # httpx client
uv add x402[requests]   # requests client

# Server frameworks (pick one)
uv add x402[fastapi]    # FastAPI middleware
uv add x402[flask]      # Flask middleware

# Blockchain mechanisms (pick one or both)
uv add x402[evm]        # EVM/Ethereum
uv add x402[svm]        # Solana

# Multiple extras
uv add x402[fastapi,httpx,evm]

# Everything
uv add x402[all]

Quick Start

Client (Async)

from x402 import x402Client
from x402.mechanisms.evm.exact import ExactEvmScheme

client = x402Client()
client.register("eip155:*", ExactEvmScheme(signer=my_signer))

# Create payment from 402 response
payload = await client.create_payment_payload(payment_required)

Client (Sync)

from x402 import x402ClientSync
from x402.mechanisms.evm.exact import ExactEvmScheme

client = x402ClientSync()
client.register("eip155:*", ExactEvmScheme(signer=my_signer))

payload = client.create_payment_payload(payment_required)

Server (Async)

from x402 import x402ResourceServer, ResourceConfig
from x402.http import HTTPFacilitatorClient
from x402.mechanisms.evm.exact import ExactEvmServerScheme

facilitator = HTTPFacilitatorClient(url="https://x402.org/facilitator")
server = x402ResourceServer(facilitator)
server.register("eip155:*", ExactEvmServerScheme())
server.initialize()

# Build requirements
config = ResourceConfig(
    scheme="exact",
    network="eip155:8453",
    pay_to="0x...",
    price="$0.01",
)
requirements = server.build_payment_requirements(config)

# Verify payment
result = await server.verify_payment(payload, requirements[0])

Server (Sync)

from x402 import x402ResourceServerSync, ResourceConfig
from x402.http import HTTPFacilitatorClientSync
from x402.mechanisms.evm.exact import ExactEvmServerScheme

facilitator = HTTPFacilitatorClientSync(url="https://x402.org/facilitator")
server = x402ResourceServerSync(facilitator)
server.register("eip155:*", ExactEvmServerScheme())
server.initialize()

result = server.verify_payment(payload, requirements[0])

Facilitator (Async)

from x402 import x402Facilitator
from x402.mechanisms.evm.exact import ExactEvmFacilitatorScheme

facilitator = x402Facilitator()
facilitator.register(
    ["eip155:8453", "eip155:84532"],
    ExactEvmFacilitatorScheme(wallet=wallet),
)

result = await facilitator.verify(payload, requirements)
if result.is_valid:
    settle_result = await facilitator.settle(payload, requirements)

Facilitator (Sync)

from x402 import x402FacilitatorSync
from x402.mechanisms.evm.exact import ExactEvmFacilitatorScheme

facilitator = x402FacilitatorSync()
facilitator.register(
    ["eip155:8453", "eip155:84532"],
    ExactEvmFacilitatorScheme(wallet=wallet),
)

result = facilitator.verify(payload, requirements)

Async vs Sync

Each component has both async and sync variants:

Async (default) Sync
x402Client x402ClientSync
x402ResourceServer x402ResourceServerSync
x402Facilitator x402FacilitatorSync
HTTPFacilitatorClient HTTPFacilitatorClientSync

Async variants support both sync and async hooks (auto-detected). Sync variants only support sync hooks and raise TypeError if async hooks are registered.

Framework Pairing

Framework HTTP Client Server Facilitator Client
FastAPI httpx x402ResourceServer HTTPFacilitatorClient
Flask requests x402ResourceServerSync HTTPFacilitatorClientSync

Mismatched variants raise TypeError at runtime.

Client Configuration

Use from_config() for declarative setup:

from x402 import x402Client, x402ClientConfig, SchemeRegistration

config = x402ClientConfig(
    schemes=[
        SchemeRegistration(network="eip155:*", client=ExactEvmScheme(signer)),
        SchemeRegistration(network="solana:*", client=ExactSvmScheme(signer)),
    ],
    policies=[prefer_network("eip155:8453")],
)
client = x402Client.from_config(config)

Policies

Filter or prioritize payment requirements:

from x402 import prefer_network, prefer_scheme, max_amount

client.register_policy(prefer_network("eip155:8453"))
client.register_policy(prefer_scheme("exact"))
client.register_policy(max_amount(1_000_000))  # 1 USDC max

Lifecycle Hooks

Client Hooks

from x402 import AbortResult, RecoveredPayloadResult

def before_payment(ctx):
    print(f"Creating payment for: {ctx.selected_requirements.network}")
    # Return AbortResult(reason="...") to cancel

def after_payment(ctx):
    print(f"Payment created: {ctx.payment_payload}")

def on_failure(ctx):
    print(f"Payment failed: {ctx.error}")
    # Return RecoveredPayloadResult(payload=...) to recover

client.on_before_payment_creation(before_payment)
client.on_after_payment_creation(after_payment)
client.on_payment_creation_failure(on_failure)

Server Hooks

server.on_before_verify(lambda ctx: print(f"Verifying: {ctx.payload}"))
server.on_after_verify(lambda ctx: print(f"Result: {ctx.result.is_valid}"))
server.on_verify_failure(lambda ctx: print(f"Failed: {ctx.error}"))

server.on_before_settle(lambda ctx: ...)
server.on_after_settle(lambda ctx: ...)
server.on_settle_failure(lambda ctx: ...)

Facilitator Hooks

facilitator.on_before_verify(...)
facilitator.on_after_verify(...)
facilitator.on_verify_failure(...)
facilitator.on_before_settle(...)
facilitator.on_after_settle(...)
facilitator.on_settle_failure(...)

Network Pattern Matching

Register handlers for network families using wildcards:

# All EVM networks
client.register("eip155:*", ExactEvmScheme(signer))

# Specific network (takes precedence)
client.register("eip155:8453", CustomScheme())

HTTP Headers

V2 Protocol (Current)

Header Description
PAYMENT-SIGNATURE Base64-encoded payment payload
PAYMENT-REQUIRED Base64-encoded payment requirements
PAYMENT-RESPONSE Base64-encoded settlement response

V1 Protocol (Legacy)

Header Description
X-PAYMENT Base64-encoded payment payload
X-PAYMENT-RESPONSE Base64-encoded settlement response

Related Modules

  • x402.http - HTTP clients, middleware, and facilitator client
  • x402.mechanisms.evm - EVM/Ethereum implementation
  • x402.mechanisms.svm - Solana implementation
  • x402.extensions - Protocol extensions (Bazaar discovery)

Examples

See examples/python.

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-2.6.0.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

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

x402-2.6.0-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

Details for the file x402-2.6.0.tar.gz.

File metadata

  • Download URL: x402-2.6.0.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for x402-2.6.0.tar.gz
Algorithm Hash digest
SHA256 387b7bbc273558fb83ff36596a76a7ebd30089d3697a0ed9ba14d31de5c168ff
MD5 035c92ec9f90d1ce98da606c8dc74162
BLAKE2b-256 200025d94128c69b83027a00803372d528066a5421bda55723f7518fa8cbfd6d

See more details on using hashes here.

Provenance

The following attestation bundles were made for x402-2.6.0.tar.gz:

Publisher: publish_pypi_x402.yml on x402-foundation/x402

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file x402-2.6.0-py3-none-any.whl.

File metadata

  • Download URL: x402-2.6.0-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for x402-2.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ee0d715d5af53128faa4d84692a8a273a1e29ba1023ef3af5373e64c38882d1
MD5 76b8456ea1dffdd9eb75aceb441b8ab3
BLAKE2b-256 fbbe3534b5027cee66052f82c5b7ff789d509ea3203c3fae855dc15a4ecac094

See more details on using hashes here.

Provenance

The following attestation bundles were made for x402-2.6.0-py3-none-any.whl:

Publisher: publish_pypi_x402.yml on x402-foundation/x402

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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