Skip to content

Conversation

@1bcMax
Copy link
Contributor

@1bcMax 1bcMax commented Dec 24, 2025

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

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:

  • examples/python/clients/example.py - Single file demonstrating both httpx (async) and requests (sync) patterns
  • examples/python/clients/README.md - Setup and usage instructions
  • examples/python/clients/.env-example - Environment configuration template
  • examples/python/README.md - Overview of Python examples directory

Key features:

  • Minimal boilerplate (~50 lines total)
  • Shows complete setup: Account → Signer → Client → Register → Request
  • Uses correct v2 SDK APIs (EthAccountSigner, register_exact_evm_client)
  • Both async (httpx) and sync (requests) patterns in one example
  • Automatic 402 payment handling - users just call .get(url)

Context:
This completes the TODO item from #841:

Add Python client examples (httpx & requests)

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:

  1. Syntax & Structure:
    python -m py_compile examples/python/clients/example.py
    # ✅ No syntax errors
    
  2. Import Verification:
    - ✅ All imports exist in SDK (x402Client, x402HttpxClient, x402_requests, etc.)
    - ✅ Correct API signatures match SDK implementation
    - ✅ Uses EthAccountSigner (not deprecated PrivateKeySigner)
  3. Code Pattern Validation:
    - ✅ 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)
  4. Payment Flow Logic:
    - ✅ 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
  5. Documentation:
    - ✅ 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

  • I have formatted and linted my code
  • All new and existing tests pass (syntax validation completed)
  • My commits are signed (required for merge) -- you may need to rebase if you initially pushed unsigned commits

@vercel
Copy link

vercel bot commented Dec 24, 2025

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

A member of the Team first needs to authorize it.

@phdargen phdargen self-assigned this Dec 24, 2025
Copy link
Contributor

@tenequm tenequm left a 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 x402 package 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.

@phdargen
Copy link
Contributor

Hi @1bcMax, thanks for your contribution!

We require commit signing, please rebase with signed commits
See here for instructions: https://docs.github.com/en/authentication/managing-commit-signature-verification/about-commit-signature-verification

@phdargen
Copy link
Contributor

httpx & requests examples should not be mixed in one file, please make separate directories for each client httpx/ and requests/ and create one example for each

@phdargen
Copy link
Contributor

Please add pyproject.toml files such that users can install dependencies with uv sync and update setup instructions in README accordingly. You can closely follow the legacy examples here, see https://github.com/coinbase/x402/blob/main/examples/python/legacy/clients/httpx/pyproject.toml and https://github.com/coinbase/x402/blob/main/examples/python/legacy/clients/httpx/README.md

@phdargen
Copy link
Contributor

Please add validation for required environment variables

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}")
Copy link
Contributor

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

@phdargen
Copy link
Contributor

Please manually test your example scripts with a local server and attach the console log to your PR description

@1bcMax
Copy link
Contributor Author

1bcMax commented Dec 27, 2025

I will do the seperate PR, and do the local test. Thank you @phdargen

@phdargen
Copy link
Contributor

I will do the seperate PR, and do the local test. Thank you @phdargen

Please don't open a separate PR @1bcMax, just address the comments above and make a new commit

@1bcMax 1bcMax force-pushed the feat/python-v2-sdk branch from 2faff61 to af983f6 Compare December 27, 2025 22:58
@1bcMax
Copy link
Contributor Author

1bcMax commented Dec 27, 2025

@phdargen
Copy link
Contributor

Hi @1bcMax, thanks for the update! Could you please rebase against coinbase:feat/python-v2-sdk? This should fix the failing workflow checks

@phdargen
Copy link
Contributor

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
@1bcMax 1bcMax force-pushed the feat/python-v2-sdk branch from af983f6 to 8973862 Compare December 30, 2025 14:14
@1bcMax
Copy link
Contributor Author

1bcMax commented Dec 30, 2025

@phdargen Hey, thank you for your feedback.
The PR should now pass the workflow checks. The commits are:

  • 8973862 - refactor: split Python client examples into separate directories
  • cff7b62 - feat: add Python v2 client examples (httpx & requests)

1bcMax and others added 2 commits December 30, 2025 13:35
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
Copy link
Contributor

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

Suggested change
PRIVATE_KEY=your_evm_private_key_here
EVM_PRIVATE_KEY=your_evm_private_key_here

Comment on lines +83 to +86
print(f"\nPayment settled successfully!")
print(f" Transaction: {settle_response.transaction}")
print(f" Network: {settle_response.network}")
print(f" Payer: {settle_response.payer}")
Copy link
Contributor

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

Suggested change
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)}")

@phdargen
Copy link
Contributor

phdargen commented Jan 5, 2026

OK let's get this in @1bcMax, thanks again for your contribution! I will follow-up with adding svm support in #904

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants