Skip to content
Back to CurriculumLesson 28 of 30
resilience-securityIntermediate

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.

Interactive Visual Concept ExplainerStep-by-Step Educational Simulation
Visual Step-by-Step Concept Explainerresilience-security
Developer API Keys: High-Entropy Prefixing (sk_live_), SHA-256 Storage & Scoped Delegation
Steps:
High-Entropy Prefix Architecture
Key: sk_live_51Mza...32bytes
Prefix enables automated GitHub / GitGuardian secret scanning to prevent git commit leaks.
One-Way Hash Storage
Database stores ONLY: SHA-256(apiKey)
If database is compromised, plaintext API keys are completely unrecoverable!
Step 1 of 6
Auto-stepping (1x)

Step 1: Developer clicks 'Generate API Key' in the CosmosApp developer dashboard.

Security Guarantee: Always use distinct prefixes (e.g. sk_live_, cosmos_live_) to enable automated secret scanning detection.
Why This Architectural Concept Exists

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.

Intuitive Mental Model

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.

Architectural System TopologyProtocol Diagram
[ 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 Limits
CosmosApp Production Milestone

CosmosApp 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-by-Step Execution Sequence
01

Step 1: Developer clicks 'Generate API Key' in the CosmosApp developer dashboard.

02

Step 2: Server generates 32 cryptographically secure random bytes (256 bits of entropy).

03

Step 3: Server formats key with a distinct prefix: cosmos_live_<base62_random_bytes>.

04

Step 4: Server computes SHA-256 hash of the full key and saves hash, prefix, and allowed scopes in database.

05

Step 5: Full key is returned to the user with a warning that it will never be displayed again.

06

Step 6: On incoming API requests, server hashes the provided token and performs a constant-time lookup in the database.

Production Architecture Blueprint
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,
  };
}
Common Architectural Misconceptions
  • ✕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.
Common Architectural Pitfall

Storing raw, plaintext API keys in the database so the dashboard can 'display the key anytime'.

Production Best Practice Fix

Store only the SHA-256 hash. If a user loses their key, they must roll and regenerate a new one.

Interactive Knowledge Check

Why should developer API keys begin with a recognizable prefix like 'sk_live_' or 'cosmos_live_'?