Skip to main content

HTTP client library for the 402fly payment protocol with automatic payment handling

Project description

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.

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

fly402client-0.1.1.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

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

fly402client-0.1.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for fly402client-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0bb8e09be7a9b3124d0e552622bfddcd511700813dc0b1370c46f71a94e42b41
MD5 f415500e11fd3fae65928101572bdfe8
BLAKE2b-256 5aec9d461af47b07ab3a984290ae03720334bdef450c2cbacd0a5d144a798e40

See more details on using hashes here.

File details

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

File metadata

  • Download URL: fly402client-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for fly402client-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a72405f85e655cf8a7fad80d2b939192441c4a91dd39de8d988d5cf947d13636
MD5 524d5eb96ca7cae4db361c6c50471f2b
BLAKE2b-256 76bdd93c7929448926e9d61399c46c1b0e170a356249786fcbc13d24e454d6f4

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