Quick answer
Nano ID (nanoid) is a small, popular library for short, random, URL-safe IDs. Its default is 21 characters from a 64-symbol alphabet (A–Z, a–z, 0–9, _ and -), which gives 126 random bits, slightly more than a UUID v4. In Playxoft's Nano ID generator, choose an alphabet and size to generate IDs in bulk with the same unbiased algorithm, then use the collision calculator to see how long they stay safe at your generation rate.
- Unbiased generation
- Six alphabet presets
- Custom alphabets
- Size from 2 to 64
- Collision calculator
- Bulk up to 1,000
- Copy and download
How to use the Nanoid Generator
- 1Pick an Alphabet: URL-safe (the nanoid default), alphanumeric, lowercase, numbers, hex, no look-alikes, or your own Custom set.
- 2Drag Size to set how many characters each ID has. The bit count shows how much randomness that buys.
- 3Set How many IDs to generate and a list layout, then Copy all or Download them.
- 4In the Collision probability calculator, enter how many IDs you create per second, minute, hour or day.
- 5Read how long it takes before a duplicate becomes likely, and lengthen the ID if the tool marks it as risky.
What is Nano ID (nanoid)?
Nano ID, published as the nanoid package, is an open-source ID generator, first written for JavaScript and since ported to most languages. It produces short random strings like V1StGXR8_Z5jdHi6B-myT: by default 21 characters from a 64-symbol URL-safe alphabet (A–Z a–z 0–9 _ -).
Three properties made it popular:
- Secure randomness. It reads from the operating system's cryptographic generator (
crypto.getRandomValuesin browsers), neverMath.random(). - Unbiased. Every symbol is exactly equally likely, even with alphabets whose size does not divide 256.
- Compact. 21 characters carry 126 random bits, slightly more than the 122 in a UUID v4, in 15 fewer characters and with no hyphens.
This page implements the same algorithm in your browser, so the IDs are interchangeable with ones from the library. A nanoid is just a string: nothing else needs to know how it was made.
How does nanoid avoid modulo bias?
The obvious way to pick a symbol is alphabet[byte % alphabet.length]. With 62 symbols that is subtly wrong: 256 = 4 × 62 + 8, so the byte values 248–255 wrap around onto the first 8 symbols, making them 25% more likely than the rest (5 chances in 256 instead of 4). Biased IDs are easier to guess and collide sooner.
Nanoid masks each random byte to the smallest power of two that covers the alphabet (63 for 62 symbols) and throws away values that land outside it. It requests about 1.6 times as many bytes as it needs so one round is almost always enough:
function randomId(alphabet, size) {
const mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1;
const step = Math.ceil((1.6 * mask * size) / alphabet.length);
let id = "";
while (true) {
const bytes = crypto.getRandomValues(new Uint8Array(step));
for (const byte of bytes) {
const symbol = alphabet[byte & mask]; // undefined when out of range
if (symbol && (id += symbol).length === size) return id;
}
}
}How likely is a nanoid collision?
A collision means generating the same ID twice. With N possible IDs, the birthday bound says you can create about √(2 · N · ln(1 / (1 − p))) IDs before the chance of any duplicate reaches p. For 21 URL-safe characters, N = 64^21 = 2^126, so a 1% risk takes about 1.3 quintillion IDs.
How size changes that with the URL-safe alphabet, from this page's calculator:
| Size | Random bits | IDs for a 1% chance | At 1,000 IDs/hour | At 1,000 IDs/second |
|---|---|---|---|---|
| 8 | 48 | 2.4 million | 3.3 months | 40 minutes |
| 10 | 60 | 152 million | 17 years | 1.8 days |
| 12 | 72 | 9.7 billion | 1,111 years | 3.7 months |
| 16 | 96 | 40 trillion | 4.6 million years | 1,265 years |
| 21 (default) | 126 | 1.3 quintillion | 149 billion years | 41 million years |
| UUID v4 | 122 | 327 quadrillion | 37 billion years | 10 million years |
Every character you remove divides the safe number of IDs by 8 (the square root of 64). Short codes are fine for small, short-lived sets, but anything stored permanently should stay near the default, and the database column should still have a unique constraint.
Which alphabet should I choose?
A smaller alphabet carries fewer bits per character, so you need more characters for the same safety:
| Alphabet | Symbols | Bits per character | Characters for 126 bits |
|---|---|---|---|
| URL-safe (default) | 64 | 6.00 | 21 |
| Alphanumeric | 62 | 5.95 | 22 |
| Lowercase + digits | 36 | 5.17 | 25 |
| No look-alikes | 36 | 5.17 | 25 |
| Hex | 16 | 4.00 | 32 |
| Numbers | 10 | 3.32 | 38 |
- URL-safe is the best default for URLs, file names and database keys.
- Alphanumeric avoids
_and-, so the whole ID selects with a double-click. - Lowercase suits case-insensitive systems such as DNS names.
- No look-alikes suits codes that people read from paper or say over the phone.
Nanoid vs UUID: which should I use?
Both are random, secure identifiers; they differ in format and ecosystem:
| Nanoid (default) | UUID v4 | |
|---|---|---|
| Example | V1StGXR8_Z5jdHi6B-myT | 919108f7-52d1-4320-9bac-f847db4148a8 |
| Length | 21 characters | 36 characters |
| Random bits | 126 | 122 |
| Standard | Library convention | RFC 9562 |
| Native database type | No (text column) | Yes (uuid, 16 bytes) |
| Sortable by time | No | No (use v7 or a ULID) |
Use nanoid for IDs people see: short links, slugs, invite codes, client-side keys. Use a UUID when a database or API expects one, and a ULID or UUID v7 when you need time ordering.
How do I generate a nanoid in code?
// JavaScript — npm install nanoid (v5 is ESM-only; v3 also supports CommonJS)
import { nanoid, customAlphabet } from "nanoid";
nanoid(); // 21 URL-safe characters
customAlphabet("0123456789abcdef", 16)(); // 16 hex characters
# Python — pip install nanoid
from nanoid import generate
generate() # 21 URL-safe characters
generate("0123456789abcdef", 16)
// Go — github.com/matoous/go-nanoid/v2
id, err := gonanoid.New()
hex, err := gonanoid.Generate("0123456789abcdef", 16)
// C# — dotnet add package Nanoid
using NanoidDotNet;
var id = Nanoid.Generate();
var hex = Nanoid.Generate("0123456789abcdef", 16);Frequently asked questions
Last updated by the Playxoft engineering team.