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.0.tar.gz (6.0 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.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for openlibx402_client-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c6dcaca0d4343a5d1617ea6f944a3837d855719d2cda1b5a7841c64a2f0c37ca
MD5 48b85395b32034893da7a20bb466a92f
BLAKE2b-256 d6d9fd556fdbea1b46de7539b8769a8a716a76a729efa89a59b21795579c5d4c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for openlibx402_client-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8222fb2d7b02a53364465130c162b0bf5f6d550bf9d6ff2b5d300d98a36cabb2
MD5 e11bf17d68fbfa20de8828f9d12f4de2
BLAKE2b-256 38bde10b941f95f707fe6edc887fdc87b7f2080691177bc48416583d80049222

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