Skip to main content

How to Generate a Random Secret in Python with secrets.token_urlsafe

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
import secrets

<secret> = secrets.token_urlsafe(<number of random bytes>)
PartWhat 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​

secret_token.py
import secrets

token = secrets.token_urlsafe(64)
print(token)
print(len(token), "characters")

The secret_token.py script in a macOS-style code window: it imports secrets, makes token with secrets.token_urlsafe(64), and prints the token and its length in characters.

Step 2: Run It​

Terminal
uv run python secret_token.py
Output
wdoJ86NBQ6sizAGipzPzqAURIJILrpiSfprEaAeH2CQOIgWpVFqQO3IRBKma8HD_dBf_DPF9viUTSFfHMdnj6A
86 characters

The terminal after uv run python secret_token.py: an 86-character random string of letters, digits, hyphens and underscores, then 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​

MistakeWhat happensDo this instead
Using random for a secretWorks, but its numbers are made for simulations and aren't meant to be secureUse secrets
Copying a secret from an example or a colleagueEveryone who saw it knows your keyRun the script yourself and keep its output private
Committing the secret to GitIt's in the repository's history for goodKeep secrets in a file Git ignores, or in a secrets store

Troubleshooting​

SymptomLikely causeFix
A library warns that a key is too shortThe secret has fewer bytes than the algorithm needsMake 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.

Additional Resources​

  • secrets — the module, with recipes.
  • PEP 506 — why the module was added.