How to Generate a Random Secret in Python with secrets.token_urlsafe
Many programs need a secret nobody can guess: a key that signs login tokens, a password for a service, an API key. Python's secrets module makes one from your operating system's secure random source, and token_urlsafe returns it as text you can paste into a configuration file. This guide makes one on Python 3.15.2.
Syntax
import secrets
<secret> = secrets.token_urlsafe(<number of random bytes>)
| Part | What it means |
|---|---|
<number of random bytes> | How much randomness to use. 64 bytes is a strong default for signing keys |
token_urlsafe(...) | Returns text made of letters, digits, - and _, safe in URLs and config files |
Python's secrets documentation describes it as "a random URL-safe text string, containing nbytes random bytes", and adds that "on average each byte results in approximately 1.3 characters."
The Example
The script makes a 64-byte secret, prints it, and prints how many characters it has.
Prerequisites
- uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with
uv init.
Step 1: Write the Script
import secrets
token = secrets.token_urlsafe(64)
print(token)
print(len(token), "characters")

Step 2: Run It
uv run python secret_token.py
wdoJ86NBQ6sizAGipzPzqAURIJILrpiSfprEaAeH2CQOIgWpVFqQO3IRBKma8HD_dBf_DPF9viUTSFfHMdnj6A
86 characters

64 random bytes became 86 characters, about 1.3 per byte as the documentation says. You get a different secret every time you run it, so never copy one from a web page, this one included. The command works unchanged in PowerShell.
Why secrets and Not random?
The random module looks like it could do the same job, but it's the wrong tool. The secrets documentation says secrets "should be used in preference to the default pseudo-random number generator in the random module, which is designed for modelling and simulation, not security or cryptography."
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using random for a secret | Works, but its numbers are made for simulations and aren't meant to be secure | Use secrets |
| Copying a secret from an example or a colleague | Everyone who saw it knows your key | Run the script yourself and keep its output private |
| Committing the secret to Git | It's in the repository's history for good | Keep secrets in a file Git ignores, or in a secrets store |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| A library warns that a key is too short | The secret has fewer bytes than the algorithm needs | Make a longer one, such as token_urlsafe(64) |
FAQ
How do I make one without writing a script?
Run it from the terminal with python -c: uv run python -c "import secrets; print(secrets.token_urlsafe(64))".
How many bytes should I use?
Match what the system that uses the secret asks for. A signing key for HS512, for example, should be at least 64 bytes long.
Conclusion
secrets.token_urlsafe(64) returned an 86-character random secret, different on every run. Use the secrets module, never random, for anything that has to stay unguessable.
