Multi-Tenant Core API
The API behind the fleet's utility customer portals: two generations of it, and the migration from one to the other.
- Role
- Primary author of both generations, architecture and security owner
- Period
- 2023 - present
- Ownership
- 58% of v1 · 76% of v2
- Stack
- Fastify 5 · TypeScript (strict) · Zod · MongoDB · AWS SDK v3 · ECS Fargate · Vitest
Problem
A fleet of utility companies each needed a customer portal that talks to Oracle Utilities CCS, the billing system of record. The v1 server I inherited when I joined in 2023 is an Express codebase that predates me (58% mine by commit today), and it had no clean way to vary behavior per tenant. Each new client meant forked code and another security audit.
The v2 rebuild started in early 2026 with two goals: adding a tenant should take configuration only, and an authorization mistake should be hard to ship by accident. Six of the seven launched tenants run production on v1 today, and the seventh launched directly on v2 in June 2026. The first v1-to-v2 migration runs v2 beside its v1 service, certified, with the cutover held. Tenants move one at a time.
Constraints
Utility customers pay their bills through this API, so it has to stay available and return correct data. Oracle CCS is reached over OAuth 2.0 with per-tenant credentials. The team is small, so the secure way to add a route had to be the default way, and the migration cannot pause billing.
Decisions
- Both generations run side by side: the portals carry interceptors for either backend, and a tenant moves only when its parity checks pass.
- Tenant behavior is composed from a base configuration plus per-tenant overrides, assembled by strategy factories, so the request path does not branch on tenant name.
- Authorization is a route-level check: an account-scoped endpoint proves the caller owns the account before any data access.
Architecture
Fastify 5 on Node 22, ESM-only, TypeScript strict with no any types. Zod validates input at the boundary, and responses share one fixed envelope so clients parse a single shape.
On v2, admin reads and impersonated sessions are audited as well as writes, attributed to the acting admin. The audit records which fields a write changed, and the v2 capture path deep-redacts tokens, passwords and identifiers. Admin writes are kept for a year, admin reads for ninety days.
Where the billing system of record and the payment provider disagree, a reconciler reports the drift first. Repairs run guarded and in shadow before they write, and the account-linking path guards against duplicate registrations and signup conflicts. Enforcing the ten-digit account number on one tenant's sign-in forms targeted about 215 CCS errors a day that partial or oversized numbers had been generating.
Typecheck, lint, tests and a Snyk gate run in CI; Bitbucket Pipelines builds to ECR and deploys to ECS Fargate behind a shared ALB.
Outcome
- On v2 a new tenant is configuration plus provisioning. One launched tenant runs production on v2, the first migration runs v2 beside its v1, certified, with the cutover held, and v1 stays patched and audited until the last tenant moves.
- Ownership checks are part of the route contract on account-scoped endpoints, so they do not depend on a reviewer noticing a missing one.
- Billing drift is reported by a reconciler first; repairs run guarded, in shadow before they write.