# API Key Generator — Random Secrets & Tokens

Source: https://passwordgeneratorlab.com/api-key-generator/

> Generate random API keys, tokens and secrets in hex, Base64URL or letters and digits from 128 to 512 bits, plus UUIDs. Made with Web Crypto in your browser.

Generated on your device with the Web Crypto API. Never sent anywhere, never stored. [How it works](https://passwordgeneratorlab.com/about/#methodology)

## Key facts

- Hex and Base64URL keys encode uniformly random bytes, so a 256-bit key has exactly 256 bits of entropy.
- A 256-bit key is 64 hex characters, or 43 Base64URL or letters-and-digits characters.
- RFC 7518 requires an HS256 JWT signing key of at least 256 bits.
- A random UUID v4 has 122 random bits; RFC 9562 advises against relying on UUIDs being hard to guess.
- A prefix such as myapp_live_ makes keys recognizable but adds no strength.

Last reviewed 9 November 2026

## How strong should a key be?

Unlike a password, a machine-to-machine secret never has to be remembered or typed, so there's no reason to keep it short. Brute force is already hopeless at 128 random bits — that's 3.4 × 10^(38) possibilities — but **256 bits** is the usual default for signing and encryption secrets, gives a large margin against future advances, and costs nothing but a few more characters.

| Use | Suggested |
| --- | --- |
| API keys and bearer tokens you issue | 256-bit Base64URL, with a prefix |
| JWT HS256 / HMAC-SHA-256 secret | 256-bit (HS384: 384-bit, HS512: 512-bit) |
| Session, CSRF or cookie-signing secret (e.g. framework `SECRET_KEY`) | 256-bit hex or Base64URL |
| AES-256 key (raw) | 256-bit hex — exactly 32 bytes |
| Database row or request ID (not a secret) | UUID v4 |

## Which format?

- **Base64URL** packs 6 bits into each character using only letters, digits, `-` and `_`, so it's compact and survives URLs, HTTP headers, JSON and `.env` files without escaping. We omit the `=` padding.
- **Hex** uses 4 bits per character. It's twice as long but universally accepted, and it's what many libraries expect when a key is given as text.
- **Letters & digits** suits systems that reject any punctuation. Each character is drawn uniformly from 62 with rejection sampling, adding about 5.95 bits; we round the length up so the key always meets the bit strength you picked.
- **UUID v4** is a standard identifier format with 122 random bits. Use it to name things, not to protect them.

The format doesn't change how hard a key is to guess — only how many characters carry the same bits. Decode the Base64URL or hex text back to bytes if your library wants raw key material.

## Generating keys in code

In production systems, generate secrets where they're used, with your platform's cryptographically secure generator. Each of these produces 256 random bits:

| Environment | Command |
| --- | --- |
| Shell (OpenSSL) | `openssl rand -hex 32` |
| Python | `secrets.token_urlsafe(32)` or `secrets.token_hex(32)` |
| Node.js | `crypto.randomBytes(32).toString("base64url")` |
| Browser / Deno / Bun | `crypto.getRandomValues(new Uint8Array(32))` |
| Go | `crypto/rand` — `rand.Read(b)` into a 32-byte slice |

Never use non-cryptographic generators such as `Math.random()`, Python's `random` module or `math/rand` for secrets: their output can be predicted.

## Handling the key safely

- Keep secrets in environment variables or a secrets manager, never in source code or Git history. A prefix makes accidentally committed keys easy for secret scanners to catch.
- When you issue API keys to others, store only a hash of each key (a fast hash such as SHA-256 is fine for long random keys) and show the full key once, at creation.
- Give each environment and client its own key so you can revoke one without breaking the rest.
- Rotate keys when someone with access leaves or a key may have been exposed.

Need something a person will type? Use the [password generator](https://passwordgeneratorlab.com/) or a [passphrase](https://passwordgeneratorlab.com/memorable-password-generator/) instead, and check the math in the [entropy calculator](https://passwordgeneratorlab.com/password-entropy-calculator/).

## Sources

- [RFC 7518, JSON Web Algorithms (JWA) — §3.2 HMAC key size](https://www.rfc-editor.org/rfc/rfc7518#section-3.2)
- [RFC 9562, Universally Unique IDentifiers (UUIDs)](https://www.rfc-editor.org/rfc/rfc9562)
- [OWASP Secrets Management Cheat Sheet](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html)
- [Python documentation: secrets — generate secure random numbers](https://docs.python.org/3/library/secrets.html)
- [MDN: Crypto.getRandomValues()](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues)

## Frequently asked questions

### Is it safe to generate an API key on a website?

This page generates keys with your browser's Web Crypto API and never sends them anywhere — you can check the Network tab or use it offline. For production secrets, generating them on the server that uses them (for example with openssl rand) avoids copying a secret through your clipboard at all.

### How long should an API key be?

Use at least 128 random bits; 256 bits is the common default. That's 43 characters in Base64URL or 64 in hex.

### Can I use this as a JWT secret?

Yes. HS256 needs a key of at least 256 bits, HS384 at least 384 and HS512 at least 512. Choose the matching strength here and keep the key on the server.

### Is a UUID a good API key?

Not on its own. A random UUID v4 has 122 random bits, which is hard to guess, but UUIDs are designed as identifiers and RFC 9562 advises against using them as security capabilities. Use a 256-bit random key for secrets.

### Does the prefix make the key stronger?

No. The prefix is a readable label that's the same for every key, so it adds no entropy. Its value is operational: you can tell keys apart and secret scanners can recognize them.

## Related guides

- [How are passwords cracked?](https://passwordgeneratorlab.com/guides/how-are-passwords-cracked/) — How attackers really get passwords: phishing, credential stuffing, online guessing and offline cracking of stolen hashes, and what length and randomness change.
- [Are online password generators safe?](https://passwordgeneratorlab.com/guides/are-online-password-generators-safe/) — When an online password generator is safe to use, the red flags that mean it isn't, and three ways to check for yourself in under a minute.

## More generators

- [Password generator](https://passwordgeneratorlab.com/) — Random passwords with full control over length and characters.
- [Passphrase generator](https://passwordgeneratorlab.com/memorable-password-generator/) — Memorable word-based passphrases from the EFF wordlist.
- [PIN generator](https://passwordgeneratorlab.com/pin-generator/) — Uniform random numeric PINs, 4–12 digits.
- [Wi-Fi password generator](https://passwordgeneratorlab.com/wifi-password-generator/) — WPA2/WPA3 passphrases that are easy to type on any device.
- [Bulk password generator](https://passwordgeneratorlab.com/bulk-password-generator/) — Up to 100 independent passwords at once.
- [Password policy generator](https://passwordgeneratorlab.com/password-policy-generator/) — Passwords that meet Entra ID, Active Directory, AWS IAM, PCI DSS and NIST rules.
- [Password strength checker](https://passwordgeneratorlab.com/password-strength-checker/) — Check how guessable a password is — entirely on your device.
- [Entropy calculator](https://passwordgeneratorlab.com/password-entropy-calculator/) — Bits of entropy and time-to-guess for any random password scheme.
