Multi-tenant billing data is messy by default. We learned that the hard way when a staging API key leaked and started writing events into production accounts. Since then we’ve tightened our process so keys, auth, and Firestore rules work together without manual babysitting.
API Keys With Scope
- Each project receives its own key limited to ingestion endpoints.
- Keys carry metadata so we can trace requests back to specific services.
- Rotation runs on a schedule, UsageBox issues replacements and we notify integrators automatically.
If a key is compromised, we revoke it without touching other tenants.
Identity First
Every request arrives with a Firebase token. Middleware validates it, enriches the request with tenant context, and enforces per-account rate limits. The same token gates dashboard access, so support and customers see consistent data. The metering pipeline builds on that foundation.
Firestore Rules That Actually Block Things
- Documents live under customer-specific collections; cross-tenant reads fail by default.
- Support roles gain read access through service accounts with limited scopes.
- Usage events are append-only. Corrections happen through adjustments, not edits.
Network Hygiene
Cloud Run middleware handles schema validation, IP allow lists for strict customers, and rate limiting for noisy clients. We log everything to Cloud Logging so we can replay suspicious traffic if needed.
Partner Integrations
Partners receive their own key sets. We tag partner-generated usage differently so finance can reconcile commissions and attribution. Audit logs keep a trail of every call.
Rolling Out Secure Metering
- Configure Firebase Authentication and decide how tenants will be represented.
- Generate keys per project and store them in your secret manager, not in code.
- Review Firestore rules with both engineering and support to ensure the right access exists.
- Load test your ingestion endpoints with invalid payloads to confirm validation works.
The end result is dull by design: keys stay scoped, tenants stay isolated, and compliance reviews stop feeling stressful.