...
Quickstart: Click "regen" to generate a new secret key, then click "copy" to copy it to your clipboard. The generated key is safe for use in .env files and command-line arguments.
Usage Examples
Flask Application (.env file)
SECRET_KEY=your-generated-key-here
DATABASE_URL=postgresql://user:pass@localhost/dbname
DEBUG=False
Flask Application (Python)
from flask import Flask
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
# Or directly in config
app.config['SECRET_KEY'] = 'your-generated-key-here'
Command Line Export
$ python app.py
Not Using Flask?
The keys above work anywhere, but some frameworks and libraries expect a specific format. The secret key generator makes keys in the exact shape Flask, Django, Express, Laravel, Auth.js, Rails and WordPress expect. For Django on its own, the Django secret key generator uses the same 50 character alphabet as get_random_secret_key(). If you sign JSON Web Tokens with HS256, HS384 or HS512, use the JWT secret generator so the key is at least as long as the hash.
Frequently Asked Questions
What makes this different from a regular password generator?
This generator specifically avoids characters that can cause problems in environment variables, shell commands, and configuration files. Regular password generators often include characters like quotes, dollar signs, and backslashes that can break .env file parsing or cause command execution issues.
What characters are included in the generated keys?
The generator uses uppercase letters (A-Z), lowercase letters (a-z), numbers (0-9), underscores (_), and hyphens (-). These characters are universally safe across all shells, .env parsers, and configuration file formats.
How long should my Flask secret key be?
Flask recommends using secret keys that are at least 24 characters long. However, for better security, we recommend using 64 characters (the default) or more. Longer keys provide more entropy and are harder to brute-force.
Is it safe to use the same secret key in development and production?
No! Always use different secret keys for different environments. If your development key is compromised, it shouldn't affect your production environment. Generate separate keys for development, staging, and production.
How should I store my secret keys?
Best practices for storing secret keys:
- Use environment variables loaded from .env files (never commit .env to version control)
- Use secret management services like AWS Secrets Manager, HashiCorp Vault, or Azure Key Vault
- For containerized apps, use Kubernetes Secrets or Docker Secrets
- For CI/CD, use your platform's secret storage (GitHub Secrets, GitLab CI Variables, etc.)
What happens if I change my Flask secret key?
Changing your Flask secret key will invalidate all existing sessions and signed cookies. Users will be logged out and need to authenticate again. Any data signed with the old key (like remember-me tokens) will become invalid. Plan key rotation during maintenance windows and notify users if necessary.
Can I use this for other frameworks besides Flask?
Yes! These keys work for any framework or application that needs secure random strings. Common uses include Django's SECRET_KEY, Express.js session secrets, Rails secret_key_base, JWT signing secrets, and API keys.
Why not just use random characters from the keyboard?
Human-generated "random" strings are predictable and have low entropy. True randomness requires a cryptographically secure random number generator. This tool picks every character with crypto.getRandomValues(), the browser's cryptographically secure random number generator, so each character is unpredictable and equally likely.
Is this generator secure? Is my key sent over the internet?
Yes, it's secure. The entire key generation process happens in your browser using JavaScript. No data is sent to our servers or over the network. You can verify this by checking your browser's network tab or viewing the page source.
What happened to the seed and stream options?
Earlier versions of this page ran a PCG32 generator seeded from a single 64-bit random value, which capped every key at roughly 53 to 64 bits of real randomness no matter how long it was. That is not enough for a secret key. The generator now draws each character directly from crypto.getRandomValues, so a 64-character key really has about 384 bits of entropy, and there is nothing to seed. If you generated a key here before September 2026, rotate it.
Should I rotate my secret keys regularly?
Yes, periodic key rotation is a good security practice. Consider rotating keys:
- Every 3-6 months as a routine practice
- Immediately after a security incident
- When team members with access leave the organization
- After major application updates or infrastructure changes
Can I use special characters if my environment supports them?
While some environments handle special characters correctly, it's safer to stick with alphanumeric characters plus underscore and hyphen. This ensures your application remains portable and won't break when deployed to different environments or platforms.
How can I verify my .env file is being loaded correctly?
Add a debug print statement in your application to verify the key is loaded:
secret_key = os.environ.get('SECRET_KEY')
print(f"Secret key loaded: {bool(secret_key)}")
print(f"Secret key length: {len(secret_key) if secret_key else 0}")
Never print the actual secret key value, even in development!
What's the difference between SECRET_KEY and other API keys?
SECRET_KEY is used internally by your application for cryptographic operations like signing cookies and generating tokens. API keys authenticate your application to external services. Both should be kept secret, but SECRET_KEY is typically more critical as it affects all user sessions and security tokens in your application.
Security Best Practices
For Developers
- Never hardcode secret keys in your source code
- Add .env to your .gitignore file immediately
- Use different keys for different environments
- Document where keys are stored (but not the keys themselves)
- Implement key rotation procedures
- Monitor for exposed keys in version control
- Use secret scanning tools in your CI/CD pipeline
- Encrypt keys at rest when possible
- Limit access to production keys to essential personnel only
- Audit key access and usage regularly
For DevOps Teams
- Use centralized secret management solutions
- Implement proper access controls and audit logging
- Automate key rotation where possible
- Use separate keys for each application instance
- Never log or display secret keys in output
- Ensure keys are transmitted only over encrypted channels
- Implement break-glass procedures for emergency key access
- Regular security audits of key management practices
Also check out the Secret Key Generator, Django Secret Key Generator, JWT Secret Generator, Strong Random Password Generator and Random Passphrase Generator, or browse all the free tools.
-Sethers