x402 Python SDK with Algorand Integration
Core implementation of the x402 payment protocol with first-class Algorand (AVM) support. 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-avm[httpx] # httpx client
uv add x402-avm[requests] # requests client
# Server frameworks (pick one)
uv add x402-avm[fastapi] # FastAPI middleware
uv add x402-avm[flask] # Flask middleware
# Blockchain mechanisms (pick one or more)
uv add x402-avm[avm] # Algorand
uv add x402-avm[evm] # EVM/Ethereum
uv add x402-avm[svm] # Solana
# Multiple extras
uv add x402-avm[fastapi,httpx,avm]
# Everything
uv add x402-avm[all]
Quick Start
Client (Async)
from x402 import x402Client
from x402.mechanisms.avm.exact import ExactAvmScheme
client = x402Client()
client.register("algorand:*", ExactAvmScheme(signer=my_avm_signer))
# Create payment from 402 response
payload = await client.create_payment_payload(payment_required)
Client (Sync)
from x402 import x402ClientSync
from x402.mechanisms.avm.exact import ExactAvmScheme
client = x402ClientSync()
client.register("algorand:*", ExactAvmScheme(signer=my_avm_signer))
payload = client.create_payment_payload(payment_required)
Server (Async)
from x402 import x402ResourceServer, ResourceConfig
from x402.http import HTTPFacilitatorClient, FacilitatorConfig
from x402.mechanisms.avm.exact import ExactAvmServerScheme
facilitator = HTTPFacilitatorClient(FacilitatorConfig(url="https://x402.org/facilitator"))
server = x402ResourceServer(facilitator)
server.register("algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=", ExactAvmServerScheme())
# Build requirements
config = ResourceConfig(
scheme="exact",
network="algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=",
pay_to="ALGO_ADDRESS...",
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
from x402.http import HTTPFacilitatorClientSync, FacilitatorConfig
from x402.mechanisms.avm.exact import ExactAvmServerScheme
facilitator = HTTPFacilitatorClientSync(FacilitatorConfig(url="https://x402.org/facilitator"))
server = x402ResourceServerSync(facilitator)
server.register("algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=", ExactAvmServerScheme())
result = server.verify_payment(payload, requirements[0])
Facilitator (Async)
from x402 import x402Facilitator
from x402.mechanisms.avm.exact import ExactAvmFacilitatorScheme
from x402.mechanisms.avm import ALGORAND_TESTNET_CAIP2
facilitator = x402Facilitator()
facilitator.register(
[ALGORAND_TESTNET_CAIP2],
ExactAvmFacilitatorScheme(signer=my_avm_facilitator_signer),
)
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.avm.exact import ExactAvmFacilitatorScheme
from x402.mechanisms.avm import ALGORAND_TESTNET_CAIP2
facilitator = x402FacilitatorSync()
facilitator.register(
[ALGORAND_TESTNET_CAIP2],
ExactAvmFacilitatorScheme(signer=my_avm_facilitator_signer),
)
result = facilitator.verify(payload, requirements)
Multi-Chain Registration
Register multiple blockchain mechanisms together:
from x402 import x402Client
from x402.mechanisms.avm.exact import ExactAvmScheme
from x402.mechanisms.evm.exact import ExactEvmScheme
from x402.mechanisms.svm.exact import ExactSvmScheme
client = x402Client()
client.register("algorand:*", ExactAvmScheme(signer=avm_signer))
client.register("eip155:*", ExactEvmScheme(signer=evm_signer))
client.register("solana:*", ExactSvmScheme(signer=svm_signer))
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="algorand:*", client=ExactAvmScheme(avm_signer)),
SchemeRegistration(network="eip155:*", client=ExactEvmScheme(evm_signer)),
SchemeRegistration(network="solana:*", client=ExactSvmScheme(svm_signer)),
],
policies=[prefer_network("algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=")],
)
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("algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI="))
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 Algorand networks
client.register("algorand:*", ExactAvmScheme(signer))
# Specific network (takes precedence)
client.register("algorand:SGO1GKSzyE7IEPItTxCByw9x8FmnrCDexi9/cOUJOiI=", TestnetScheme())
# All EVM networks
client.register("eip155:*", ExactEvmScheme(signer))
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 clientx402.mechanisms.avm- Algorand implementationx402.mechanisms.evm- EVM/Ethereum implementationx402.mechanisms.svm- Solana implementationx402.extensions- Protocol extensions (Bazaar discovery)
Examples
See examples/python.
Metadata
Release files for x402-avm 2.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| x402_avm-2.0.2.tar.gz | 1.6 MB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| x402_avm-2.0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 3.2 MB
Release files / x402_avm-2.0.2.tar.gz
| Download URL | x402_avm-2.0.2.tar.gz |
|---|---|
| Size | 1.6 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fe4b3ac8e9da1b7b8524f992167a9c4fd1900c1416d22211b2772eccae16f00d
|
|
BLAKE2b-256 checksum How to use checksums |
727f74428d64e4a06efdd1f53a1f23e1f1e739637f34eb0c1215f5d6c53608d9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Hatch/1.16.3 cpython/3.14.0 HTTPX/0.28.1
|
Release files / x402_avm-2.0.2-py3-none-any.whl
| Download URL | x402_avm-2.0.2-py3-none-any.whl |
|---|---|
| Size | 1.6 MB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8887a91cfe6c6166a475546e0931af09694e240955536e89e331e117aaba99ca
|
|
BLAKE2b-256 checksum How to use checksums |
d137e4669d0e49473d154a0dd01dfa18522c1cec6cec7c7fc73368dac041ccbc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Hatch/1.16.3 cpython/3.14.0 HTTPX/0.28.1
|
Security for x402-avm 2.0.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.