Skip to content

Latest commit

 

History

History
90 lines (66 loc) · 2.62 KB

File metadata and controls

90 lines (66 loc) · 2.62 KB

x402 requests Client Example

This example demonstrates how to use the x402 v2 SDK with requests (sync) to make requests to 402-protected endpoints with support for both EVM (Ethereum) and SVM (Solana) payments.

Setup

  1. Copy .env-local to .env and add your private keys:
cp .env-local .env
  1. Install dependencies:
uv sync

Usage

Run the example:

uv run python main.py

How it Works

The example demonstrates the complete x402 payment flow:

  1. Create x402 client - Set up the payment client
  2. Register payment schemes - Enable EVM and/or SVM payments:
    • register_exact_evm_client for Ethereum-based payments
    • register_exact_svm_client for Solana-based payments
  3. Make request - The x402_requests session automatically handles 402 responses:
    • Intercepts 402 Payment Required responses
    • Creates and signs payment payload
    • Retries request with payment header
    • Returns successful response
  4. Extract payment response - Decode the settlement confirmation from response headers

Code Overview

from x402 import x402ClientSync
from x402.http import x402HTTPClientSync
from x402.http.clients import x402_requests
from x402.mechanisms.evm import EthAccountSigner
from x402.mechanisms.evm.exact.register import register_exact_evm_client
from x402.mechanisms.svm import KeypairSigner
from x402.mechanisms.svm.exact.register import register_exact_svm_client

# Setup
client = x402ClientSync()

# Register EVM (Ethereum) payments
account = Account.from_key(evm_private_key)
register_exact_evm_client(client, EthAccountSigner(account))

# Register SVM (Solana) payments
svm_signer = KeypairSigner.from_base58(svm_private_key)
register_exact_svm_client(client, svm_signer)

# Make request - payment handling is automatic
with x402_requests(client) as session:
    response = session.get(url)

    # Extract payment settlement info
    http_client = x402HTTPClientSync(client)
    settle_response = http_client.get_payment_settle_response(
        lambda name: response.headers.get(name)
    )
    print(f"Transaction: {settle_response.transaction}")

Environment Variables

Variable Description
EVM_PRIVATE_KEY Your EVM private key (with or without 0x prefix)
SVM_PRIVATE_KEY Your Solana private key (base58 encoded)
RESOURCE_SERVER_URL Base URL of the x402-protected server
ENDPOINT_PATH Path to the protected endpoint

Note: At least one of EVM_PRIVATE_KEY or SVM_PRIVATE_KEY must be provided.

Learn More