← Back to all products

Usage Metering

$19

Usage tracking for SaaS with API calls, storage, compute minutes, and custom metrics.

📁 9 files
PythonMarkdown

📄 Product Preview

Try the interactive reader and demo tools below, or get the full product with all content unlocked.

📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample

📁 File Structure 9 files

usage-metering/ ├── LICENSE ├── README.md ├── examples/ │ └── basic_example.py ├── free-sample.zip ├── guide/ │ ├── 01_features.md │ ├── 02_quick-start.md │ └── 03_project-structure.md ├── index.html └── src/ └── main.py

📖 Documentation Preview README excerpt

Usage Metering

A Python usage tracking system for SaaS applications. Tracks API calls, storage, compute minutes, and custom metrics with quota enforcement, billing period aggregation, idempotency protection, and billing integration hooks — all built on Python's standard library.

Features

  • Event recording — Track any metric (API calls, storage, compute, custom)
  • Quota enforcement — Per-plan limits with configurable overage policies
  • Idempotency — Dedup protection prevents double-counting on retries
  • Usage summaries — Aggregate by tenant, metric, and billing period
  • Billing reports — Generate billing-ready reports with overage calculations
  • Top users — Identify heaviest users within a tenant
  • Batch recording — Record multiple events in one call
  • Plan management — Define usage plans with per-metric quotas

Requirements

  • Python 3.10+
  • No external dependencies (stdlib only)

Quick Start


python src/main.py --init-demo

# Record usage
curl -X POST http://localhost:8005/api/events \
  -H "Content-Type: application/json" \
  -d '{"tenant_id": "tenant_acme", "metric": "api_calls", "quantity": 1, "user_id": "user_1"}'

# Check usage
curl http://localhost:8005/api/usage/tenant_acme/api_calls

# Check quota
curl http://localhost:8005/api/quota/tenant_acme/api_calls

# Generate billing report
curl -X POST http://localhost:8005/api/billing-report \
  -H "Content-Type: application/json" \
  -d '{"tenant_id": "tenant_acme"}'

API Endpoints

MethodPathDescription
POST/api/eventsRecord a usage event
GET/api/usage/:tenant/:metricGet usage summary
GET/api/quota/:tenant/:metricCheck quota status
POST/api/billing-reportGenerate billing report

Project Structure


usage-metering/
├── README.md
├── LICENSE
├── src/

*... continues with setup instructions, usage examples, and more.*

📄 Code Sample .py preview

examples/basic_example.py#!/usr/bin/env python3 """ Usage Metering — Basic Usage Example ====================================== Demonstrates recording usage events, checking quotas, and generating billing reports. Run: python3 basic_example.py """ from __future__ import annotations import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src")) from main import MeteringEngine, UsageQuota def main() -> None: print("=== Usage Metering — Basic Example ===\n") engine = MeteringEngine(data_dir=Path("/tmp/metering-demo")) # --- 1. Create a usage plan with quotas --- plan = engine.create_plan("Pro Plan", [ UsageQuota(metric="api_calls", limit=1000, period="monthly", overage_allowed=True, overage_price_cents=1), UsageQuota(metric="storage_mb", limit=5120, period="monthly", overage_allowed=True, overage_price_cents=5), UsageQuota(metric="compute_minutes", limit=100, period="monthly", overage_allowed=False), ]) print(f"Created plan: {plan.name} ({len(plan.quotas)} quotas)") # --- 2. Assign plan to a tenant --- tenant_id = "tenant_acme" engine.assign_plan(tenant_id, plan.id) print(f"Assigned {plan.name} to {tenant_id}")
Buy Now — $19 Back to Products