Skip to main content

HTTP client library for X402 payment protocol with automatic payment handling

Project description

openlibx402-client

HTTP client library for the X402 payment protocol with automatic payment handling.

Overview

The openlibx402-client package provides HTTP client libraries that automatically handle X402 payment requirements. When accessing payment-protected APIs, the client can automatically detect payment requirements, process payments, and retry requests with payment authorization.

Features

  • Automatic detection of payment-required responses (402 status)
  • Seamless payment processing and request retry
  • Support for both automatic and explicit payment flows
  • Wallet management and transaction handling
  • Built on top of httpx for modern async/sync HTTP support

Installation

pip install openlibx402-client

Usage

X402AutoClient - Automatic Payment Handling

The X402AutoClient automatically handles payment requirements transparently:

from openlibx402_client import X402AutoClient
from solders.keypair import Keypair
import asyncio

# Load your wallet keypair
keypair = Keypair()  # Or load from file

# Create client with automatic payment handling
client = X402AutoClient(
    wallet_keypair=keypair,
    max_payment="5.0",  # Maximum payment amount (safety limit)
    auto_retry=True,    # Automatically retry after payment
)

async def fetch_data():
    # Client automatically handles payment if required
    response = await client.fetch("https://api.example.com/premium-data")
    print(response.text)

    await client.close()

asyncio.run(fetch_data())

X402Client - Explicit Payment Control

For more control over the payment process:

from openlibx402_client import X402Client
from solders.keypair import Keypair
import asyncio

keypair = Keypair()
client = X402Client(wallet_keypair=keypair)

async def fetch_with_explicit_payment():
    try:
        # First request (may return 402 Payment Required)
        response = await client.fetch("https://api.example.com/premium-data")
        print(response.text)
    except Exception as e:
        if "402" in str(e):
            # Handle payment explicitly
            print("Payment required - processing...")
            # The client provides methods for explicit payment handling

    await client.close()

asyncio.run(fetch_with_explicit_payment())

Loading Wallet from File

import json
from solders.keypair import Keypair

# Load wallet from Solana CLI format
with open("wallet.json") as f:
    wallet_data = json.load(f)
    keypair = Keypair.from_bytes(bytes(wallet_data))

Configuration

Key parameters for X402AutoClient:

  • wallet_keypair: Your Solana wallet keypair for payments
  • max_payment: Maximum payment amount (in tokens) - safety limit
  • auto_retry: Whether to automatically retry after payment (default: True)
  • rpc_url: Solana RPC endpoint (default: devnet)
  • timeout: HTTP request timeout

Example: Integration with Existing Code

from openlibx402_client import X402AutoClient
from solders.keypair import Keypair

# Minimal changes to add payment support
keypair = Keypair()
client = X402AutoClient(wallet_keypair=keypair, max_payment="10.0")

# Use like a normal HTTP client
response = await client.fetch("https://api.example.com/data")
data = response.json()

Documentation

For complete API reference and guides, see:

Testing

pytest tests/

License

MIT License - See LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openlibx402_client-0.1.1.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

openlibx402_client-0.1.1-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file openlibx402_client-0.1.1.tar.gz.

File metadata

  • Download URL: openlibx402_client-0.1.1.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for openlibx402_client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5c249e20cba862d64a278203c5b97a7be9aec183dfbd0538c77486f1c8c88070
MD5 ffcbcb90c2b2ef6d4afedf42c2d1ed2d
BLAKE2b-256 3cdc77f05a40c117190d5c2c9f0f497b0304ac667c73212bf99f5ad3a4806c9e

See more details on using hashes here.

File details

Details for the file openlibx402_client-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for openlibx402_client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 abffd8bde74d2144843d4535f219ff0ab9044f9e60c378fb110b6851e36781e3
MD5 57df3820a6d0d05023a744cb6e0e385a
BLAKE2b-256 86e35b0371a53ade2ee2d6ee92e350a04a3d4549ac4f14c345f4b8dc9788f414

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page