Flask Secret Key Generator

Flask uses SECRET_KEY to sign session cookies. Generate a 32-byte random key here.

SECRET_KEY

Generating in your browser…

Generated on your device with the Web Crypto API. Never sent or stored.

.env line
SECRET_KEY=

Prefer the terminal?

  • Python
    python -c "import secrets; print(secrets.token_hex(32))"

How to use your Flask SECRET_KEY

Copy the value above and add it to your environment and load it with app.config['SECRET_KEY'] = os.environ['SECRET_KEY']. Keep it out of source control: commit an .env.example with an empty value instead, and set the real value in your hosting provider's environment settings or a secrets manager.

Flask signs the session cookie with this key, and extensions such as Flask-WTF use it for CSRF tokens. Anyone who knows the key can forge a session, so a hard-coded key in app.py is a common and serious mistake. A 32-byte hex key (64 characters) is plenty.

Is it safe to generate secrets in a browser?

Only if the secret is created locally, which is how this page works. The value comes from crypto.getRandomValues, the same cryptographically secure source browsers use for TLS keys, and it never leaves your device: there is no network request, log or analytics event that includes it.

If you prefer the command line, the commands on this page produce an equivalent secret on your own machine. Either way, use a different secret for development, staging and production, and rotate it straight away if it ever appears in a log, a screenshot or a public repository.

Frequently asked questions

Where do I put the Flask secret key?

In an environment variable, read with os.environ['SECRET_KEY']. Never hard-code it in app.py.