Disclosure: Some tools link to SaaS products we may earn a commission from — at no cost to you. All tools are free and run entirely in your browser. See full disclosure →
Generated locally via Web Crypto API — zero server contact
0 bits · Weak

Why API Keys Need a Specific Design

An API key is not just a random string — it's a credential that will be embedded in code, passed through CI/CD pipelines, logged in request headers, and copied into config files by developers who may not always be careful. Good API key design accounts for these failure modes upfront.

The industry has converged on a pattern: a readable prefix that identifies the key's type and environment, followed by a cryptographically random body long enough that guessing is computationally infeasible. This design gives you three things: scannability (automated tools can find leaked keys by pattern), safety (test keys can't accidentally hit production endpoints), and security (128+ bits of entropy on the random body).

How Stripe does it — the gold standard

Stripe's key format is widely copied because it solves every relevant problem elegantly:

  • sk_live_ — live secret key (server-side only, full access)
  • pk_live_ — live publishable key (safe for client-side code)
  • sk_test_ — test secret key (no real charges)
  • pk_test_ — test publishable key (safe for client-side test code)
  • whsec_ — webhook signing secret (rotate on vendor change)
  • rk_live_ — restricted key (scoped to specific API endpoints)

The prefix makes the environment and capability immediately visible. A developer reading config can tell at a glance whether a key is live or test, secret or publishable, restricted or full-access. This is the pattern to follow for your own service.

Designing Your Prefix Convention

If you're building an API that issues keys, establish your prefix convention before your first user. Changing it after launch means issuing new keys to all existing users and deprecating the old format — painful.

Key typeRecommended prefixBody lengthNotes
Production API keysk_live_ or api_live_32 charsServer-side only. Full entropy.
Development/test keysk_test_ or api_dev_32 charsCan be stored less securely in dev configs.
Public/embeddable keypk_ or pub_24 charsSafe for client code. Should only allow read or non-destructive actions.
Webhook secretwhsec_32–48 charsUsed to sign payloads, not to auth API calls. Rotate on every webhook endpoint change.
Service-to-service tokensvc_ or int_48 charsHigher entropy for internal services where rotation is automated.
Personal access tokenpat_ or ghp_36 charsUser-scoped. Should expire or be revocable individually.

The Secret Rotation Protocol

Key rotation is the most important security practice most teams skip. The reason to rotate isn't just that a key might be compromised — it's that you want to discover a compromise quickly, not months later when you finally rotate. A key that's been sitting unchanged for 18 months has an unknown exposure window. A key rotated every 90 days has a known maximum exposure window.

Zero-downtime rotation pattern

  1. Generate a new key using this tool.
  2. Add the new key to your platform alongside the old key (most APIs accept multiple active keys per account).
  3. Update all services to use the new key (deploy, verify health checks pass).
  4. Revoke the old key only after confirming all services are using the new one.

Never revoke the old key before deploying the new one. The brief overlap period is intentional — it prevents downtime during rotation.

Storing Keys in Deployment Platforms

The correct way to store API keys in production is encrypted environment variables, never in committed code. Modern deployment platforms make this easy:

  • Cloudflare Workers Secrets — encrypted at rest, injected as env vars at runtime, not visible after creation even to the account owner.
  • Vercel Environment Variables — per-environment (preview/production/development), encrypted, visible only on first creation.
  • AWS Secrets Manager — native rotation automation, IAM-controlled access, full audit trail.
  • HashiCorp Vault — self-hosted option for organizations that can't use cloud-managed secrets.

Whatever platform you use, the principle is the same: the secret lives in the platform's encrypted store and is injected into your runtime environment at deploy time. Your codebase contains only the variable name, never the value.

Implementing Key Hashing on Your Backend

If your service issues API keys to users, you must hash them before storage. The pattern mirrors password storage:

  • At creation: generate key → show to user once → store sha256(key) in database
  • At verification: receive key in request header → compute sha256(key) → look up hash in database
  • At rotation: generate new key → show once → store new hash → invalidate old hash

If your database is ever compromised, attackers get SHA-256 hashes, not raw keys. SHA-256 is appropriate here (unlike passwords) because API keys already have sufficient entropy — an attacker cannot precompute a rainbow table for 32-character random strings.

Manage secrets properly — use Cloudflare Workers

Cloudflare Workers Secrets encrypts your API keys at rest, injects them at runtime, and never exposes them after storage — even in your dashboard. Free tier handles millions of requests.

Frequently Asked Questions

What format should an API key use?

Modern API keys typically use a prefix (like sk_live_, ghp_, or pat_) followed by a random body of 24–64 characters. The prefix identifies the key type, environment (live vs test), and issuer, while the random body provides security. Stripe, GitHub, Twilio, and most major SaaS products follow this convention. The total entropy should be at least 128 bits for production keys — that means a 32-character alphanumeric body gives 190 bits, comfortably over the threshold.

Should API keys be alphanumeric or hex?

Alphanumeric (A-Z, a-z, 0-9) gives more entropy per character (log₂(62) ≈ 5.95 bits vs log₂(16) = 4 bits for hex). For a given key length, alphanumeric keys are about 49% more secure than hex keys. Use hex if your system has constraints that require hex (some APIs use hex specifically for consistency with hash-based signatures). Otherwise alphanumeric is the stronger choice.

How do I handle API key storage on my server?

Never store the raw API key in your database. Store a hash of the key (SHA-256 or bcrypt). When the user presents the key on an API call, hash it and compare against your stored hash — exactly like password verification. Show the full key only once at creation time and tell the user to copy it immediately. This means if your database is compromised, attackers get hashes, not usable keys.

What is secret scanning and does it affect me?

GitHub, GitLab, Bitbucket, and cloud providers like AWS scan code commits for credential patterns. They look for specific prefixes (sk_live_, AKIA for AWS, etc.). If you push a file containing a matching key, they automatically revoke it and notify you. If you build your own service, register your prefix pattern with GitHub's Secret Scanning Partner Program so your keys get the same protection — it's free and takes about 30 minutes to set up.

How often should I rotate API keys?

Production secrets: every 90 days. Service-to-service tokens: every 30 days. After any team member departure or role change. After any suspected breach — even if unconfirmed. When migrating vendors. The goal is to limit the window of damage from an undetected leak. Automate rotation using environment variable management in your deployment platform (Cloudflare Workers Secrets, Vercel encrypted env vars, AWS Secrets Manager).

Is this different from UUID v4?

UUID v4 has 122 bits of entropy in a standardised format (8-4-4-4-12 hex). It is fine as an identifier but its fixed format is recognisable — attackers know the pattern. A prefixed API key with a 32-char alphanumeric body has ~190 bits of entropy and a custom prefix, so it can be scoped and rotated by environment (live/test) in ways UUID cannot. For API authentication specifically, prefixed keys with a random body are the current industry standard.

ID & Secret Generators
UUID v4 Generator UUID v7 Generator Nano ID Generator Password Generator
💡
Need AI tools for your development workflow?

Explore our reviews of the best AI coding assistants, documentation generators, and developer productivity tools — ranked by real-world usefulness.

Browse AI Tools for Developers →
The DevTools Team
Infinfy Engineering
We build free developer utilities that we actually use ourselves. No accounts, no tracking, no backend — just fast, accurate, in-browser tools. Part of Infinfy Solutions.