Flask SECRET_KEY Generator

64 hex characters — exactly what the Flask docs' secrets.token_hex() emits.

Copy the result before closing this tab.

Generate 32 random bytes encoded as 64 hex characters for Flask's SECRET_KEY. Flask uses this key to sign session cookies and extensions may use it too. Load it from your environment or secret manager.

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?

Use a long, cryptographically random value. The default here is 32 random bytes encoded as hex, following the example in Flask’s configuration documentation. Load it from your environment or secret manager; do not hardcode it into the application repository.

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.

FAQ

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.

How generation works

About & privacy

Generated on your device

Web Crypto provides the randomness. Generation works offline after the page has loaded.

No tracking or saved values

No analytics scripts, accounts, cookies or local storage. Values stay in this tab until you copy them.

Browser and device security

A compromised browser or device can expose values. For production private keys, use a trusted local tool.

Other generators

All generators ↗