Principle:ALwrity ALwrity API Key Storage
| Knowledge Sources | |
|---|---|
| Domains | Security, DevOps, Onboarding |
| Last Updated | 2026-10-24 00:00 GMT |
Overview
Mechanism that stores per-provider API credentials so that downstream services can access them without the caller passing them explicitly, while keeping each tenant's keys isolated.
Description
API key storage is the pattern of persisting secret credentials (LLM provider keys, search API keys) in a way that is both accessible to the services that need them and isolated between tenants. The classic solution is environment variables, which are simple but global to the process. In a multi-tenant system this is insufficient: one tenant's key would be visible to every request. The refined pattern layers a per-tenant database store on top of the environment, where the environment acts as a development-time default and the database as the production source of truth.
This solves the dual problem of security (secrets are not hard-coded and are isolated per tenant) and convenience (services read from a single well-known location rather than receiving keys as parameters).
Usage
Use this principle when:
- A platform integrates with multiple external APIs that require keys.
- Different tenants use different keys (BYO-key model).
- A development setup uses environment variables but production requires per-tenant isolation.
- Keys must be checked at runtime before an expensive operation.
Theoretical Basis
Storage is a two-tier lookup with the environment as the L1 cache and the per-tenant database as the authoritative L2:
# Abstract two-tier lookup (NOT a real implementation)
def get_key(provider, user_id):
key = os.getenv(f"{provider.upper()}_API_KEY") # L1: env
if not key:
key = db_lookup(user_id, provider) # L2: tenant DB
return key
Writes propagate to both tiers (env for immediate use, DB for persistence).
Practical Guide
- Never hard-code. Keys come from env or DB, never from source.
- Per-tenant isolation. Store keys in the per-user database, not a shared table.
- Minimal scope. Load only the keys a request needs; do not pre-load all keys into memory for every request.
- Audit.' Log key presence/absence, never the key value itself.