Skip to content

Conversation

@1bcMax
Copy link
Contributor

@1bcMax 1bcMax commented Jan 11, 2026

Summary

Adds 5 advanced Python client examples demonstrating x402 SDK v2 features.

Part of #841 - addresses TODO item: "Add advanced/custom Python client examples"

Examples

Example Description
hooks.py Payment lifecycle hooks (before, after, failure callbacks)
preferred_network.py Custom network preference selector
builder_pattern.py Network-specific registration with wildcards
error_recovery.py Error classification and recovery strategies
custom_transport.py Custom httpx transport with retry/timing

Features

  • CLI entry point (index.py) for running examples
  • Comprehensive test suite (40 unit tests passing)
  • E2E integration tests
  • README documentation

Test Results

============================= test session starts ==============================
collected 40 items

tests/test_builder_pattern.py ........                                   [ 20%]
tests/test_custom_transport.py .........                                 [ 42%]
tests/test_error_recovery.py ...........                                 [ 72%]
tests/test_hooks.py .......                                              [ 90%]
tests/test_preferred_network.py ....                                     [100%]

============================== 40 passed in 0.51s ==============================

Usage

cd examples/python/clients/advanced
cp .env-local .env  # Add your private key

uv sync
uv run python index.py hooks    # Run specific example
uv run python index.py all      # Run all examples
uv run pytest tests/ -v         # Run tests

Checklist

  • Commit signed
  • Based on feat/python-v2-sdk branch
  • Separate directory structure
  • pyproject.toml with uv sync support
  • Environment variable validation
  • Unit tests passing (40/40)

Implements 5 advanced patterns demonstrating x402 SDK v2 features:
- hooks.py: Payment lifecycle hooks (before, after, failure)
- preferred_network.py: Custom network preference selector
- builder_pattern.py: Network-specific registration with wildcards
- error_recovery.py: Error classification and recovery strategies
- custom_transport.py: Custom httpx transport with retry/timing

Includes:
- CLI entry point (index.py) for running examples
- Comprehensive test suite (40 unit tests)
- E2E integration tests
- README documentation

All tests passing.
@vercel
Copy link

vercel bot commented Jan 11, 2026

@1bcMax is attempting to deploy a commit to the Coinbase Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions bot added the examples Changes to examples label Jan 11, 2026
@phdargen phdargen self-assigned this Jan 12, 2026
Comment on lines 8 to 11
"x402",
"eth-account>=0.13.0",
"python-dotenv>=1.0.0",
"httpx>=0.27.0",
Copy link
Contributor

@phdargen phdargen Jan 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use the new import pattern for optional dependencies introduced in #901, see eg https://github.com/coinbase/x402/blob/feat/python-v2-sdk/examples/python/clients/httpx/pyproject.toml

Suggested change
"x402",
"eth-account>=0.13.0",
"python-dotenv>=1.0.0",
"httpx>=0.27.0",
"x402[evm,svm,httpx]",
"python-dotenv>=1.0.0",

@phdargen
Copy link
Contributor

Please remove all the tests, these belong in the sdk, not the examples. If you identified any missing test coverage in the sdk, please move them to python/x402/tests

Comment on lines 33 to 39
NETWORK_PREFERENCES = [
"eip155:8453", # Base mainnet (preferred - low fees)
"eip155:42161", # Arbitrum One
"eip155:10", # Optimism
"eip155:1", # Ethereum mainnet (fallback)
"eip155:84532", # Base Sepolia (testnet)
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also include svm here

@phdargen
Copy link
Contributor

Suggest renaming custom_transport.py -> retry_transport.py

@phdargen
Copy link
Contributor

Suggest renaming custom_transport.py -> retry_transport.py

Actually custom_transport.py fails on runtime with TypeError: AsyncClient.__init__() got an unexpected keyword argument 'httpx_client'. Also not sure about the error_recovery.py example, it doesn't seem to implement any recovery and thus is somewhat duplicate to hooks.py. In any case, the custom_transport.py and error_recovery.py examples go beyond what we currently have in the typescript sdk. I think for the python v2 release it would be best to aim for typescript parity and remove these 2 for now. After the release, we can then add additional examples for all sdks simultaneously, if desired

@phdargen
Copy link
Contributor

Hi @1bcMax, thanks a lot for your contribution! Please have a look at my comments above

Address PR review feedback:
- Remove tests directory (tests belong in SDK, not examples)
- Remove custom_transport.py (runtime error, beyond TS parity)
- Remove error_recovery.py (duplicate of hooks.py, beyond TS parity)
- Update index.py, README.md, pyproject.toml accordingly
@1bcMax
Copy link
Contributor Author

1bcMax commented Jan 12, 2026

@phdargen Hey my buddy, i have moved the test code as you suggest.

@phdargen
Copy link
Contributor

Great thanks @1bcMax, could you please also address the other 2 comments above (imports and svm)?

- Update pyproject.toml to use x402[evm,svm,httpx] instead of
  listing dependencies separately
- Add Solana/SVM network examples to preferred_network.py
- Support both EVM and SVM signers for cross-chain flexibility
- Update .env-local with SOLANA_PRIVATE_KEY option
@1bcMax
Copy link
Contributor Author

1bcMax commented Jan 13, 2026

Addressed the feedback:

  • Updated pyproject.toml to use x402[evm,svm,httpx] optional dependencies
  • Added Solana/SVM network examples to preferred_network.py alongside EVM chains
  • The example now supports both EVM and Solana signers for cross-chain flexibility

Ready for re-review!

@phdargen
Copy link
Contributor

Thanks for the update @1bcMax, LGTM 🚀

@phdargen phdargen merged commit ee85c65 into coinbase:feat/python-v2-sdk Jan 13, 2026
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

examples Changes to examples

Development

Successfully merging this pull request may close these issues.

2 participants