Skip to main content

Bursar for Python

PyPI PyPI downloads

Bursar logo

The Python SDK for Bursar. It meters usage, prices operations, and manages balances against the shared canonical PostgreSQL schema and the same versioned configuration document as the JavaScript SDK. Python 3.12 and 3.13 are supported.

Installation

pip install bursar[postgres]

Extras: postgres (default recommended), providers (Stripe, Dodo), s3 (optional billing archive), google-adk (model-call admission and settlement plugin), and test (dev/test tooling).

Apply the SQL baseline before starting an application:

export DATABASE_URL=postgresql://...
bursar migrate

bursar migrate applies the ordered SQL files, records checksums, and is safe to re-run. Repeat --post-migrate-sql to run idempotent host-owned SQL in the same transaction.

Usage

from bursar import Bursar, PostgresStore

store = PostgresStore(database_url, tenant_id=tenant_id)
bursar = Bursar.create(credit_store=store)

grant = bursar.credits.add_credits(user_id, 500, entry_type="purchase", idempotency_key="checkout:42")
charge = bursar.credits.deduct_credits(user_id, 20, idempotency_key="job:42")
refund = bursar.credits.refund_credits(charge.entry_id)

page = bursar.credits.list_ledger_entries(user_id, limit=25)
while page.next_cursor is not None:
    page = bursar.credits.list_ledger_entries(user_id, limit=25, cursor=page.next_cursor)

LedgerEntry, LedgerCursor, and LedgerPage are exported from bursar; pagination is cursor-only. PostgresStore is the production, tenant-scoped store; CreditStore is the abstract base for custom implementations.

Publish one versioned configuration document through the facade — billing and auto-recharge read the same active document:

bursar.catalog.publish_and_activate(config)

Optional S3 and ClickHouse storage

PostgreSQL remains authoritative. S3 and ClickHouse are optional delivery targets, managed by create_bursar_runtime from bursar.storage:

from bursar.storage import BursarRuntimeOptions, create_bursar_runtime

runtime = create_bursar_runtime(
    BursarRuntimeOptions(
        postgres=os.environ["DATABASE_URL"],
        tenant_id=os.environ["BURSAR_TENANT_ID"],
    )
)
runtime.start()
bursar = runtime.bursar

With no S3/ClickHouse configuration the runtime creates no background worker and analytics query PostgreSQL directly. See the storage guide for the full S3 and ClickHouse setup.

Development

cd python
uv sync --group dev        # runtime + dev/test deps
uv run pytest              # full suite; integration tests need Postgres
ruff check src/ tests/
pyright src/

Real-Postgres tests resolve DATABASE_URL, else spin up a disposable PostgreSQL 17 + pg_partman 5 + pg_jsonschema 0.3 testcontainer. See CONTRIBUTING.md.

License

AGPL-3.0.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bursar-2.0.0.tar.gz (402.0 kB view details)

Uploaded Source

Built Distribution

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

bursar-2.0.0-py3-none-any.whl (382.4 kB view details)

Uploaded Python 3

File details

Details for the file bursar-2.0.0.tar.gz.

File metadata

  • Download URL: bursar-2.0.0.tar.gz
  • Upload date:
  • Size: 402.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for bursar-2.0.0.tar.gz
Algorithm Hash digest
SHA256 feb1ec5ea9515a35de6e09dc025fc8653c3752323ca42fdca3c8799184579044
MD5 21eab0212c099bfc91b675d3b700852e
BLAKE2b-256 c3b419c00abe4cb14d1b8c2df74d6d44f6706e3e9cba1b9e82b67cff5992db55

See more details on using hashes here.

File details

Details for the file bursar-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: bursar-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 382.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.3 {"installer":{"name":"uv","version":"0.12.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for bursar-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 40346cf11f4b8a3ed3956ffcdac9bdf32307bca17f3e058e6e03d7c25e55103e
MD5 a2645df9ef0bb9ce51884c756641e0c2
BLAKE2b-256 e8681c8139e4404a0a26adad2cef99382c700b5908e64c61c80933c4b321b081

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