Skip to main content

x402 payment SDK for billmyagent — auto-pays HTTP 402 challenges with an eth-account signer and gates your own endpoints (FastAPI/Flask)

Project description

billmyagent (Python)

x402 payment SDK for Python. Pay x402-gated APIs as a server/agent and gate your own endpoints with a paywall. The Python counterpart of @billmyagent/payments-core, built on the official x402 library + eth-account.

Unlike the browser-oriented JS SDK, the agent here holds its private key directly — which is why this SDK matters for autonomous Python agents (LangChain, CrewAI, FastAPI services).

Install

pip install billmyagent              # buyer client
pip install 'billmyagent[fastapi]'   # + FastAPI seller middleware
pip install 'billmyagent[flask]'     # + Flask seller middleware

Pay an x402-gated API (buyer / agent)

import os
from billmyagent import PaymentClient, create_signer

# The private key is held in-process and never leaves it.
signer = create_signer("base", os.environ["PRIVATE_KEY"])
client = PaymentClient(signer=signer)

# client.http is a requests.Session; any HTTP 402 it hits is paid and retried.
res = client.http.get("https://api.example.com/paid-resource")
print(res.json(), "paid:", bool(res.headers.get("x-payment-response")))

Manage your merchant account (REST)

client = PaymentClient(api_key=os.environ["BMA_API_KEY"], auth_token=jwt)

client.get_payout_settings()
client.update_payout_settings({"base_address": "0x…"})
client.list_payments()

Charge for your endpoints (seller)

FastAPI:

from fastapi import FastAPI
from billmyagent.middleware.fastapi import add_payment_middleware

app = FastAPI()
add_payment_middleware(
    app,
    pay_to="0xYourPayoutAddress",
    routes={"GET /premium": "$0.01"},
    network="base-sepolia",   # base | base-sepolia | polygon
)

@app.get("/premium")
def premium():
    return {"unlocked": True}

Flask is identical via billmyagent.middleware.flask.add_payment_middleware.

Mainnet (base/polygon) settlement needs a facilitator with CDP credentials — pass facilitator_url=.... Omit it to use the public x402.org testnet facilitator.

Supported networks

EVM only, exact scheme: base, base-sepolia (testing), polygon.

Development

pip install -e '.[dev,fastapi,flask]'
pytest && ruff check . && mypy billmyagent

License

Apache 2.0.

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

billmyagent-2.1.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

billmyagent-2.1.2-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file billmyagent-2.1.2.tar.gz.

File metadata

  • Download URL: billmyagent-2.1.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for billmyagent-2.1.2.tar.gz
Algorithm Hash digest
SHA256 c4db97d3d8f1c7deb23d837914dc2d6c4b54f6f51164928050b077c0971898fd
MD5 54cebb17d9298a5f89c301fd06d59a51
BLAKE2b-256 cbb3948d723fc3ad189f67f800c4b6a40208ef33adbf1f924f6edcfde97b54e3

See more details on using hashes here.

Provenance

The following attestation bundles were made for billmyagent-2.1.2.tar.gz:

Publisher: publish-sdks.yml on RealMaxPower/payment-processing

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file billmyagent-2.1.2-py3-none-any.whl.

File metadata

  • Download URL: billmyagent-2.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for billmyagent-2.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4b267a35f85a2a352c53b44865f79a9d320927a6b38853112c7b3a1248f50ec0
MD5 3dabe38ae9d6a32a546dc930abb4f692
BLAKE2b-256 07f41604e566755c603429511ca7225710b903b86053387441fece8b20ac8173

See more details on using hashes here.

Provenance

The following attestation bundles were made for billmyagent-2.1.2-py3-none-any.whl:

Publisher: publish-sdks.yml on RealMaxPower/payment-processing

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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