Billing Engine

Last Commit Repo Size License

Python FastAPI Pydantic Uvicorn SQLite SQLAlchemy Redis RQ Swagger

A lightweight, modular, backend‑only billing engine built with FastAPI, SQLAlchemy, and a clean service‑layer architecture. It ingests usage events, aggregates them, prices them, and generates invoices β€” all without any frontend or dashboard.

This project is ideal for demonstrating backend engineering skills such as:


✨ Features

Record usage events (e.g., API calls, storage, workflows)

Daily usage aggregation (group + sum raw events)

Pricing engine (simple per‑unit pricing, easily extendable)

Invoice preview (estimate charges before billing period ends)

Invoice generation (creates real invoice records)

Clean service layer (routers β†’ services β†’ models)

Background‑safe aggregation (manual or scheduled)

SQLite for simplicity (swap for Postgres easily)


πŸ“ Project Structure

billing_engine/
β”‚
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ routers/
β”‚   β”‚   β”œβ”€β”€ usage.py
β”‚   β”‚   β”œβ”€β”€ invoice.py
β”‚   β”‚   └── aggregator.py
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ usage_service.py
β”‚   β”‚   β”œβ”€β”€ invoice_service.py
β”‚   β”‚   └── aggregator_service.py
β”‚   β”œβ”€β”€ models.py
|   β”œβ”€β”€ aggregator.py
|   β”œβ”€β”€ pricing.py
β”‚   β”œβ”€β”€ schemas.py
β”‚   β”œβ”€β”€ deps.py
β”‚   └── database.py
β”‚
β”œβ”€β”€ tests.py/
|   β”œβ”€β”€ conftest.py
|   β”œβ”€β”€ test_aggregator.py
|   β”œβ”€β”€ test_api.py
|   β”œβ”€β”€ test_invoice.py
|   β”œβ”€β”€ test_usage.py
β”œβ”€β”€ scheduler.py
β”œβ”€β”€ main.py
β”œβ”€β”€ worker.py
└── README.md
└── LICENSE.md
└── CONTRIBUITING.md

πŸš€ Getting Started

Create & activate a virtual environment

python -m venv venv
source venv/bin/activate   # macOS/Linux
venv\Scripts\activate      # Windows

Install dependencies

pip install -r requirements.txt

Start Redis (required for the worker)

On Windows (WSL recommended):

redis-server

Start the FastAPI server

uvicorn main:app --reload

Your API is now live at:

http://127.0.0.1:8000
Swagger UI:

http://127.0.0.1:8000/docs

Start the background worker

In a new terminal:

rq worker

Start the scheduler (optional)

In another terminal:

python scheduler.py

πŸ§ͺ How to Use the API (Step‑by‑Step) Swagger UI

POST /aggregate/run

Preview an invoice

GET /invoice/preview/{customer_id}

Generate a real invoice

POST /invoice/{customer_id}


🧠 How It Works (Conceptual Overview)

Usage Ingestion Raw events are stored exactly as they happen:

Aggregation

A background job periodically:

fetches raw events

groups them by metric + customer

sums units

writes aggregated rows

This reduces billing load and keeps invoices fast.

Pricing Engine

A simple per‑unit pricing dictionary:

PRICING = {
    "api_calls": 0.0005,
    "storage_gb": 0.25,
    "workflows": 0.10,
}

You can easily extend this to:

tiered pricing

free allowances

per‑customer overrides

Invoice Preview

Reads aggregated usage so far and returns:

usage totals

estimated amount

billing window

No DB writes.

Invoice Generation

Reads aggregated usage, calculates the final amount, and writes an invoice row.


πŸ“¦ Tech Stack


🧱 Database Models