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, 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


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.

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.8.tar.gz (800.4 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.8-py3-none-any.whl (812.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: httpayer-0.2.8.tar.gz
  • Upload date:
  • Size: 800.4 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.8.tar.gz
Algorithm Hash digest
SHA256 1a51904c946010097fc9150097cf091add2be39e68f57c4b049e37e79fa8c0d8
MD5 7ad9f923afe3c6908ca7a5bd511208f0
BLAKE2b-256 1855875d42f5cbe16fd00dfe5cf603d89829836db1037e0b7159e3acbc394505

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpayer-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 812.0 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.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6e03bae1884c51d9c1c804187c0d9ec904a696ecb0149924e0cfcf06b75e79db
MD5 df65dfaec5f5c6246d9109324b9829fc
BLAKE2b-256 dd30a10f95f0ae2275f095144afd6e7fe75c79b1b1c5a83e386194e8c8a91f68

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