Skip to main content

Python SDK for HTTPayer

Project description

HTTPayer – Python SDK

Documentation

HTTPayer is a lightweight Python SDK for accessing APIs protected by 402 Payment Required responses using the x402 protocol.

The SDK supports two access patterns for handling 402-protected resources:

Access Patterns

Proxy Mode (/proxy) – Account-Based Abstraction

  • Uses API keys for authentication
  • Abstracts on-chain payments from the client
  • Credits deducted only if HTTPayer is charged by the API
  • Ideal for Web2 and server-side integrations
  • No wallet or private key management required

Relay Mode (/relay) – x402-Native Access

  • Directly implements the x402 Protocol
  • Uses payment headers for authorization
  • No API key required – self-custodial payments
  • Supports cross-chain access (pay on Base, access Solana APIs)
  • Privacy-preserving routing via HTTPayer relay (when privacy_mode=True)
  • Direct x402 payments to API providers (when privacy_mode=False)
  • Automatic refunds if payment is not executed by target API

Features

  • Auto-handles 402 payment flows
  • Dual access patterns: Proxy (API key) or Relay (private key)
  • Cross-chain payments between EVM and Solana
  • Privacy mode for anonymized payments via HTTPayer relay
  • Dry-run simulation (simulate=True)
  • Compatible with Base, Base Sepolia, SKALE Base, SKALE Base Sepolia, Solana, and Solana Devnet
  • Response modes: "text" (unwrapped) or "json" (wrapped)

Installation

pip install httpayer

For complete API reference and guides, visit docs.httpayer.com


Environment Setup

Copy .env.sample.env and configure your API key and/or Private Key:

HTTPAYER_API_KEY=your-api-key
EVM_PRIVATE_KEY=your-private-key
SOLANA_PRIVATE_KEY=your-private-key
SOLANA_KEYPAIR=your-keypair

Quick Start

Proxy Mode (API Key)

from httpayer import HTTPayerClient

# Initialize with API key (from HTTPAYER_API_KEY env var)
client = HTTPayerClient()

# Auto-handles 402 Payment Required - credits deducted on success
response = client.request("GET", "https://api.example.com/protected")

print(response.status_code)  # 200
print(response.json())       # resource data

Relay Mode (Private Key)

import os
from httpayer import HTTPayerClient

# Initialize with EVM private key
client = HTTPayerClient(
    private_key=os.getenv("EVM_PRIVATE_KEY"),
    network="base"  # Payment network
)

# Auto-handles 402 - self-custodial payment from your wallet
response = client.request("GET", "https://api.example.com/protected")

print(response.status_code)  # 200
print(response.json())       # resource data

Cross-Chain Example

# Pay on Solana, access API requiring Base payments
client = HTTPayerClient(
    private_key=os.getenv("SOLANA_PRIVATE_KEY"),
    network="solana-mainnet-beta"
)

# HTTPayer relay handles cross-chain conversion
response = client.request("GET", "https://base-api.example.com/data")

Simulation (Dry-Run)

# Preview payment cost without executing
sim = client.request("GET", "https://api.example.com/protected", simulate=True)
print(sim.json())  # Shows payment requirements and cost

# Then execute actual payment
response = client.request("GET", "https://api.example.com/protected")

Examples

See the examples/ directory for copy-paste ready code:

Proxy Mode

  • examples/proxy/basic_request.py - Simple GET request with auto-payment
  • examples/proxy/simulate_then_pay.py - Preview cost before payment
  • examples/proxy/check_balance.py - Check account balance

Relay Mode

  • examples/relay/evm_payment.py - Self-custodial payment with EVM wallet
  • examples/relay/solana_payment.py - Self-custodial payment with Solana wallet
  • examples/relay/check_limits.py - Check relay usage limits

Run any example:

python examples/proxy/basic_request.py
python examples/relay/evm_payment.py

Note: Local endpoints cannot be paid through the hosted router. For local testing, use the Coinbase x402 SDKs.


Project Layout

httpayer/
├── client.py              # HTTPayerClient – main SDK client
├── x402_solana/           # Solana x402 implementation
examples/
├── proxy/                 # Proxy mode examples (API key)
│   ├── basic_request.py
│   ├── simulate_then_pay.py
│   └── check_balance.py
├── relay/                 # Relay mode examples (private key)
│   ├── evm_payment.py
│   ├── solana_payment.py
│   └── check_limits.py
tests/
├── proxy/                 # Proxy mode tests
├── relay/                 # Relay mode tests
├── unit/                  # Unit tests (no credentials needed)
└── conftest.py            # Shared pytest fixtures
.env.sample                # Environment template

Author

HTTPayer Team


Attribution

This SDK builds upon and includes modified versions of open source software:

HTTPayer provides its own Solana x402 implementation (httpayer/x402_solana/) compatible with the x402 protocol specification.

For detailed attribution information, see THIRD_PARTY_NOTICES.md.


License

This SDK is proprietary and licensed under the HTTPayer SDK License. Cloning, redistribution, or republishing is strictly prohibited. See the LICENSE.md file for details.

Third-party dependencies are licensed under their respective open source licenses (Apache-2.0).

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

httpayer-0.2.9.tar.gz (801.2 kB view details)

Uploaded Source

Built Distribution

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

httpayer-0.2.9-py3-none-any.whl (814.3 kB view details)

Uploaded Python 3

File details

Details for the file httpayer-0.2.9.tar.gz.

File metadata

  • Download URL: httpayer-0.2.9.tar.gz
  • Upload date:
  • Size: 801.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for httpayer-0.2.9.tar.gz
Algorithm Hash digest
SHA256 64966c1978ff1ff5756a7c072691596ff3ff8775e031b9f9c076612ceb7c1f7d
MD5 8588d2f2d1890d0bfdfd52fdcf906b2f
BLAKE2b-256 0f2384ed014697b911f18c5fec429f410c8c8c1f7bae04a240f682f7a6f34127

See more details on using hashes here.

File details

Details for the file httpayer-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: httpayer-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 814.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for httpayer-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 d50a733998cc39bcfec04b406cd9ae5de55793189b09247db3a3b734cf6ecfbe
MD5 6458ee469bd1354f533e2d688ce21f8e
BLAKE2b-256 071d64ae8f559ba8298e3a57be856992ab91c9b74b25af6006d2e8bec2e573a8

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