This directory contains advanced x402 client examples demonstrating hooks, custom selectors, builder patterns, and spend controls across EVM, SVM, and TVM networks.
- Python 3.11+
- At least one configured signer:
- EVM private key with testnet funds (e.g., Base Sepolia)
- SVM private key with Solana Devnet funds
- TVM private key with TON testnet funds and testnet USDT
- A running x402 resource server (e.g., the FastAPI example server)
To fund your TVM payer wallet, request testnet TON from @testgiver_ton_bot for fees. Then open the testnet USDT transfer link or scan the QR code below to obtain testnet USDT:

-
Install dependencies:
cd examples/python/clients/advanced uv sync -
Configure environment:
cp .env-local .env # Edit .env and add one or more signer credentials -
Start a test server (in another terminal):
cd examples/python/servers/fastapi uv sync && uv run uvicorn main:app --port 4021
Use the CLI to run specific examples:
# Run a specific example
uv run python all_networks.py
uv run python index.py hooks
uv run python index.py preferred_network
uv run python index.py builder_pattern
uv run python index.py spend_controls
# Run all examples
uv run python index.py all
# List available examples
uv run python index.py --listOr run individual files directly:
uv run python all_networks.py
uv run python hooks.py
uv run python preferred_network.py
uv run python builder_pattern.py
uv run python spend_controls.pyDemonstrates how to add all supported networks with optional chain configuration, including TVM.
Demonstrates payment lifecycle hooks for logging, validation, and error recovery:
on_before_payment_creation- Called before payment creation, can aborton_after_payment_creation- Called after successful paymenton_payment_creation_failure- Called on failure, can recover
Use cases:
- Logging payment events for debugging
- Custom validation before allowing payments
- Metrics and analytics collection
- Error recovery with fallback payloads
Shows how to implement a custom payment requirements selector:
- Define network preference order (e.g., prefer L2 over L1)
- Automatic fallback to supported alternatives
- Useful for cost optimization or user preferences
Use cases:
- Prefer cheaper networks (Base > Ethereum)
- User-configurable network preferences
- Wallet UI with network selection
Demonstrates network-specific scheme registration:
- Different signers for different networks
- Wildcard patterns (
eip155:*) with specific overrides (eip155:1) - Separate keys for mainnet vs testnet
Use cases:
- Production vs development key separation
- Multi-network wallet support
- Network-specific signer configurations
By default the client caps recognized pegged assets at $1 and rejects everything else. Use spend_controls to raise the cap or opt into non-default tokens.
client = x402Client.from_config(
x402ClientConfig(
schemes=[SchemeRegistration(network="eip155:*", client=ExactEvmScheme(signer))],
spend_controls={
"max_amount_per_payment": "$1", # default USD cap on recognized pegged assets
"allowed_assets": [
# opt-in non-default with atomic cap
{"network": "eip155:*", "asset": "0xCustomToken", "max_amount_per_payment": "2000000"},
# opt-in non-default uncapped
{"network": "eip155:*", "asset": "0xOtherToken"},
# override USD cap for a default asset by ticker (or on-chain id)
{"network": "eip155:*", "asset": "USDC", "max_amount_per_payment": "1000000"},
],
},
)
)| Control | Purpose |
|---|---|
max_amount_per_payment |
USD ceiling on recognized pegged assets (default $1). Set False to remove. |
allowed_assets |
Opt-in for non-default tokens. List of { network, asset } with optional atomic max_amount_per_payment, or True to allow any asset. |
spend_controls: False |
Disable all spend controls. Use only for UI-confirmed flows (paywall). |
Use cases:
- Bound spend against a malicious 402 or unbounded custom token
- Allow a specific custom token without disabling the USD cap on stables
- Override the cap for one ticker (e.g. PYUSD) without raising it globally
advanced/
├── .env-local # Environment template
├── README.md # This file
├── all_networks.py # Register EVM, SVM, and TVM schemes
├── pyproject.toml # Dependencies
├── index.py # CLI entry point
├── hooks.py # Lifecycle hooks example
├── preferred_network.py # Custom selector example
├── builder_pattern.py # Network registration example
└── spend_controls.py # Spend controls example
- Use hooks for observability - Log payment events for debugging and metrics
- Configure network preferences - Users may prefer specific networks
- Separate keys per environment - Don't use production keys for testing