Flask SECRET_KEY Generator
64 hex characters — exactly what the Flask docs' secrets.token_hex() emits.
$ python -c 'import secrets; print(secrets.token_hex())'
What the Flask docs tell you to run
Straight from the Flask configuration docs: python -c 'import secrets; print(secrets.token_hex())'. With no argument, token_hex() uses CPython's DEFAULT_ENTROPY of 32 bytes and prints them as 64 lowercase hex characters — that is exactly what the generator above produces, without needing Python installed. Flask signs your session cookie with this key (via itsdangerous), so a weak or shared value lets anyone forge a session; a full-entropy random one closes that off.
How long should a Flask secret key be?
Long enough to be unguessable, which 32 random bytes comfortably is — and note that Flask's own docs are internally inconsistent here, so don't read too much into a specific number. The SECRET_KEY section shows 32 bytes (64 hex chars); the from_prefixed_env example elsewhere uses 16. The signing is HMAC-SHA-1 based, whose block gives a 160-bit effective ceiling, so anything from 16 bytes up is fine and 32 is the safe, doc-matching default. What Flask does not do is validate the format — a too-short or malformed key fails silently as a weaker signature, never as an error, which is exactly why getting it right at generation time matters.
Rotating it without logging everyone out
Since Flask 3.1 you can set SECRET_KEY_FALLBACKS to a list of old keys: Flask signs new sessions with SECRET_KEY and still accepts cookies signed with a fallback, so you can rotate without invalidating every live session at once. Keep the key in an environment variable, out of the repository, and use a distinct one per environment. This is the same 32-byte hex shape as a raw hex secret and a close cousin of Django's SECRET_KEY and Rails' secret_key_base — the differences are the length and the encoding, not the idea.
How do I generate a Flask SECRET_KEY?
The Flask docs' own command is python -c 'import secrets; print(secrets.token_hex())', which prints 64 hex characters (32 bytes). The generator above produces the same thing without Python. Set it as SECRET_KEY in config, read from an environment variable.
How long should a Flask secret key be?
32 random bytes (64 hex characters) is the doc-matching default and is plenty. Flask's own docs are inconsistent — the SECRET_KEY example shows 32 bytes, another example uses 16 — and the itsdangerous signing is HMAC-SHA-1-based, so 16 bytes and up are all fine. Flask validates no format, so a weak key fails as a weaker signature, never as an error.
What is SECRET_KEY_FALLBACKS?
A Flask 3.1 feature for key rotation: set it to a list of old keys and Flask signs new sessions with SECRET_KEY while still accepting cookies signed with a fallback. It lets you rotate without logging every user out at once.
The session is unavailable because no secret key was set — what now?
Flask raises this when SECRET_KEY is empty and you touch the session. Generate a key above and set app.config['SECRET_KEY'], ideally from an environment variable rather than a literal in the source.
Nothing leaves your browser
- Don't take our word for it — turn off your wifi. Every generator on this site keeps working with the network disconnected. That's the whole proof, and it takes five seconds.
- Every value comes from
crypto.getRandomValues()— the CSPRNG built into your browser, neverMath.random(). - Generated secrets are never transmitted, logged or stored: no server-side generation, no cookies, no localStorage.
- Verify it yourself in the network tab: after loading, the page only talks to our self-hosted, cookie-less analytics — which counts page views and which generator type gets copied, never any value.
- Strict Content-Security-Policy; no third-party script origins.
All generators
- Base64 Secret Generator
- UUID Generator
- Password Generator
- Hex Secret Generator
- JWT Secret Generator
- Passphrase Generator
- SSH Key Generator (Ed25519)
- PIN Generator
- WireGuard Key Generator
- API Key Generator
- TOTP Secret Generator
- Django SECRET_KEY Generator
- Laravel APP_KEY Generator
- NextAuth Secret Generator
- Rails secret_key_base Generator
- Strapi APP_KEYS Generator
- ULID Generator
- Nano ID Generator