Skip to main content

Python SDK for HttPayer

Project description

HTTPayer – Python SDK

HTTPayer is a Python SDK and decorator toolkit for accessing 402 Payment Required resources using the x402 protocol. It integrates with the HTTPayer router to enable seamless off-chain stablecoin payments using EIP-3009 and tokenized authorization headers.

This package provides:

  • HttPayerClient: a programmatic client for automatically paying 402 responses using a hosted HTTPayer server
  • X402Gate: a decorator for protecting Web2 API endpoints using 402-compliant authorization and on-chain token metadata
  • Environment-variable support for network/facilitator configuration

Features

  • Unified HTTPayer router integration
  • Automatic retry on 402 with X-PAYMENT headers
  • Framework-agnostic endpoint protection with X402Gate
  • EVM token metadata verification (name/version via web3)
  • Compatible with Base Sepolia, Avalanche Fuji, and other testnets

Installation

Install from source or using pip:

pip install .

Install with demo dependencies (for Flask/CCIP demos):

pip install .[demo]

Environment Setup

Create a .env file or set environment variables directly:

NETWORK_TYPE=testnet
NETWORK=base
FACILITATOR_URL=https://x402.org
HTTPAYER_API_KEY=your-api-key
RPC_GATEWAY=https://your-gateway.example
PAY_TO_ADDRESS=0xYourReceivingAddress

Usage

HttPayerClient

A client for paying 402-gated endpoints using a hosted HTTPayer router.

from httpayer import HttPayerClient

client = HttPayerClient()

response = client.request("GET", "http://provider.akash-palmito.org:30862/base-weather")

print(response.status_code)      # 200
print(response.headers)          # Includes X-PAYMENT-RESPONSE
print(response.json())           # Actual resource data

You can also manually call pay_invoice(...) if you already received a 402 response.


X402Gate Decorator

A gate/decorator for protecting Web2 API routes using x402 payment authorization headers.

from httpayer import X402Gate
from web3 import Web3

gate = X402Gate(
    pay_to="0xYourReceivingAddress",
    network="base-sepolia",
    asset_address="0xTokenAddress",
    max_amount=1000,  # atomic units (e.g. 0.001 USDC = 1000)
    asset_name="USD Coin",
    asset_version="2",
    facilitator_url="https://x402.org"
)

@gate.gate
def protected_resource(request_data):
    return {
        "status": 200,
        "headers": {},
        "body": {"message": "Access granted."}
    }

# Call using:
response = protected_resource({
    "headers": {"X-Payment": "<base64 header>"},
    "url": "http://your-api.com/protected"
})

Examples

test1.py – Programmatic Client Example

Runs multiple GET requests to x402-protected endpoints and prints response metadata.

python tests/test1.py

test2.py – Flask Weather Server Demo

Starts a local API server (/weather) that requires a valid X-PAYMENT header:

python tests/test2.py

Send payment using HTTPayer:

http POST http://localhost:31157/httpayer \
  api_url=http://localhost:50358/weather \
  method=GET \
  x-api-key:your-api-key

Development

Build & Publish

uv venv
uv sync
python -m build
twine upload dist/*

Optional Dev Tools

Install development extras:

pip install .[dev]

Project Structure

httpayer/                 # Main package
├── __init__.py
├── client.py            # HttPayerClient class
├── gate.py              # X402Gate and helpers
tests/
├── test1.py             # Client-based demo
├── test2.py             # Flask server demo
.env.sample              # Environment config template
pyproject.toml           # Build config (PEP 621)
README.md

License

MIT License. See LICENSE for full details.


Author

Created by Brandyn Hamilton

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.1.0.tar.gz (8.7 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.1.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: httpayer-0.1.0.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for httpayer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 372ab594b632d5ea868c09a6c6b46ae9250ba0b8ae0e471dec028ad165bbe06d
MD5 6a165cfdf0589f0317f4a995f1e1d20b
BLAKE2b-256 b16c878b01cab9178287d6667a65ccb18c7a6aa0efa17d8d12aab6c3b8d8b339

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpayer-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for httpayer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9b6d0ecaaee5c32901079b9b513ee9a919a17f1f62c578845e9f4e1bfd6705e
MD5 890dd7e96c7770f358c32c5089f46c99
BLAKE2b-256 0955e1d4827a0186d7b601526abe99eb5682ba98a04d4781132a7bab9c77d624

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