Skip to main content

Python SDK for HTTPayer

Project description

HTTPayer – Python SDK

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

It integrates with the HTTPayer Router to automatically fulfill off-chain payments (e.g. USDC / stablecoins via EIP-3009), enabling pay-per-use APIs and metered endpoints.


Features

  • Auto-handles 402 payment flows through the hosted HTTPayer router
  • Supports response_mode="text" (default) or "json" for structured proxy results
  • Built-in polling for asynchronous payment completions (202 + webhook)
  • Optional dry-run simulation (simulate=True or simulate_invoice)
  • API key + router configuration via environment or arguments
  • Compatible with Base Sepolia, Avalanche Fuji, and other EVM testnets
  • Flask X402Gate decorator for securing your own 402-protected endpoints

Installation

pip install httpayer

or with demo dependencies (Flask, CCIP examples):

pip install httpayer[demo]

Environment Setup

Copy .env.sample.env and configure your API key and network:

HTTPAYER_API_KEY=your-api-key

For testing the X402Gate decorator or local demos:

NETWORK=base
FACILITATOR_URL=https://x402.org
RPC_GATEWAY=https://your-gateway.example
PAY_TO_ADDRESS=0xYourReceivingAddress

Usage

1. Programmatic Client

from httpayer import HTTPayerClient

client = HTTPayerClient(response_mode="json")

# auto-handles 402 Payment Required
resp = client.request("GET", "https://x402.org/protected")

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

Manual Payment or Simulation

# simulate required payment (dry-run)
sim = client.simulate_invoice("GET", "https://x402.org/protected")

# pay actual invoice via router
paid = client.pay_invoice("GET", "https://x402.org/protected")

If the router returns 202 Accepted, the client will automatically poll the provided webhook_url until completion.


2. Flask Authorization – X402Gate

Protect your own Web2 endpoints with tokenized payment headers:

from httpayer.gate import X402Gate
from flask import Flask, jsonify, make_response

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

app = Flask(__name__)

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

Each route can define its own X402Gate instance with unique token or chain parameters.


Examples

File Description
tests/test1.py Programmatic 402 requests using HTTPayerClient
tests/test2.py Flask Weather API protected with X402Gate
tests/test3.py Explicit simulate_invoice / pay_invoice usage
tests/test4.py Raw JSON return (response_mode='json')

Run with:

python tests/test1.py

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
├── gate.py          # X402Gate – Flask decorator & helpers
tests/
├── test1.py         # Programmatic example
├── test2.py         # Flask demo server
├── test3.py         # Explicit pay/simulate example
├── test4.py         # Raw JSON example
.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.6.tar.gz (797.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.2.6-py3-none-any.whl (810.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: httpayer-0.2.6.tar.gz
  • Upload date:
  • Size: 797.7 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.6.tar.gz
Algorithm Hash digest
SHA256 064cd27f533bdb7e54ff8bc4763c2505a5558fc2a4121eec50862f1041ae489d
MD5 a88203dcc623d8b6e198f0d2849ba2d8
BLAKE2b-256 b4df574e5ac1e2d24d77fd17b0e630bc0d64383adb6c9a163443577463a9dcf4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: httpayer-0.2.6-py3-none-any.whl
  • Upload date:
  • Size: 810.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.6-py3-none-any.whl
Algorithm Hash digest
SHA256 263f32ec770fd16277443e4dad01e7f441dd16c3da454330914b5a0a9f06554b
MD5 00a0e667460986c9ba23bf563f215517
BLAKE2b-256 d85aa970a8fb303d4119e48d9981441e81a77f56721c283c9509c441057ad110

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