Skip to main content

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.
api_key No None AgentPayments hosted-platform API key (ap_live_...). When set, agent keys are issued and metered via the platform instead of self-signed locally. See Hosted Platform Mode below.
platform_url No AgentPayments-hosted URL Override for a self-hosted platform API.

Django reads these from settings.* (e.g., settings.CHALLENGE_SECRET, settings.AGENTPAYMENTS_API_KEY). FastAPI and Flask accept them as constructor arguments.

Hosted Platform Mode

Setting api_key switches agent-key issuance from local (ag_...) to platform-issued (agp_...), and — when the platform account has an on-chain fee configured — every 402 response's payment dict gains a platform_fee field describing a second required USDC transfer, to be sent in the same Solana transaction as the vendor payment. Missing that second transfer is treated as an unpaid request, same as any other invalid payment. This is opt-in per vendor account and has no effect on self-hosted deployments (no api_key). The standards-compliant accepts[]/X-PAYMENT-REQUIRED x402 fields are untouched — they still describe only the vendor leg.

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 RuntimeError in 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.json via pathlib.
  • Logging uses Python stdlib logging module.
  • All shared modules are framework-agnostic; adapters are thin wiring.

Metadata

Release files for agentpayments-python 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for agentpayments-python 0.1.2
File Size Uploaded Provenance Actions
agentpayments_python-0.1.2.tar.gz 33.1 kB Without provenance Details for agentpayments_python-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for agentpayments-python 0.1.2
File Interpreter ABI Platform Actions
agentpayments_python-0.1.2-py3-none-any.whl Python 3 none any Details for agentpayments_python-0.1.2-py3-none-any.whl

Total release size: 66.5 kB

Release files / agentpayments_python-0.1.2.tar.gz

Download URL agentpayments_python-0.1.2.tar.gz
Size 33.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3fe70ac2da708bf2f306673c5b87c7865b93a79c3f0b74b48bea534cb495286e
BLAKE2b-256 checksum
How to use checksums
e4f34b40f5fd840ecf9bb28a2f116d16dc257e8348f7987d3307ad15dc603e69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Release files / agentpayments_python-0.1.2-py3-none-any.whl

Download URL agentpayments_python-0.1.2-py3-none-any.whl
Size 33.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
08dd8b1b3a544deaad1b39c7570b0e306cf13903655c72555e1986b7548f1c85
BLAKE2b-256 checksum
How to use checksums
6f208511de92fdb6930085ab0b4f4f38fe01d6848cd93bc10797f046332ae6b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.6

Security for agentpayments-python 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page