feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
"""x402 Python Facilitator for E2E Testing.
|
|
|
|
|
|
|
|
|
|
FastAPI-based facilitator service that verifies and settles payments
|
|
|
|
|
on-chain for the x402 protocol.
|
|
|
|
|
|
|
|
|
|
Supports:
|
|
|
|
|
- EVM networks (Base Sepolia) via web3.py
|
|
|
|
|
- SVM networks (Solana Devnet) via solders
|
2026-05-08 17:13:22 +03:00
|
|
|
- TVM networks (TON testnet/mainnet) via pytoniq + Toncenter/TonAPI
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
- Bazaar discovery extension for resource cataloging
|
2026-03-19 11:17:34 -07:00
|
|
|
- EIP-2612 gas sponsoring extension (gasless Permit2 approval via permit)
|
|
|
|
|
- ERC-20 approval gas sponsoring extension (gasless Permit2 via signed tx relay)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
- V1 and V2 protocol versions
|
|
|
|
|
|
|
|
|
|
Run with: uv run uvicorn main:app --port 4022
|
|
|
|
|
"""
|
|
|
|
|
|
2026-03-19 11:17:34 -07:00
|
|
|
import logging
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-03-19 11:17:34 -07:00
|
|
|
logging.basicConfig(level=logging.INFO, format="%(name)s %(levelname)s: %(message)s")
|
|
|
|
|
logging.getLogger("x402.permit2").setLevel(logging.DEBUG)
|
|
|
|
|
logging.getLogger("x402.signers").setLevel(logging.DEBUG)
|
|
|
|
|
|
2026-04-29 12:19:09 -05:00
|
|
|
from bazaar import BazaarCatalog
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
from dotenv import load_dotenv
|
|
|
|
|
from fastapi import FastAPI, HTTPException
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
from solders.keypair import Keypair
|
|
|
|
|
|
|
|
|
|
from x402 import x402Facilitator
|
2026-02-11 18:46:46 -05:00
|
|
|
from x402.extensions.bazaar import extract_discovery_info
|
2026-03-19 11:17:34 -07:00
|
|
|
from x402.extensions.eip2612_gas_sponsoring import EIP2612_GAS_SPONSORING
|
|
|
|
|
from x402.extensions.erc20_approval_gas_sponsoring import (
|
|
|
|
|
Erc20ApprovalFacilitatorExtension,
|
|
|
|
|
WriteContractCall,
|
|
|
|
|
)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
from x402.mechanisms.evm import FacilitatorWeb3Signer
|
2026-03-19 11:17:34 -07:00
|
|
|
from x402.mechanisms.evm.constants import TX_STATUS_SUCCESS
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
from x402.mechanisms.evm.exact import register_exact_evm_facilitator
|
2026-03-19 11:17:34 -07:00
|
|
|
from x402.mechanisms.evm.types import TransactionReceipt
|
2026-04-29 12:19:09 -05:00
|
|
|
from x402.mechanisms.evm.upto import UptoEvmFacilitatorScheme
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
from x402.mechanisms.svm import FacilitatorKeypairSigner
|
|
|
|
|
from x402.mechanisms.svm.exact import register_exact_svm_facilitator
|
2026-05-08 17:13:22 +03:00
|
|
|
from x402.mechanisms.tvm import (
|
|
|
|
|
TVM_TESTNET,
|
|
|
|
|
TVM_PROVIDER_TONAPI,
|
|
|
|
|
HighloadV3Config,
|
|
|
|
|
FacilitatorHighloadV3Signer,
|
|
|
|
|
)
|
|
|
|
|
from x402.mechanisms.tvm.exact import ExactTvmFacilitatorScheme
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
|
|
|
|
# Load environment variables
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
|
|
# Configuration
|
|
|
|
|
PORT = int(os.environ.get("PORT", "4022"))
|
|
|
|
|
|
|
|
|
|
# Initialize bazaar catalog
|
|
|
|
|
bazaar_catalog = BazaarCatalog()
|
|
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
# Validate that at least one chain is configured
|
|
|
|
|
if not any(
|
|
|
|
|
[
|
|
|
|
|
os.environ.get("EVM_PRIVATE_KEY"),
|
|
|
|
|
os.environ.get("SVM_PRIVATE_KEY"),
|
|
|
|
|
os.environ.get("TVM_PRIVATE_KEY"),
|
|
|
|
|
]
|
|
|
|
|
):
|
|
|
|
|
print(
|
|
|
|
|
"❌ At least one of EVM_PRIVATE_KEY, SVM_PRIVATE_KEY, or TVM_PRIVATE_KEY is required"
|
|
|
|
|
)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
# Network configuration
|
|
|
|
|
EVM_NETWORK = os.environ.get("EVM_NETWORK", "eip155:84532")
|
|
|
|
|
SVM_NETWORK = os.environ.get("SVM_NETWORK", "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1")
|
|
|
|
|
TVM_NETWORK = os.environ.get("TVM_NETWORK", TVM_TESTNET)
|
|
|
|
|
|
|
|
|
|
# Initialize the EVM signer from private key when configured
|
|
|
|
|
evm_signer = None
|
|
|
|
|
if os.environ.get("EVM_PRIVATE_KEY"):
|
|
|
|
|
evm_rpc_url = os.environ.get("EVM_RPC_URL") or "https://sepolia.base.org"
|
|
|
|
|
evm_signer = FacilitatorWeb3Signer(
|
|
|
|
|
private_key=os.environ["EVM_PRIVATE_KEY"],
|
|
|
|
|
rpc_url=evm_rpc_url,
|
|
|
|
|
)
|
|
|
|
|
print(f"EVM Facilitator account: {evm_signer.get_addresses()[0]}")
|
|
|
|
|
|
|
|
|
|
# Initialize the SVM signer from private key when configured
|
|
|
|
|
svm_signer = None
|
|
|
|
|
if os.environ.get("SVM_PRIVATE_KEY"):
|
|
|
|
|
svm_keypair = Keypair.from_base58_string(os.environ["SVM_PRIVATE_KEY"])
|
|
|
|
|
svm_signer = FacilitatorKeypairSigner(svm_keypair)
|
|
|
|
|
print(f"SVM Facilitator account: {svm_signer.get_addresses()[0]}")
|
|
|
|
|
|
|
|
|
|
# Initialize the TVM signer from private key when configured
|
|
|
|
|
tvm_signer = None
|
|
|
|
|
if os.environ.get("TVM_PRIVATE_KEY"):
|
|
|
|
|
tvm_config = HighloadV3Config.from_private_key(os.environ["TVM_PRIVATE_KEY"])
|
|
|
|
|
tvm_provider = (os.environ.get("TVM_PROVIDER") or "").strip().lower()
|
|
|
|
|
tvm_config.provider = tvm_provider or tvm_config.provider
|
|
|
|
|
tvm_config.api_key = (
|
|
|
|
|
os.environ.get("TONAPI_API_KEY")
|
|
|
|
|
if tvm_provider == TVM_PROVIDER_TONAPI
|
|
|
|
|
else os.environ.get("TONCENTER_API_KEY")
|
|
|
|
|
)
|
|
|
|
|
tvm_config.provider_base_url = (
|
|
|
|
|
os.environ.get("TONAPI_BASE_URL")
|
|
|
|
|
if tvm_provider == TVM_PROVIDER_TONAPI
|
|
|
|
|
else os.environ.get("TONCENTER_BASE_URL")
|
|
|
|
|
)
|
|
|
|
|
tvm_signer = FacilitatorHighloadV3Signer({TVM_NETWORK: tvm_config})
|
|
|
|
|
print(f"TVM Facilitator account: {tvm_signer.get_addresses()[0]}")
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
|
|
|
|
|
2026-03-19 11:17:34 -07:00
|
|
|
class Erc20ApprovalSigner:
|
|
|
|
|
"""Wraps FacilitatorWeb3Signer with send_transactions for ERC-20 approval sponsoring.
|
|
|
|
|
|
|
|
|
|
Broadcasts pre-signed approval txs and settles via the proxy contract,
|
|
|
|
|
matching the Go/TS facilitator pattern.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, base_signer: FacilitatorWeb3Signer):
|
|
|
|
|
self._signer = base_signer
|
|
|
|
|
|
|
|
|
|
def send_transactions(self, transactions: list) -> list[str]:
|
|
|
|
|
hashes: list[str] = []
|
|
|
|
|
for tx in transactions:
|
|
|
|
|
if isinstance(tx, str):
|
fix: e2e approve bug (#1803)
* fix: e2e approve bug
* feat: remove new docstring addition
* fix: update Next.js e2e server to /exact/evm/... path structure
The `feat: add upto to typescript sdk` commit reverted proxy.ts and
test.config.json from the improved `/api/exact/evm/...` path structure
back to old `protected-*` paths, without recreating the deleted route
files. Fix this properly by:
- Update proxy.ts route keys and config.matcher to /exact/evm/... paths
- Update test.config.json to /exact/evm/... paths, preserving
coldstart: true on the two permit2 endpoints
- Add missing proxy route files at new paths:
exact/evm/eip3009/proxy, exact/evm/permit2/proxy,
exact/evm/permit2-eip2612GasSponsoring/proxy,
exact/evm/permit2-erc20ApprovalGasSponsoring/proxy,
exact/svm/proxy, exact/stellar/proxy, exact/aptos/proxy
Note: route files are in directories named "proxy" which matches a
root .gitignore pattern, so they are tracked with git add -f.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: flatten non-EVM chain routes to /exact/{chain} base path
SVM, Aptos, and Stellar each have a single transfer mechanism with no
variants, so the /proxy sub-path is unnecessary. Move them to the base
level /exact/{chain} pattern, matching EVM's approach of only adding
sub-paths when there are multiple mechanisms or extensions to test.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: pr cleanup and review
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 15:27:46 -07:00
|
|
|
raw_bytes = bytes.fromhex(tx[2:] if tx.startswith("0x") else tx)
|
|
|
|
|
w3 = self._signer._w3
|
|
|
|
|
|
|
|
|
|
payer_address = w3.eth.account.recover_transaction(tx)
|
|
|
|
|
# Use the same gas constants as the library's approve tx builder
|
2026-05-08 17:13:22 +03:00
|
|
|
gas_cost = (
|
|
|
|
|
70_000 * 1_000_000_000
|
|
|
|
|
) # ERC20_APPROVE_GAS_LIMIT * DEFAULT_MAX_FEE_PER_GAS
|
fix: e2e approve bug (#1803)
* fix: e2e approve bug
* feat: remove new docstring addition
* fix: update Next.js e2e server to /exact/evm/... path structure
The `feat: add upto to typescript sdk` commit reverted proxy.ts and
test.config.json from the improved `/api/exact/evm/...` path structure
back to old `protected-*` paths, without recreating the deleted route
files. Fix this properly by:
- Update proxy.ts route keys and config.matcher to /exact/evm/... paths
- Update test.config.json to /exact/evm/... paths, preserving
coldstart: true on the two permit2 endpoints
- Add missing proxy route files at new paths:
exact/evm/eip3009/proxy, exact/evm/permit2/proxy,
exact/evm/permit2-eip2612GasSponsoring/proxy,
exact/evm/permit2-erc20ApprovalGasSponsoring/proxy,
exact/svm/proxy, exact/stellar/proxy, exact/aptos/proxy
Note: route files are in directories named "proxy" which matches a
root .gitignore pattern, so they are tracked with git add -f.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: flatten non-EVM chain routes to /exact/{chain} base path
SVM, Aptos, and Stellar each have a single transfer mechanism with no
variants, so the /proxy sub-path is unnecessary. Move them to the base
level /exact/{chain} pattern, matching EVM's approach of only adding
sub-paths when there are multiple mechanisms or extensions to test.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: pr cleanup and review
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 15:27:46 -07:00
|
|
|
|
|
|
|
|
payer_balance = w3.eth.get_balance(payer_address)
|
|
|
|
|
if payer_balance < gas_cost:
|
|
|
|
|
deficit = gas_cost - payer_balance
|
2026-05-08 17:13:22 +03:00
|
|
|
print(
|
|
|
|
|
f"⛽ Funding payer {payer_address} with {deficit} wei for gas"
|
|
|
|
|
)
|
fix: e2e approve bug (#1803)
* fix: e2e approve bug
* feat: remove new docstring addition
* fix: update Next.js e2e server to /exact/evm/... path structure
The `feat: add upto to typescript sdk` commit reverted proxy.ts and
test.config.json from the improved `/api/exact/evm/...` path structure
back to old `protected-*` paths, without recreating the deleted route
files. Fix this properly by:
- Update proxy.ts route keys and config.matcher to /exact/evm/... paths
- Update test.config.json to /exact/evm/... paths, preserving
coldstart: true on the two permit2 endpoints
- Add missing proxy route files at new paths:
exact/evm/eip3009/proxy, exact/evm/permit2/proxy,
exact/evm/permit2-eip2612GasSponsoring/proxy,
exact/evm/permit2-erc20ApprovalGasSponsoring/proxy,
exact/svm/proxy, exact/stellar/proxy, exact/aptos/proxy
Note: route files are in directories named "proxy" which matches a
root .gitignore pattern, so they are tracked with git add -f.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: flatten non-EVM chain routes to /exact/{chain} base path
SVM, Aptos, and Stellar each have a single transfer mechanism with no
variants, so the /proxy sub-path is unnecessary. Move them to the base
level /exact/{chain} pattern, matching EVM's approach of only adding
sub-paths when there are multiple mechanisms or extensions to test.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: pr cleanup and review
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 15:27:46 -07:00
|
|
|
fund_tx = {
|
|
|
|
|
"to": payer_address,
|
|
|
|
|
"value": deficit,
|
|
|
|
|
"gas": 21000,
|
|
|
|
|
"gasPrice": w3.eth.gas_price,
|
2026-05-08 17:13:22 +03:00
|
|
|
"nonce": w3.eth.get_transaction_count(
|
|
|
|
|
self._signer._account.address
|
|
|
|
|
),
|
fix: e2e approve bug (#1803)
* fix: e2e approve bug
* feat: remove new docstring addition
* fix: update Next.js e2e server to /exact/evm/... path structure
The `feat: add upto to typescript sdk` commit reverted proxy.ts and
test.config.json from the improved `/api/exact/evm/...` path structure
back to old `protected-*` paths, without recreating the deleted route
files. Fix this properly by:
- Update proxy.ts route keys and config.matcher to /exact/evm/... paths
- Update test.config.json to /exact/evm/... paths, preserving
coldstart: true on the two permit2 endpoints
- Add missing proxy route files at new paths:
exact/evm/eip3009/proxy, exact/evm/permit2/proxy,
exact/evm/permit2-eip2612GasSponsoring/proxy,
exact/evm/permit2-erc20ApprovalGasSponsoring/proxy,
exact/svm/proxy, exact/stellar/proxy, exact/aptos/proxy
Note: route files are in directories named "proxy" which matches a
root .gitignore pattern, so they are tracked with git add -f.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: flatten non-EVM chain routes to /exact/{chain} base path
SVM, Aptos, and Stellar each have a single transfer mechanism with no
variants, so the /proxy sub-path is unnecessary. Move them to the base
level /exact/{chain} pattern, matching EVM's approach of only adding
sub-paths when there are multiple mechanisms or extensions to test.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: pr cleanup and review
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 15:27:46 -07:00
|
|
|
"chainId": w3.eth.chain_id,
|
|
|
|
|
}
|
|
|
|
|
signed_fund = self._signer._account.sign_transaction(fund_tx)
|
2026-05-08 17:13:22 +03:00
|
|
|
fund_hash = w3.eth.send_raw_transaction(
|
|
|
|
|
signed_fund.raw_transaction
|
|
|
|
|
).hex()
|
fix: e2e approve bug (#1803)
* fix: e2e approve bug
* feat: remove new docstring addition
* fix: update Next.js e2e server to /exact/evm/... path structure
The `feat: add upto to typescript sdk` commit reverted proxy.ts and
test.config.json from the improved `/api/exact/evm/...` path structure
back to old `protected-*` paths, without recreating the deleted route
files. Fix this properly by:
- Update proxy.ts route keys and config.matcher to /exact/evm/... paths
- Update test.config.json to /exact/evm/... paths, preserving
coldstart: true on the two permit2 endpoints
- Add missing proxy route files at new paths:
exact/evm/eip3009/proxy, exact/evm/permit2/proxy,
exact/evm/permit2-eip2612GasSponsoring/proxy,
exact/evm/permit2-erc20ApprovalGasSponsoring/proxy,
exact/svm/proxy, exact/stellar/proxy, exact/aptos/proxy
Note: route files are in directories named "proxy" which matches a
root .gitignore pattern, so they are tracked with git add -f.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* fix: flatten non-EVM chain routes to /exact/{chain} base path
SVM, Aptos, and Stellar each have a single transfer mechanism with no
variants, so the /proxy sub-path is unnecessary. Move them to the base
level /exact/{chain} pattern, matching EVM's approach of only adding
sub-paths when there are multiple mechanisms or extensions to test.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* feat: pr cleanup and review
---------
Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-27 15:27:46 -07:00
|
|
|
fund_receipt = w3.eth.wait_for_transaction_receipt(fund_hash)
|
|
|
|
|
if fund_receipt["status"] != 1:
|
|
|
|
|
raise RuntimeError(f"gas_funding_failed: {fund_hash}")
|
|
|
|
|
print(f"⛽ Gas funding confirmed: {fund_hash}")
|
|
|
|
|
|
|
|
|
|
tx_hash = w3.eth.send_raw_transaction(raw_bytes).hex()
|
2026-03-19 11:17:34 -07:00
|
|
|
elif isinstance(tx, dict) or isinstance(tx, WriteContractCall):
|
|
|
|
|
if isinstance(tx, dict):
|
|
|
|
|
call = WriteContractCall(**tx)
|
|
|
|
|
else:
|
|
|
|
|
call = tx
|
|
|
|
|
tx_hash = self._signer.write_contract(
|
|
|
|
|
call.address, call.abi, call.function, *call.args
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError(f"Unsupported transaction type: {type(tx)}")
|
|
|
|
|
|
|
|
|
|
receipt = self._signer.wait_for_transaction_receipt(tx_hash)
|
|
|
|
|
if receipt.status != TX_STATUS_SUCCESS:
|
|
|
|
|
raise RuntimeError(f"transaction_failed: {tx_hash}")
|
|
|
|
|
hashes.append(tx_hash)
|
|
|
|
|
return hashes
|
|
|
|
|
|
|
|
|
|
def wait_for_transaction_receipt(self, tx_hash: str) -> TransactionReceipt:
|
|
|
|
|
return self._signer.wait_for_transaction_receipt(tx_hash)
|
|
|
|
|
|
|
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
erc20_approval_signer = (
|
|
|
|
|
Erc20ApprovalSigner(evm_signer) if evm_signer is not None else None
|
|
|
|
|
)
|
2026-03-19 11:17:34 -07:00
|
|
|
|
|
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
def _handle_after_verify(ctx: Any) -> None:
|
|
|
|
|
"""Handle after verify hook - extract discovery info and catalog."""
|
|
|
|
|
print("✅ Payment verified")
|
|
|
|
|
|
|
|
|
|
# Extract discovered resource from payment for bazaar catalog
|
|
|
|
|
try:
|
|
|
|
|
discovered = extract_discovery_info(
|
|
|
|
|
ctx.payment_payload,
|
|
|
|
|
ctx.requirements,
|
|
|
|
|
validate=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if discovered:
|
|
|
|
|
print(f" 📝 Discovered resource: {discovered.resource_url}")
|
|
|
|
|
print(f" 📝 Method: {discovered.method}")
|
|
|
|
|
print(f" 📝 X402Version: {discovered.x402_version}")
|
|
|
|
|
|
|
|
|
|
# Convert discovery_info to dict for serialization
|
|
|
|
|
discovery_info_dict = None
|
|
|
|
|
if discovered.discovery_info:
|
|
|
|
|
if hasattr(discovered.discovery_info, "model_dump"):
|
|
|
|
|
discovery_info_dict = discovered.discovery_info.model_dump(
|
|
|
|
|
by_alias=True, exclude_none=True
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
discovery_info_dict = discovered.discovery_info
|
|
|
|
|
|
|
|
|
|
bazaar_catalog.catalog_resource(
|
|
|
|
|
resource_url=discovered.resource_url,
|
|
|
|
|
method=discovered.method,
|
|
|
|
|
x402_version=discovered.x402_version,
|
|
|
|
|
discovery_info=discovery_info_dict,
|
|
|
|
|
payment_requirements=ctx.requirements.model_dump(by_alias=True)
|
|
|
|
|
if hasattr(ctx.requirements, "model_dump")
|
|
|
|
|
else ctx.requirements,
|
2026-03-23 08:49:46 -07:00
|
|
|
route_template=getattr(discovered, "route_template", None),
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
)
|
|
|
|
|
print(" ✅ Added to bazaar catalog")
|
|
|
|
|
except Exception as err:
|
|
|
|
|
print(f" ⚠️ Failed to extract discovery info: {err}")
|
|
|
|
|
|
|
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
# Initialize the x402 Facilitator with optional EVM/SVM/TVM support
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
facilitator = (
|
|
|
|
|
x402Facilitator()
|
|
|
|
|
.on_before_verify(lambda ctx: print("Before verify", ctx))
|
|
|
|
|
.on_after_verify(lambda ctx: _handle_after_verify(ctx))
|
|
|
|
|
.on_verify_failure(lambda ctx: print("Verify failure", ctx))
|
|
|
|
|
.on_before_settle(lambda ctx: print("Before settle", ctx))
|
|
|
|
|
.on_after_settle(lambda ctx: print(f"🎉 Payment settled: {ctx.result.transaction}"))
|
|
|
|
|
.on_settle_failure(lambda ctx: print("Settle failure", ctx))
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Register EVM schemes (V1 and V2)
|
2026-05-08 17:13:22 +03:00
|
|
|
if evm_signer is not None:
|
|
|
|
|
register_exact_evm_facilitator(
|
|
|
|
|
facilitator,
|
|
|
|
|
evm_signer,
|
|
|
|
|
networks=EVM_NETWORK,
|
|
|
|
|
deploy_erc4337_with_eip6492=True,
|
|
|
|
|
)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
# Register upto EVM scheme (V2 only)
|
|
|
|
|
facilitator.register([EVM_NETWORK], UptoEvmFacilitatorScheme(evm_signer))
|
2026-04-16 12:38:54 -07:00
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
# Register SVM schemes (V1 and V2)
|
2026-05-08 17:13:22 +03:00
|
|
|
if svm_signer is not None:
|
|
|
|
|
register_exact_svm_facilitator(
|
|
|
|
|
facilitator,
|
|
|
|
|
svm_signer,
|
|
|
|
|
networks=SVM_NETWORK,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Register TVM schemes (V2)
|
|
|
|
|
if tvm_signer is not None:
|
|
|
|
|
facilitator.register(
|
|
|
|
|
[TVM_NETWORK],
|
|
|
|
|
ExactTvmFacilitatorScheme(tvm_signer),
|
|
|
|
|
)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
2026-03-19 11:17:34 -07:00
|
|
|
# Register gas sponsoring extensions
|
2026-05-08 17:13:22 +03:00
|
|
|
if evm_signer is not None and erc20_approval_signer is not None:
|
|
|
|
|
facilitator.register_extension(EIP2612_GAS_SPONSORING)
|
|
|
|
|
facilitator.register_extension(
|
|
|
|
|
Erc20ApprovalFacilitatorExtension(signer=erc20_approval_signer)
|
|
|
|
|
)
|
2026-03-19 11:17:34 -07:00
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
|
|
|
|
# Pydantic models for request/response
|
|
|
|
|
class VerifyRequest(BaseModel):
|
|
|
|
|
"""Verify endpoint request body."""
|
|
|
|
|
|
|
|
|
|
paymentPayload: dict
|
|
|
|
|
paymentRequirements: dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SettleRequest(BaseModel):
|
|
|
|
|
"""Settle endpoint request body."""
|
|
|
|
|
|
|
|
|
|
paymentPayload: dict
|
|
|
|
|
paymentRequirements: dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Initialize FastAPI app
|
|
|
|
|
app = FastAPI(
|
|
|
|
|
title="x402 Python Facilitator (E2E)",
|
|
|
|
|
description="Verifies and settles x402 payments on-chain for e2e testing",
|
|
|
|
|
version="2.0.0",
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/verify")
|
|
|
|
|
async def verify(request: VerifyRequest):
|
|
|
|
|
"""Verify a payment against requirements.
|
|
|
|
|
|
|
|
|
|
Note: Payment tracking and bazaar discovery are handled by lifecycle hooks.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
request: Payment payload and requirements to verify.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
VerifyResponse with isValid and payer (if valid) or invalidReason.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
from x402.schemas import parse_payment_payload, parse_payment_requirements
|
|
|
|
|
|
|
|
|
|
# Parse payload (auto-detects V1/V2) and requirements (based on payload version)
|
|
|
|
|
payload = parse_payment_payload(request.paymentPayload)
|
|
|
|
|
requirements = parse_payment_requirements(
|
|
|
|
|
payload.x402_version, request.paymentRequirements
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Hooks will automatically:
|
|
|
|
|
# - Track verified payment (on_after_verify)
|
|
|
|
|
# - Extract and catalog discovery info (on_after_verify)
|
|
|
|
|
response = await facilitator.verify(payload, requirements)
|
|
|
|
|
|
2026-03-19 11:17:34 -07:00
|
|
|
if not response.is_valid:
|
2026-05-08 17:13:22 +03:00
|
|
|
print(
|
|
|
|
|
f" ❌ Verify rejected: {response.invalid_reason} (payer={response.payer})"
|
|
|
|
|
)
|
2026-03-19 11:17:34 -07:00
|
|
|
|
2026-03-24 00:41:51 +09:00
|
|
|
return response.model_dump(by_alias=True, exclude_none=True)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
except Exception as e:
|
2026-03-19 11:17:34 -07:00
|
|
|
import traceback
|
2026-05-08 17:13:22 +03:00
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
print(f"Verify error: {e}")
|
2026-03-19 11:17:34 -07:00
|
|
|
traceback.print_exc()
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/settle")
|
|
|
|
|
async def settle(request: SettleRequest):
|
|
|
|
|
"""Settle a payment on-chain.
|
|
|
|
|
|
|
|
|
|
Note: Verification validation and cleanup are handled by lifecycle hooks.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
request: Payment payload and requirements to settle.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
SettleResponse with success, transaction, network, and payer.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
from x402.schemas import parse_payment_payload, parse_payment_requirements
|
|
|
|
|
|
|
|
|
|
# Parse payload (auto-detects V1/V2) and requirements (based on payload version)
|
|
|
|
|
payload = parse_payment_payload(request.paymentPayload)
|
|
|
|
|
requirements = parse_payment_requirements(
|
|
|
|
|
payload.x402_version, request.paymentRequirements
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
# Hooks will automatically:
|
|
|
|
|
# - Validate payment was verified (on_before_settle - will abort if not)
|
|
|
|
|
# - Check verification timeout (on_before_settle)
|
|
|
|
|
# - Clean up tracking (on_after_settle / on_settle_failure)
|
|
|
|
|
response = await facilitator.settle(payload, requirements)
|
|
|
|
|
|
2026-03-24 00:41:51 +09:00
|
|
|
return response.model_dump(by_alias=True, exclude_none=True)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Settle error: {e}")
|
|
|
|
|
|
|
|
|
|
# Check if this was an abort from hook
|
|
|
|
|
if "aborted" in str(e).lower() or "Settlement aborted" in str(e):
|
2026-03-24 00:41:51 +09:00
|
|
|
from x402.schemas import SettleResponse
|
|
|
|
|
|
|
|
|
|
abort = SettleResponse(
|
|
|
|
|
success=False,
|
|
|
|
|
error_reason=str(e).replace("Settlement aborted: ", ""),
|
|
|
|
|
network=request.paymentPayload.get("accepted", {}).get(
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
"network", "unknown"
|
|
|
|
|
),
|
2026-03-24 00:41:51 +09:00
|
|
|
transaction="",
|
|
|
|
|
)
|
|
|
|
|
return abort.model_dump(by_alias=True, exclude_none=True)
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/supported")
|
|
|
|
|
async def supported():
|
|
|
|
|
"""Get supported payment kinds and extensions.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
SupportedResponse with kinds, extensions, and signers.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
response = facilitator.get_supported()
|
|
|
|
|
|
|
|
|
|
return {
|
2026-05-08 17:13:22 +03:00
|
|
|
"kinds": [
|
|
|
|
|
k.model_dump(by_alias=True, exclude_none=True) for k in response.kinds
|
|
|
|
|
],
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
"extensions": response.extensions,
|
|
|
|
|
"signers": response.signers,
|
|
|
|
|
}
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Supported error: {e}")
|
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.get("/discovery/resources")
|
|
|
|
|
async def discovery_resources(limit: int = 100, offset: int = 0):
|
|
|
|
|
"""List all discovered resources from bazaar.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Discovery response with x402Version, items, and pagination.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return bazaar_catalog.get_resources(limit, offset)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Discovery error: {e}")
|
|
|
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
2026-04-29 12:19:09 -05:00
|
|
|
@app.get("/discovery/search")
|
|
|
|
|
async def discovery_search(query: str, type: str | None = None, limit: int | None = None):
|
|
|
|
|
"""Search discovered resources using keyword matching.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
query: The search query string (required).
|
|
|
|
|
type: Optional filter by resource type.
|
|
|
|
|
limit: Optional advisory maximum number of results.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Search response with x402Version, items, and optional pagination hints.
|
|
|
|
|
"""
|
|
|
|
|
try:
|
|
|
|
|
return bazaar_catalog.search_resources(query, type, limit)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Discovery search error: {e}")
|
|
|
|
|
raise HTTPException(status_code=500, detail=str(e)) from e
|
|
|
|
|
|
|
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
@app.get("/health")
|
|
|
|
|
async def health():
|
|
|
|
|
"""Health check endpoint."""
|
|
|
|
|
return {
|
|
|
|
|
"status": "ok",
|
2026-05-08 17:13:22 +03:00
|
|
|
"networks": [kind.network for kind in facilitator.get_supported().kinds],
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
"facilitator": "python",
|
|
|
|
|
"version": "2.0.0",
|
2026-03-19 11:17:34 -07:00
|
|
|
"extensions": facilitator.get_extensions(),
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
"discoveredResources": bazaar_catalog.get_count(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.post("/close")
|
|
|
|
|
async def close():
|
|
|
|
|
"""Graceful shutdown endpoint."""
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
print("Received shutdown request")
|
2026-05-08 17:13:22 +03:00
|
|
|
if tvm_signer is not None:
|
|
|
|
|
tvm_signer.close()
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
|
|
|
|
|
async def shutdown():
|
|
|
|
|
await asyncio.sleep(0.1)
|
|
|
|
|
os._exit(0)
|
|
|
|
|
|
|
|
|
|
asyncio.create_task(shutdown())
|
|
|
|
|
return {"message": "Facilitator shutting down gracefully"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
import uvicorn
|
|
|
|
|
|
2026-05-08 17:13:22 +03:00
|
|
|
supported_networks = [kind.network for kind in facilitator.get_supported().kinds]
|
|
|
|
|
active_extensions = facilitator.get_extensions()
|
|
|
|
|
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
print(f"""
|
|
|
|
|
╔════════════════════════════════════════════════════════╗
|
|
|
|
|
║ x402 Python Facilitator (E2E) ║
|
|
|
|
|
╠════════════════════════════════════════════════════════╣
|
|
|
|
|
║ Server: http://localhost:{PORT} ║
|
2026-05-08 17:13:22 +03:00
|
|
|
║ Networks: {", ".join(supported_networks[:2])[:36]:<36}║
|
|
|
|
|
║ Extensions: {", ".join(active_extensions)[:36]:<36}║
|
feat: New Python SDK with v2 support (#841)
* chore: renamed python/x402 to python/legacy
* feat: initial Python core SDK
* feat: added core HTTP wrappers
* feat: added core types
* feat: added evm mechanism for exact v1/v2
* feat: added httpx client
* feat: added requests client
* feat: added fastapi middleware
* feat: added flask middleware
* feat: added integration tests
* feat: added Python SDK to e2e tests
* python-v2: fix workflow checks (#863)
* fix format
* fix lint
* fix build
* fix pytest
* Fix syntax errors and missing imports in manual integration example (#902)
- Add missing imports (base64, json, Response, JSONResponse, PaymentPayload, FacilitatorClient)
- Fix function signature syntax error
- Fix dict initialization syntax (colon to equals)
- Add missing return statement after successful payment
- Remove trailing whitespace
* feat: add Python SDK v2 server examples (FastAPI, Flask) (#879)
* feat: add fastapi example project configuration
* feat: add fastapi example with x402 payment middleware
* docs: add comprehensive README for fastapi example
* feat: add flask example project configuration
* feat: add flask example with x402 payment middleware
* docs: add comprehensive README for flask example
* feat: add test clients for fastapi and flask examples
* docs: add test client usage to READMEs
* chore - remove python-version and test-client.py
* docs - remove test client guide from README.md
* chore - remove license and classifier
* feat - add description and mime type
* feat - update .env-local
* feat: add Python v2 client examples (httpx & requests) (#857)
* feat: add Python v2 client examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
- examples/python/clients/example.py - Simple example showing both httpx and requests
- Uses EthAccountSigner with register_exact_evm_client helper
- Minimal configuration required
* refactor: split Python client examples into separate directories
Address PR review feedback:
- Separate httpx/ and requests/ directories
- Add pyproject.toml for uv sync support
- Add environment variable validation
- Add payment response header extraction
- Use context manager for requests session cleanup
Tested on Base Sepolia testnet:
- httpx: tx 0xce49f668aba01c4b0b1af2eb0d9a381f4c62295506a91f20af479e0e834ef350
- requests: tx 0xb83b6feefa5a744b7779ba5b37df33f9c7241f58119454d66102acd97a92f024
* Update examples/python/clients/httpx/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
* Update examples/python/clients/requests/main.py
Co-authored-by: phdargen <pdargent@icloud.com>
---------
Co-authored-by: phdargen <pdargent@icloud.com>
* feat(python): Make v2 framework and client dependencies optional extras (#901)
* python-v2: add svm mechanism (#904)
* add svm mechanism
* add unit tests
* add integration tests
* update server examples
* add e2e tests
* update clients
* make EVM/SVM dependencies optional extras
* fix legacy uv paths
* V2 python bazaar (#929)
* add bazaar extension
* strip query params for discovery
* fix 862
* updated e2e tests
* Add advanced Python client examples (#947)
* Add advanced Python client examples
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.
* Remove tests and examples beyond TypeScript SDK parity
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
* Address PR feedback: add SVM support and use optional deps
- 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
* Python-v2: facilitator example + e2e (#950)
* add facilitator example
* improve svm signer
* add description to DiscoveredResource
* add facilitator e2e
* add paywall builder (#953)
* Add Python custom client example (#954)
Demonstrates manual x402 v2 payment flow without convenience wrappers.
Shows the 6-step process:
1. Initialize client and register payment schemes
2. Make initial request, receive 402
3. Decode PAYMENT-REQUIRED header
4. Create signed payment payload
5. Retry with PAYMENT-SIGNATURE header
6. Handle success/failure response
Useful for integrating with non-standard HTTP libraries or implementing
custom retry logic.
* feat(v2-sdk): support async dynamic hooks for price and metadata (#900)
* feat(http): implement dual class design for sync/async support
- Add x402HTTPResourceServer (async) for FastAPI/Starlette
- Add x402HTTPResourceServerSync (sync) for Flask/Django
- Share common logic via _x402HTTPServerBase
- Remove dynamic description/resource (keep only dynamic price/payTo)
- Update Flask middleware to use sync server directly
- Integrate with enrich_extensions for bazaar support
- Add per-route hook_timeout_seconds configuration
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* fix(types): add missing Awaitable import and fix test import sorting
- Add Awaitable import to types.py for DynamicPayTo/DynamicPrice type hints
- Fix import block sorting in test_async_hooks.py and test_core.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
* refactor: simplify API by removing unused types and making timeout opt-in
Address reviewer feedback from @phdargen:
- Remove SyncDynamicPayTo/SyncDynamicPrice (unused types that would require SyncPaymentOption to be useful)
- Change hook_timeout_seconds default to None (opt-in, matching TS/Go parity)
- Export x402HTTPResourceServerSync from http/__init__.py
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
* python-v2: make create_facilitator_config work with both V1 and V2 (#968)
* make create_facilitator_config work with both V1 and V2
* fix lint
* feat: add Python SDK v2 server advanced example (FastAPI) (#952)
* feat - add pyproject.toml, uv.lock for advanced example
* feat - add bazaar extension
* feat - add hooks
* feat - add x402 extensions to pyproject.toml
* feat - add dynamic pricing feature
* feat - add dynamic pay-to and custom money parser features
* docs - add README for advanced server example
* style - apply lint
* chore - add comments for bazaar extension code
* fix: python v2 x402HTTPAdapter retry logic (#978)
* fix: fix consecutive payment bug in x402HTTPAdapter
- Replace instance-level _is_retry flag with per-request header
to prevent state leakage between consecutive 402 requests.
* tests - add test case for consecutive payment with x402HTTPAdapter
* style - apply lint
* Python-v2: async lifecycle hooks (#975)
* add dual sync/async classes for server/client/facilitator
* update integration/e2e tests
* add unit tests for core, http and implement wrapHttpxWithPaymentFromConfig/wrapRequestsWithPayment
* refactor core server/client/facilitator
* refactor x402_http_server/facilitator_client/x402_http_client
* update exmamples
* add utility function that takes care of html-safe escaping
* add sdk readmes
* add retry unit tests to httpx client
* Update Python SDK Docs for v2 (#976)
* Porting docs and updating for v2 changes (#813)
* porting docs and updating for v2 changes
* update quickstart for sellers
* update quickstarts, miniapps, and mcp docs
* update doc links
* update FAQs
* fix getting started docs
* update core concepts
* remove miniapps guide
* update extensions
* fix bazaar code examples
* fix broken links and yaml file
* remove unnecessary protocol layer doc
* add migration guide
* update documentation for python v2 sdk
* testing pr preview
* docs(python): remove unused os imports and align with TS tab patterns
- Remove unused 'import os' from FastAPI and Flask examples in quickstart-for-sellers
- Update bazaar-discovery-layer Python example to use hardcoded '0xYourPrivateKey' to match TypeScript tab consistency
* update migration doc
* cleanup comments
* cleanup readme
* fix: Python facilitator e2e code
* fix: lint
---------
Co-authored-by: phdargen <p.dargent@cern.ch>
Co-authored-by: somesome <57699892+sohw400@users.noreply.github.com>
Co-authored-by: Logan Kang <kcs93023@gmail.com>
Co-authored-by: 1bcMax <viewitter@gmail.com>
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: Vladimir Sotnikov <bubert9@gmail.com>
Co-authored-by: Shuhei <shuhei0866@gmail.com>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Faris Habib <faris.habib@coinbase.com>
2026-01-21 16:32:07 -08:00
|
|
|
║ ║
|
|
|
|
|
║ Endpoints: ║
|
|
|
|
|
║ • POST /verify (verify payment) ║
|
|
|
|
|
║ • POST /settle (settle payment) ║
|
|
|
|
|
║ • GET /supported (get supported kinds) ║
|
|
|
|
|
║ • GET /discovery/resources (list discovered) ║
|
|
|
|
|
║ • GET /health (health check) ║
|
|
|
|
|
║ • POST /close (shutdown server) ║
|
|
|
|
|
╚════════════════════════════════════════════════════════╝
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
# Log that facilitator is ready (needed for e2e test discovery)
|
|
|
|
|
print("Facilitator listening")
|
|
|
|
|
|
|
|
|
|
uvicorn.run(app, host="0.0.0.0", port=PORT, log_level="warning")
|