-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add Python v2 client examples (httpx & requests) #857
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add Python v2 client examples (httpx & requests) #857
Conversation
|
@1bcMax is attempting to deploy a commit to the Coinbase Team on Vercel. A member of the Team first needs to authorize it. |
tenequm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good - clean examples that get straight to the point 👏
Few small things:
- README install is missing the
x402package itself requests_example()should probably use a context manager for cleanup- A quick env var check would save people from cryptic errors
Nothing major, just polish. Nice work getting this together.
|
Hi @1bcMax, thanks for your contribution! We require commit signing, please rebase with signed commits |
|
httpx & requests examples should not be mixed in one file, please make separate directories for each client |
|
Please add pyproject.toml files such that users can install dependencies with |
|
Please add validation for required environment variables |
examples/python/clients/example.py
Outdated
| response = await http.get(os.getenv("SERVER_URL", "http://localhost:4021/weather")) | ||
| await response.aread() | ||
| print(f"Status: {response.status_code}") | ||
| print(f"Body: {response.text}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please extract payment response header, decode and print it as done in the typescript examples, see eg https://github.com/coinbase/x402/blob/main/examples/typescript/clients/fetch/index.ts
|
Please manually test your example scripts with a local server and attach the console log to your PR description |
|
I will do the seperate PR, and do the local test. Thank you @phdargen |
2faff61 to
af983f6
Compare
|
Hi @1bcMax, thanks for the update! Could you please rebase against coinbase:feat/python-v2-sdk? This should fix the failing workflow checks |
|
Also your first commit is still unverified, when you rebase, please sign all commits |
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
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
af983f6 to
8973862
Compare
Co-authored-by: phdargen <pdargent@icloud.com>
Co-authored-by: phdargen <pdargent@icloud.com>
| @@ -0,0 +1,3 @@ | |||
| PRIVATE_KEY=your_evm_private_key_here | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest renaming to EVM_PRIVATE_KEY in anticipation of svm to be added soon
| PRIVATE_KEY=your_evm_private_key_here | |
| EVM_PRIVATE_KEY=your_evm_private_key_here |
| print(f"\nPayment settled successfully!") | ||
| print(f" Transaction: {settle_response.transaction}") | ||
| print(f" Network: {settle_response.network}") | ||
| print(f" Payer: {settle_response.payer}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest to print the full payment response directly, same for requests example
| print(f"\nPayment settled successfully!") | |
| print(f" Transaction: {settle_response.transaction}") | |
| print(f" Network: {settle_response.network}") | |
| print(f" Payer: {settle_response.payer}") | |
| print(f"\nPayment response: {settle_response.model_dump_json(indent=2)}") |
Python V2 Client Examples (httpx & requests)
Add simple client examples demonstrating httpx (async) and requests (sync) usage with the x402 Python v2 SDK.
Description
This PR adds Python client examples for the v2 SDK as part of the Python V2 migration effort (related to #841).
What's included:
Key features:
EthAccountSigner,register_exact_evm_client).get(url)Context:
This completes the TODO item from #841:
The examples follow the same simple pattern as the Go HTTP client examples, making it easy for developers to get started with x402 payments.
Tests
Validation performed:
python -m py_compile examples/python/clients/example.py # ✅ No syntax errors- ✅ All imports exist in SDK (x402Client, x402HttpxClient, x402_requests, etc.)
- ✅ Correct API signatures match SDK implementation
- ✅ Uses EthAccountSigner (not deprecated PrivateKeySigner)
- ✅ Follows x402 v2 SDK patterns from python/x402/tests/integrations/test_core.py
- ✅ Correct setup order: account → signer → client → register → request
- ✅ Proper async/await usage for httpx
- ✅ Proper session cleanup for requests
- ✅ Uses register_exact_evm_client() helper (registers both V2 and V1)
- ✅ Transport/Adapter intercepts 402 responses automatically
- ✅ Extracts PaymentRequired from headers
- ✅ Creates payment payload via registered EVM scheme
- ✅ Retries with PAYMENT-SIGNATURE header
- ✅ Returns successful response to user
- ✅ Clear README with installation and usage instructions
- ✅ Environment variable template provided
- ✅ Example works with default local server setup
Testing code samples:
All imports resolve correctly
from x402 import x402Client
from x402.http.clients.httpx import x402HttpxClient
from x402.http.clients.requests import x402_requests
from x402.mechanisms.evm.exact.register import register_exact_evm_client
from x402.mechanisms.evm.signers import EthAccountSigner
Example can be run with:
cd examples/python/clients
python example.py
Checklist