.env · security🔥 popular🔒 100% client-side — never leaves your browser

API Key Generator

Generate a secure API key for your own service: this API key generator creates prefixed API keys, API secrets, OAuth client secrets and admin secrets from 32–40 random bytes, plus the SHA-256 hash your server should store instead of the key.

By Aliniyaz Mamat · last reviewed

Be the first to rate
generated locally · crypto.getRandomValues · nothing sent anywhere
API Key
key_ prefix + 32 bytes hex — give this to the client; store only its SHA-256 hash
Secret value
.env line
API_KEY=
API Secret
Base64url, 32 bytes — pair with a public API key for HMAC signing
Secret value
.env line
API_SECRET=
OAuth Client Secret
Base64url, 32 bytes — for OAuth2 / OIDC client credentials
Secret value
.env line
CLIENT_SECRET=
Admin Secret
Base64url, 40 bytes — extra entropy for privileged/admin-only routes
Secret value
.env line
ADMIN_SECRET=
Public API Key (prefixed, safe to expose)
pk_ prefix + 24 bytes hex — identifiable, not secret
Secret value
.env line
NEXT_PUBLIC_API_KEY=
Store this, not the key
SHA-256 of the API key above. Save the hash (and the prefix for lookups) in your database, show the key to the user once, and compare hashes on each request. A database leak then exposes no usable keys.
sha256:

Key takeaways

  • ✓Every value has at least 256 random bits (32 bytes): far beyond brute force.
  • ✓The key_ prefix makes keys easy to find in logs and lets secret scanners (GitHub secret scanning, gitleaks) match them with a simple pattern.
  • ✓Store only the SHA-256 hash of an API key. Show the key once at creation, then compare hashes on every request.
  • ✓API key = identifies the caller and may appear in logs; API secret = proves identity and must never leave the server.

What it does

  • ▸Prefixed API key (key_ + 64 hex chars)
  • ▸API secret and OAuth client secret (32 bytes, base64url)
  • ▸Admin secret with 40 bytes for privileged routes
  • ▸SHA-256 hash of the key for database storage
  • ▸Ready .env lines, 100% client-side

Privacy

Runs 100% in your browser. Your .env never touches our servers.

client-side only

When to use this tool

  • →Issuing API keys to customers of your SaaS
  • →Service-to-service authentication inside your own infrastructure
  • →A client secret for an OAuth app you register with your own auth server
  • →Protecting an internal admin endpoint or a cron route with a shared secret

Common mistakes

  • ⚠Storing API keys in plain text in the database: one SQL injection or backup leak exposes every customer
  • ⚠Comparing keys with ===: use a constant-time comparison (crypto.timingSafeEqual, hmac.compare_digest) or compare hashes
  • ⚠Putting an API secret in frontend code or a mobile app bundle. Anything shipped to clients is public
  • ⚠Keys without expiry or rotation: give each key an ID, a creation date and a way to revoke it

Designing API keys like Stripe and GitHub do

PartExampleWhy
Prefixkey_, sk_live_, ghp_humans and scanners see what the key is for
Environment marker_live_ / _test_prevents using test keys in production by accident
Random body64 hex chars (32 bytes)unguessable, URL- and header-safe
Optional checksumlast 6 chars = CRC32 of the bodylets scanners reject random strings that only look like keys

GitHub's token format (ghp_ + 30 random chars + a CRC32 checksum) is the reason its secret scanning has very few false positives. A prefix alone already gets you most of the benefit.

Storing and checking API keys on the server

import { createHash, timingSafeEqual } from "node:crypto";

const sha256 = (s: string) => createHash("sha256").update(s).digest();

// on creation: show apiKey to the user once, store only the hash + a lookup prefix
const record = { prefix: apiKey.slice(0, 12), hash: sha256(apiKey).toString("hex") };

// on each request
function verify(presented: string, stored: { hash: string }) {
  const a = sha256(presented);
  const b = Buffer.from(stored.hash, "hex");
  return a.length === b.length && timingSafeEqual(a, b);
}

A fast hash (SHA-256) is fine here, unlike for passwords: the key has 256 random bits, so there is nothing to brute-force. For human passwords use the bcrypt generator instead.

Keeping your own API keys out of git

  • Read keys from environment variables (process.env.PAYMENTS_API_KEY), never hard-code them.
  • Commit a .env.example with empty values, generated by the .env.example generator.
  • Scan before pushing: the ENV leak checker flags known key formats in a .env.
  • If a key leaks, revoke it first and investigate second. Rotation is cheap when every key has an ID.

Frequently asked questions

What's the difference between an API key and an API secret?

An API key identifies the caller and may show up in logs or dashboards. An API secret proves the caller's identity, for example to sign requests with HMAC, and must stay on the server.

How long should an API key be?

At least 32 random bytes (256 bits). Shorter keys (16 bytes, 128 bits) are still unguessable, but 32 bytes costs nothing extra.

Should I prefix my API keys?

Yes. A prefix like key_ or acme_live_ makes leaked keys easy to recognise and lets you register a pattern with GitHub secret scanning or gitleaks.

Why store the SHA-256 hash instead of the key?

If your database leaks, hashes cannot be used to call your API. Because API keys are long and random, a plain SHA-256 is enough; no slow password hash is needed.

Is the generated key uploaded or stored anywhere?

No. Keys and hashes are computed in your browser with the Web Crypto API.

Related tools

Learn more

coming soon

Get notified when env syncing launches

We're building a tiny tool to keep .env files in sync across teammates and environments. Leave your email — no spam, just a single launch ping.

We use your address only for this launch email. You can ask us to delete it at any time. Privacy policy