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
  • Flask 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 httpayer

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

pip install httpayer[demo]

Environment Setup

For use of the HTTPayerClient class, you copy the .env.sample file into a .env file and fill the HTTPAYER_API_KEY variable with your API key.

HTTPAYER_API_KEY=your-api-key

While the X402Gate class itself doesn't read environment variables, the test script included here does require several variables. To run that script it is reccomended you copy the .env.sample file into a .env file and fill in for the following:

NETWORK=base
FACILITATOR_URL=https://x402.org
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", "https://demo.httpayer.com/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 Flask API routes using x402 payment authorization headers.

from httpayer.gate import X402Gate
from web3 import Web3
from flask import Flask, request, jsonify, make_response

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"
)

def create_app():
    app = Flask(__name__)

    @app.route('/')
    def index():
        return "<h1>Weather Server</h1><p>Welcome to the Weather Server!</p>"

    @app.route("/weather")
    @gate.gate
    def weather():
        response = make_response(jsonify({"weather": "sunny", "temp": 75}))
        return response

    return app

We can dynamically generate the payment requirements in our Flask app and add it to specific endpoints in our app. Each endpoint can have its own specialized payment instructions.

gate_usdc = X402Gate(
    pay_to=...,
    network="base-sepolia",
    asset_address=USDC_ADDRESS,
    max_amount=1000000,
    ...
)

gate_dai = X402Gate(
    pay_to=...,
    network="avalanche-fuji",
    asset_address=DAI_ADDRESS,
    max_amount=2000000,
    ...
)

@app.route("/api/usdc-data")
@gate_usdc.gate
def usdc_endpoint():
    ...

@app.route("/api/dai-data")
@gate_dai.gate
def dai_endpoint():
    ...

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 with /weather and /post-weather endpoints that require a valid X-PAYMENT header:

python tests/test2.py

Note The HTTPayer server cannot pay a locally-hosted endpoint. You will need to use the x402 Javascript or Python SDK to pay and test these endpoints if deployed locally.

Send payment using HTTPayer to the GET method endpoint:

http POST http://app.httpayer.com/pay \
  api_url=http://demo-server:5035/weather \
  method=GET \
  x-api-key:your-api-key

Send payment using HTTPayer to the POST method endpoint

http POST http://app.httpayer.com/pay \
  api_url=http://demo-server:5035/post-weather \
  method=POST \
  payload=YourCity \
  x-api-key:your-api-key

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
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.7.tar.gz (11.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.1.7-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: httpayer-0.1.7.tar.gz
  • Upload date:
  • Size: 11.2 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.7.tar.gz
Algorithm Hash digest
SHA256 1718b4321d2e341573a2df79c1aff21dc869a0eb4de90f29da12a09c34d56c8e
MD5 5ed5ca382793982dc4cead6da2a70510
BLAKE2b-256 430e0c22b48d3c1cc438aacaa1af011227776d8b6d471d9cb4af50037078afcc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpayer-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 8.3 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 3a47d9002f56b83ac24e389e90099a85add2da6ddd84c770cf2b98b127619aae
MD5 0b765e03b6b73a109c627c1b8dd62864
BLAKE2b-256 0b1c8d36c126fbbcb95934e4d2daa5a6cef0b59851e5a67c106ae69ef155ceb1

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