How Do I Generate Random API Tokens From The Terminal?
API tokens are everywhere: personal access tokens, webhook secrets, session keys, “bearer” strings for internal tools, and one-off secrets you hand to a teammate for testing. When you need a strong random token quickly, the terminal is often the fastest and most dependable place to create it—no extra apps, no copy-pasting from questionable generators, and no waiting on a UI.
This article walks through practical ways to generate random API tokens from a computer terminal, with examples for macOS, Linux, and Windows. It also covers token length choices, encoding formats, and safe handling tips.
What makes a good API token?
A solid API token should be:
- Unpredictable: generated from a cryptographically secure random source.
- Long enough: resistant to guessing and brute force.
- Easy to store and transmit: often as hex or Base64 text.
- Unique: one token per client/app/service whenever possible.
How long should it be?
A common target is 128 bits of randomness minimum for many use cases, and 256 bits for high-value tokens.
- 128-bit token
- Hex: 32 characters
- Base64: 22 characters (often 24 with padding
==)
- 256-bit token
- Hex: 64 characters
- Base64: 43 characters (often 44 with padding
=)
If you’re unsure, generate 256 bits. The extra length is rarely a problem and raises the bar significantly.
Prefer cryptographically secure random sources
On Unix-like systems, the safest sources are provided by the OS: /dev/urandom and tools that call secure system APIs. On Windows, PowerShell has cryptography helpers.
Avoid using random() in scripting languages unless it’s clearly a crypto-safe generator.
Generate tokens with OpenSSL (macOS/Linux)
OpenSSL is widely available and straightforward.
Hex tokens
Generate a 32-byte (256-bit) token as hex:
Bash
Example output (yours will differ):
Html
Base64 tokens
Generate 32 bytes and encode as Base64:
Bash
This may include + and /, which can be awkward in URLs. If you need something URL-friendly, see the Base64url section below.
Generate tokens with head + /dev/urandom (macOS/Linux)
This method is simple and works on most systems.
Hex encoding using xxd
Bash
head -c 32reads 32 bytes (256 bits).xxd -pprints plain hex.
Base64 encoding using base64
Bash
If you want a single line without wrapping:
Bash
Generate tokens with python (macOS/Linux/Windows)
Python’s secrets module is built for secure tokens.
URL-safe tokens (recommended for many APIs)
Bash
Notes:
- The number
32here is bytes before encoding, not characters. - Output is Base64url-like and typically safe in URLs, headers, and config files.
Hex tokens
Bash
That prints 64 hex characters (256 bits).
Generate tokens with node (macOS/Linux/Windows)
Node.js can generate secure random bytes via the crypto module.
Hex token
Bash
Base64 token
Bash
If you need a URL-safe variant, you can replace characters and trim padding:
Bash
Generate tokens on Windows with PowerShell
PowerShell can pull secure random bytes and encode them cleanly.
Hex token (256-bit)
Powershell
Base64 token (256-bit)
Powershell
Base64url token (URL-friendly)
Powershell
Base64 vs hex: which should you pick?
Both are fine; choose based on where the token goes.
- Hex
- Pros: only
0-9a-f, very copy/paste friendly, rarely needs escaping - Cons: longer output for the same randomness
- Pros: only
- Base64
- Pros: shorter output
- Cons: may include characters that need encoding in URLs or some config contexts
- Base64url
- Pros: shorter and URL/header friendly
- Cons: not always the default option in tooling (but easy to produce)
If the token might appear in a URL path or query string, Base64url or hex is usually the least painful.
Quick recipes (copy/paste)
256-bit hex (macOS/Linux)
Bash
256-bit Base64url (Python)
Bash
256-bit hex (Node)
Bash
256-bit Base64url (PowerShell)
Powershell
Safe handling tips in the terminal
Generating a strong token is only half the job; handling it poorly can still leak it.
- Avoid saving tokens in shell history: commands that print secrets can end up in history. Consider turning off history temporarily in your shell session or generating via a script file you delete afterward.
- Avoid pasting tokens into shared chat: use a secret manager or a secure channel.
- Limit token scope: if your system supports scopes/permissions, grant the minimum needed.
- Set expiration when possible: short-lived tokens reduce risk.
- Rotate tokens: treat rotation as routine, not as emergency work.
The terminal offers quick, repeatable ways to create strong API tokens using trusted system randomness. Pick a tool you already have, choose a length like 32 bytes (256 bits), and output in hex or Base64url depending on where the token will live. With a few reliable one-liners, you can generate tokens on demand without relying on web tools or manual guesswork.












