Skip to main content

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.

Metadata

Release files for openlibx402-client 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openlibx402-client 0.1.1
File Size Uploaded
openlibx402_client-0.1.1.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for openlibx402-client 0.1.1
File Interpreter ABI Platform
openlibx402_client-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / openlibx402_client-0.1.1.tar.gz

Download URL openlibx402_client-0.1.1.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5c249e20cba862d64a278203c5b97a7be9aec183dfbd0538c77486f1c8c88070
BLAKE2b-256 checksum
How to use checksums
3cdc77f05a40c117190d5c2c9f0f497b0304ac667c73212bf99f5ad3a4806c9e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release files / openlibx402_client-0.1.1-py3-none-any.whl

Download URL openlibx402_client-0.1.1-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abffd8bde74d2144843d4535f219ff0ab9044f9e60c378fb110b6851e36781e3
BLAKE2b-256 checksum
How to use checksums
86e35b0371a53ade2ee2d6ee92e350a04a3d4549ac4f14c345f4b8dc9788f414
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.23

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page