agentpayments-python
Python adapters for the AgentPayments gate. Supports Django, FastAPI/Starlette, and Flask.
Install
pip install agentpayments-python
# or, in this monorepo:
# pip install -e sdk/python
Django
# settings.py
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"agentpayments_python.django_adapter.GateMiddleware",
# ... other middleware
]
# Required settings
CHALLENGE_SECRET = os.environ["CHALLENGE_SECRET"]
HOME_WALLET_ADDRESS = os.environ["HOME_WALLET_ADDRESS"]
# Optional settings
SOLANA_RPC_URL = os.environ.get("SOLANA_RPC_URL")
USDC_MINT = os.environ.get("USDC_MINT")
DEBUG = True # True = devnet, False = mainnet
FastAPI
from fastapi import FastAPI, Request
from agentpayments_python.fastapi_adapter import (
AgentPaymentsASGIMiddleware,
challenge_verify_endpoint,
)
app = FastAPI()
app.add_middleware(
AgentPaymentsASGIMiddleware,
challenge_secret=os.environ["CHALLENGE_SECRET"],
home_wallet_address=os.environ["HOME_WALLET_ADDRESS"],
debug=True,
)
@app.post("/__challenge/verify")
async def verify(request: Request):
return await challenge_verify_endpoint(
request,
challenge_secret=os.environ["CHALLENGE_SECRET"],
)
Flask
from flask import Flask
from agentpayments_python.flask_adapter import register_agentpayments
app = Flask(__name__)
register_agentpayments(
app,
challenge_secret=os.environ["CHALLENGE_SECRET"],
home_wallet_address=os.environ["HOME_WALLET_ADDRESS"],
debug=True,
)
Configuration
| Parameter | Required | Default | Description |
|---|---|---|---|
challenge_secret |
Yes (production) | 'default-secret-change-me' |
HMAC secret for signing cookies, nonces, and agent keys. |
home_wallet_address |
Yes | '' |
Solana wallet address to receive USDC payments. |
solana_rpc_url |
No | Auto (devnet/mainnet) | Custom Solana RPC endpoint. |
usdc_mint |
No | Auto (devnet/mainnet) | Custom USDC mint address. |
debug |
No | True |
True = devnet. False = mainnet + strict mode. |
Django reads these from settings.* (e.g., settings.CHALLENGE_SECRET). FastAPI and Flask accept them as constructor arguments.
Security Features
- Timing-safe HMAC comparison — uses
hmac.compare_digest()for all signature checks - Payment verification cache — 10-minute TTL, 1000-entry max (thread-safe)
- Rate limiting — 20 challenge verifications per minute per IP (thread-safe)
- Input size limits — key (64 chars), nonce (128), return URL (2048), fingerprint (128)
- Wallet address validation — base58 format, 32-44 chars, validated at init
- Default secret detection — warns in debug, raises
RuntimeErrorin production - Secure cookies — Django auto-detects HTTPS via
request.is_secure()
Module Structure
agentpayments_python/
__init__.py
django_adapter.py Django middleware (GateMiddleware)
fastapi_adapter.py FastAPI/Starlette ASGI middleware
flask_adapter.py Flask integration (before_request hook)
challenge.py Shared challenge HTML generation
cookies.py Cookie creation and validation
crypto.py HMAC signing and agent key management
detection.py Browser detection and public path checks
solana.py On-chain payment verification + caching
ratelimit.py Shared IP-based rate limiter
Notes
- Constants loaded from
sdk/constants.jsonviapathlib. - Logging uses Python stdlib
loggingmodule. - All shared modules are framework-agnostic; adapters are thin wiring.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file agentpayments_python-0.1.0.tar.gz.
File metadata
- Download URL: agentpayments_python-0.1.0.tar.gz
- Upload date:
- Size: 29.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7019b2ec45964d1802bfcc1e541fd03a327c87bf535cc1df414f4189a4dab84
|
|
| MD5 |
e9b4fbd629b802f372b1fdab42a96959
|
|
| BLAKE2b-256 |
769c82037e252315637ca0a20941f18ea9c4b0ecbde4cfc9e5008404dc6fef3d
|
File details
Details for the file agentpayments_python-0.1.0-py3-none-any.whl.
File metadata
- Download URL: agentpayments_python-0.1.0-py3-none-any.whl
- Upload date:
- Size: 30.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92b1fb1827287f353ec7f2c287e7d164535700590a9566547a80ec8f81ea1690
|
|
| MD5 |
789500afe9f608bc9d27093646a31b4b
|
|
| BLAKE2b-256 |
7cfe48c426b3d85c9c722b26e70f609c82e37677cddff6b0f1fbf965f987f839
|