Skip to main content

402fly-client

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

Overview

The 402fly-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 402fly-client

Usage

Fly402AutoClient - Automatic Payment Handling

The Fly402AutoClient automatically handles payment requirements transparently:

from 402fly_client import Fly402AutoClient
from solders.keypair import Keypair
import asyncio

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

# Create client with automatic payment handling
client = Fly402AutoClient(
    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())

Fly402Client - Explicit Payment Control

For more control over the payment process:

from 402fly_client import Fly402Client
from solders.keypair import Keypair
import asyncio

keypair = Keypair()
client = Fly402Client(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 Fly402AutoClient:

  • 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 402fly_client import Fly402AutoClient
from solders.keypair import Keypair

# Minimal changes to add payment support
keypair = Keypair()
client = Fly402AutoClient(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 fly402client 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 fly402client 0.1.1
File Size Uploaded
fly402client-0.1.1.tar.gz 7.6 kB Details

Built distribution (wheel)

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

Total release size: 16.9 kB

Release files / fly402client-0.1.1.tar.gz

Download URL fly402client-0.1.1.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
0bb8e09be7a9b3124d0e552622bfddcd511700813dc0b1370c46f71a94e42b41
BLAKE2b-256 checksum
How to use checksums
5aec9d461af47b07ab3a984290ae03720334bdef450c2cbacd0a5d144a798e40
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

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

Download URL fly402client-0.1.1-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a72405f85e655cf8a7fad80d2b939192441c4a91dd39de8d988d5cf947d13636
BLAKE2b-256 checksum
How to use checksums
76bdd93c7929448926e9d61399c46c1b0e170a356249786fcbc13d24e454d6f4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.1 This release

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