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.0.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.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: billmyagent-2.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 f2f5b2798fedd357b2f44960d182c9e329cfcf427287c71c3958d66c18d7ad6d
MD5 f4a600089cb7f889ca3c460821e9f214
BLAKE2b-256 36571298581015a29986397b817483e44678f570031701070f58e9d8d862fc4c

See more details on using hashes here.

Provenance

The following attestation bundles were made for billmyagent-2.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: billmyagent-2.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 04b408808c9f4cb95d852d63138dc9eeba939f26015e492c5c353baff4392a2c
MD5 645799ab74d979358a5db09875cdbfb3
BLAKE2b-256 08d08b357299219006e2c96f83942f55620b88f1d528e5b54a2fcc5a2455bb8d

See more details on using hashes here.

Provenance

The following attestation bundles were made for billmyagent-2.1.0-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