Developer API Keys: High-Entropy Prefixing (sk_live_), SHA-256 Storage & Scoped Delegation
Developer API keys provide programmatic access to APIs for automated scripts, SDKs, and integrations. Secure API key architecture uses high-entropy cryptographically random strings, recognizable prefixes ('cosmos_live_', 'sk_live_') for automated secret scanning, stores only SHA-256 hashes in databases, and enforces granular permission scopes.
Step 1: Developer clicks 'Generate API Key' in the CosmosApp developer dashboard.
Developers inadvertently commit API keys into public GitHub repositories. If keys lack recognizable prefixes, secret scanning bots cannot detect leaks. If the database stores plaintext API keys, a database breach compromises every customer integration instantly.
The Monogrammed Factory Key: A company issues engraved master keys. Each key begins with the company's embossed logo so airport security scanners recognize it immediately. The factory keeps only an ink imprint of the key's teeth; even if the factory records are stolen, no working keys can be cut.
[ Key Creation: ]
1. Generate 32 crypto random bytes ──► "sk_live_9a8f2c3d..."
2. Compute SHA-256 Hash ─────────────► "e3b0c44298fc1c149afb..."
3. Store in Database:
INSERT INTO api_keys (key_hash, prefix, user_id, scopes)
VALUES ('e3b0...', 'sk_live_9a8f', 'usr_42', '["analytics:read"]')
4. Show full key to developer ONCE (Never recoverable!)
│
[ API Request: ]
Authorization: Bearer sk_live_9a8f2c3d...
│
├── Hash incoming key with SHA-256
├── Query: SELECT * FROM api_keys WHERE key_hash = SHA256(key)
└── Validate Scopes & Enforce Rate LimitsCosmosApp launches its public developer API. When a developer creates a key, CosmosApp generates a key prefixed with cosmos_live_ followed by 32 bytes of secure entropy. CosmosApp stores only the SHA-256 hash in PostgreSQL and registers its prefix with GitHub Secret Scanning, ensuring that if a developer commits a key to GitHub, it is instantly revoked.
Step 1: Developer clicks 'Generate API Key' in the CosmosApp developer dashboard.
Step 2: Server generates 32 cryptographically secure random bytes (256 bits of entropy).
Step 3: Server formats key with a distinct prefix: cosmos_live_<base62_random_bytes>.
Step 4: Server computes SHA-256 hash of the full key and saves hash, prefix, and allowed scopes in database.
Step 5: Full key is returned to the user with a warning that it will never be displayed again.
Step 6: On incoming API requests, server hashes the provided token and performs a constant-time lookup in the database.
import crypto from 'node:crypto';
// CosmosApp Developer API Key Generator:
export function generateApiKey(scopes: string[]) {
const prefix = 'cosmos_live_';
const secretBytes = crypto.randomBytes(32).toString('base64url');
const fullKey = `${prefix}${secretBytes}`;
// Store only the SHA-256 hash:
const keyHash = crypto.createHash('sha256').update(fullKey).digest('hex');
const keyPreview = `${prefix}${secretBytes.slice(0, 4)}...`;
return {
fullKey, // Displayed ONCE to developer
keyHash, // Stored in Database
keyPreview, // Displayed in UI lists
scopes,
};
}- ✕Storing API keys in plaintext in the database is acceptable because they aren't user passwords (A database breach exposes all customer API keys, enabling total API takeover).
- ✕API keys should use Argon2id (Argon2id is for low-entropy passwords; API keys have 256 bits of entropy, so fast SHA-256 hashing is secure and ensures low API latency).
Key Architectural Takeaways
- ✓Always use distinct prefixes (e.g. sk_live_, cosmos_live_) to enable automated secret scanning detection.
- ✓Never store API keys in plaintext; store the SHA-256 hash.
- ✓Present the full API key to the developer exactly once upon generation.
Storing raw, plaintext API keys in the database so the dashboard can 'display the key anytime'.
Store only the SHA-256 hash. If a user loses their key, they must roll and regenerate a new one.
Why should developer API keys begin with a recognizable prefix like 'sk_live_' or 'cosmos_live_'?