secret_key (Flask)
The secret_key is a Flask config value used to cryptographically sign data like session cookies and CSRF tokens. Without it, sessions and flash messages raise an error; it must be a long, random, secret string in production.
Flask
Flask signs the session cookie so the client cannot tamper with it, and that signature is computed with the secret_key. If an attacker knew the key they could forge sessions, so the value must be unpredictable and kept out of source control.
Set it via app.secret_key = '...' or app.config['SECRET_KEY'] = '...'. In real projects you load it from an environment variable rather than hard-coding it, e.g. os.environ['SECRET_KEY'], and you generate a strong value with secrets.token_hex(32).
Because the same key validates existing cookies, rotating it logs everyone out (their old session signatures no longer verify). Features that depend on it include session, flash messages, CSRF protection, and any itsdangerous-based token you generate.
Syntax
app.secret_key = os.environ["SECRET_KEY"]Examples
Python
import os, secrets
# generate once for production:
# secrets.token_hex(32)
app.secret_key = os.environ.get("SECRET_KEY", secrets.token_hex(32))