# zakzap16/flyrank-capstone-metering-billing architecture

> Architecture diagram of the GitHub repository zakzap16/flyrank-capstone-metering-billing, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/zakzap16/flyrank-capstone-metering-billing
- Repository: https://github.com/zakzap16/flyrank-capstone-metering-billing

## Overview

Flyrank is a multi-tenant metering and billing API. The principal flow is tenant-authenticated usage submission through middleware and the meter endpoint, followed by idempotency, subscription/quota checks, integer-cost calculation, and event recording. Separate branches provide usage and quota reads, tenant authentication, Stripe checkout/billing, and Stripe webhook synchronization. The README documents these capabilities; source-index references and excerpts support selected call relationships, while some storage and route details remain unsampled.

Main entry points: [`src/main.py`](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/main.py) (FastAPI app), [`src/repositories`](https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/repositories) (Data repositories), [`src/api/v1/usage.py`](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/usage.py) (Usage endpoint), [`src/services/meter_service.py`](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/meter_service.py) (Meter service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and access"]
  node_app["FastAPI app<br/>[main.py]"]
  node_middleware["Request middleware"]
  node_tenant_auth["Tenant authentication<br/>[auth.py]"]
  node_auth_api["Auth endpoints<br/>[auth.py]"]
  node_meter_api["Meter endpoint<br/>[meter.py]"]
  node_usage_api["Usage endpoint<br/>[usage.py]"]
  node_auth_service["API key service<br/>[auth_service.py]"]
end

subgraph group_metering["Metering and quotas"]
  node_meter_service["Meter service<br/>[meter_service.py]"]
  node_quota_service["Quota service<br/>[quota_service.py]"]
  node_cost_service["Cost calculation<br/>[cost_service.py]"]
end

subgraph group_billing["Billing lifecycle"]
  node_billing_api["Billing and checkout<br/>[checkout.py]"]
  node_billing_routes["Billing routes<br/>[billing.py]"]
  node_subscription_api["Subscription routes<br/>[subscription.py]"]
  node_webhook_api["Stripe webhook handler<br/>[webhook.py]"]
  node_stripe_service["Stripe integration<br/>[stripe_service.py]"]
end

subgraph group_data["State and integrations"]
  node_repositories["Data repositories"]
  node_models["Domain records"]
  node_postgres[("PostgreSQL")]
  node_cache["Cache and idempotency<br/>[cache.py]"]
  node_redis[("Redis")]
  node_settings["Runtime settings<br/>[settings.py]"]
end

node_client(("SaaS client"))
node_stripe_actor(("Stripe"))

node_client -->|"sends requests"| node_app
node_app -->|"installs"| node_middleware
node_app -->|"routes to"| node_meter_api
node_app -->|"routes to"| node_usage_api
node_app -->|"routes to"| node_auth_api
node_app -->|"routes to"| node_billing_api
node_app -->|"routes to"| node_billing_routes
node_app -->|"routes to"| node_subscription_api
node_app -->|"routes to"| node_webhook_api
node_meter_api -->|"authenticates via"| node_tenant_auth
node_meter_api -->|"calls"| node_meter_service
node_meter_service -->|"checks quota"| node_quota_service
node_meter_service -->|"calculates cost"| node_cost_service
node_meter_service -->|"records usage"| node_repositories
node_meter_service -.->|"uses idempotency"| node_cache
node_quota_service -->|"reads usage and plans"| node_repositories
node_usage_api -->|"authenticates via"| node_tenant_auth
node_usage_api -->|"reads quotas"| node_quota_service
node_usage_api -->|"uses cost data"| node_cost_service
node_usage_api -->|"queries rollups"| node_repositories
node_tenant_auth -->|"verifies keys"| node_auth_service
node_tenant_auth -->|"caches tenants"| node_cache
node_tenant_auth -.->|"loads tenant"| node_repositories
node_auth_api -->|"manages keys"| node_auth_service
node_billing_api -->|"creates checkout"| node_stripe_service
node_billing_routes -->|"uses billing"| node_stripe_service
node_webhook_api -->|"verifies signature"| node_stripe_service
node_webhook_api -->|"deduplicates and updates"| node_repositories
node_webhook_api -->|"invalidates cache"| node_cache
node_stripe_actor -->|"sends events"| node_webhook_api
node_stripe_service -->|"calls Stripe API"| node_stripe_actor
node_repositories -->|"persists records"| node_models
node_models -->|"stored in"| node_postgres
node_cache -->|"reads and writes"| node_redis
node_cache -->|"loads Redis config"| node_settings
node_stripe_service -->|"loads credentials"| node_settings

click node_app "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/main.py"
click node_middleware "https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/api/middleware"
click node_tenant_auth "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/middleware/auth.py"
click node_auth_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/auth.py"
click node_meter_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/meter.py"
click node_usage_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/usage.py"
click node_billing_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/checkout.py"
click node_billing_routes "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/billing.py"
click node_subscription_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/subscription.py"
click node_webhook_api "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/webhook.py"
click node_meter_service "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/meter_service.py"
click node_quota_service "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/quota_service.py"
click node_cost_service "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/cost_service.py"
click node_auth_service "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/auth_service.py"
click node_stripe_service "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/stripe_service.py"
click node_repositories "https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/repositories"
click node_models "https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/models"
click node_cache "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/config/cache.py"
click node_settings "https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/config/settings.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_app,node_middleware,node_tenant_auth,node_auth_api,node_meter_api,node_usage_api,node_auth_service,node_client toneBlue
class node_meter_service,node_quota_service,node_cost_service toneAmber
class node_billing_api,node_billing_routes,node_subscription_api,node_webhook_api,node_stripe_service toneMint
class node_repositories,node_models,node_postgres,node_cache,node_redis,node_settings toneRose
class node_stripe_actor toneIndigo
```

## Components

### API and access

- [FastAPI app](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/main.py): `src/main.py`
- [Request middleware](https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/api/middleware): `src/api/middleware`
- [Tenant authentication](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/middleware/auth.py): `src/api/middleware/auth.py`
- [Auth endpoints](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/auth.py): `src/api/v1/auth.py`
- [Meter endpoint](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/meter.py): `src/api/v1/meter.py`
- [Usage endpoint](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/usage.py): `src/api/v1/usage.py`
- [API key service](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/auth_service.py): `src/services/auth_service.py`

### Metering and quotas

- [Meter service](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/meter_service.py): `src/services/meter_service.py`
- [Quota service](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/quota_service.py): `src/services/quota_service.py`
- [Cost calculation](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/cost_service.py): `src/services/cost_service.py`

### Billing lifecycle

- [Billing and checkout](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/checkout.py): `src/api/v1/checkout.py`
- [Billing routes](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/billing.py): `src/api/v1/billing.py`
- [Subscription routes](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/subscription.py): `src/api/v1/subscription.py`
- [Stripe webhook handler](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/api/v1/webhook.py): `src/api/v1/webhook.py`
- [Stripe integration](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/services/stripe_service.py): `src/services/stripe_service.py`

### State and integrations

- [Data repositories](https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/repositories): `src/repositories`
- [Domain records](https://github.com/zakzap16/flyrank-capstone-metering-billing/tree/main/src/models): `src/models`
- **PostgreSQL**
- [Cache and idempotency](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/config/cache.py): `src/config/cache.py`
- **Redis**
- [Runtime settings](https://github.com/zakzap16/flyrank-capstone-metering-billing/blob/main/src/config/settings.py): `src/config/settings.py`

### Other components

- **SaaS client**
- **Stripe**

## Connections

- SaaS client → FastAPI app: sends requests
- FastAPI app → Request middleware: installs (evidence: `src/main.py`)
- FastAPI app → Meter endpoint: routes to (evidence: `src/main.py`)
- FastAPI app → Usage endpoint: routes to (evidence: `src/main.py`)
- FastAPI app → Auth endpoints: routes to (evidence: `src/main.py`)
- FastAPI app → Billing and checkout: routes to (evidence: `src/main.py`)
- FastAPI app → Billing routes: routes to (evidence: `src/main.py`)
- FastAPI app → Subscription routes: routes to (evidence: `src/main.py`)
- FastAPI app → Stripe webhook handler: routes to (evidence: `src/main.py`)
- Meter endpoint → Tenant authentication: authenticates via (evidence: `src/api/v1/meter.py`)
- Meter endpoint → Meter service: calls (evidence: `src/api/v1/meter.py`)
- Meter service → Quota service: checks quota (evidence: `src/services/meter_service.py`)
- Meter service → Cost calculation: calculates cost (evidence: `src/services/meter_service.py`)
- Meter service → Data repositories: records usage (evidence: `src/services/meter_service.py`)
- Meter service → Cache and idempotency: uses idempotency
- Quota service → Data repositories: reads usage and plans (evidence: `src/services/quota_service.py`)
- Usage endpoint → Tenant authentication: authenticates via (evidence: `src/api/v1/usage.py`)
- Usage endpoint → Quota service: reads quotas (evidence: `src/api/v1/usage.py`)
- Usage endpoint → Cost calculation: uses cost data (evidence: `src/api/v1/usage.py`)
- Usage endpoint → Data repositories: queries rollups (evidence: `src/api/v1/usage.py`)
- Tenant authentication → API key service: verifies keys (evidence: `src/api/middleware/auth.py`)
- Tenant authentication → Cache and idempotency: caches tenants (evidence: `src/api/middleware/auth.py`)
- Tenant authentication → Data repositories: loads tenant
- Auth endpoints → API key service: manages keys (evidence: `src/api/v1/auth.py`)
- Billing and checkout → Stripe integration: creates checkout (evidence: `src/api/v1/checkout.py`)
- Billing routes → Stripe integration: uses billing (evidence: `src/api/v1/billing.py`)
- Stripe webhook handler → Stripe integration: verifies signature (evidence: `src/api/v1/webhook.py`)
- Stripe webhook handler → Data repositories: deduplicates and updates (evidence: `src/api/v1/webhook.py`)
- Stripe webhook handler → Cache and idempotency: invalidates cache (evidence: `src/api/v1/webhook.py`)
- Stripe → Stripe webhook handler: sends events
- Stripe integration → Stripe: calls Stripe API
- Data repositories → Domain records: persists records (evidence: `src/repositories/usage_repo.py`)
- Domain records → PostgreSQL: stored in
- Cache and idempotency → Redis: reads and writes
- Cache and idempotency → Runtime settings: loads Redis config (evidence: `src/config/cache.py`)
- Stripe integration → Runtime settings: loads credentials (evidence: `src/services/stripe_service.py`)
