Introduction
NanoID is a compact, URL-friendly unique ID generator that has been replacing UUID in JavaScript projects since 2017. At 21 characters, a NanoID is shorter than a UUID (36 characters with hyphens) while providing more entropy (126 bits vs 122 bits). The alphabet is URL-safe (A-Za-z0-9_-), so NanoIDs work in URLs, file names, and HTML attributes without encoding. Created by Andrey Sitnik, NanoID is used by millions of npm packages. This generator produces NanoIDs using the browser's CSPRNG, with configurable length and bulk generation.
What this tool does
- Generates NanoIDs using crypto.getRandomValues with a 64-character URL-safe alphabet.
- Configurable ID length from 1 to 36 characters (default 21, matching the NanoID standard).
- Bulk generation: produce up to 100 IDs at once, one per line.
- Uses the unbiased masking algorithm from the NanoID specification to avoid modulo bias.
- All generation happens client-side with no network calls.
How this tool works
The generator implements the NanoID algorithm: it uses a 64-character alphabet (0-9, A-Z, a-z, _, -) and draws random bytes from crypto.getRandomValues. To avoid modulo bias (which occurs when the random byte range is not evenly divisible by the alphabet size), it uses a bitmask: mask = (2 << floor(log2(alphabetSize - 1))) - 1. Each byte is AND-ed with the mask, and values outside the alphabet range are discarded. This is the same algorithm used by the nanoid npm package. The default size of 21 characters yields 126 bits of entropy.
How NanoID generation works
NanoID was created by Andrey Sitnik in 2017 as a faster, more compact alternative to UUID. The design goals were:
1. Compact: 21 characters vs UUID's 36 (with hyphens). 40% shorter in URLs. 2. URL-safe: Uses A-Za-z0-9_- (no +, /, or = that require URL encoding). 3. Fast: No dependencies, uses crypto.getRandomValues directly. 4. Unbiased: Masking algorithm avoids the modulo bias that skews shorter alphabets.
Entropy comparison:
| ID Type | Length | Alphabet | Entropy | |---|---|---|---| | UUID v4 | 36 chars | 16 (hex) | 122 bits | | NanoID (default) | 21 chars | 64 | 126 bits | | NanoID (16 chars) | 16 chars | 64 | 96 bits | | NanoID (36 chars) | 36 chars | 64 | 216 bits |
The collision probability for a 21-character NanoID is approximately 1 in 2^126, which is comparable to UUID v4. For a system generating 1 million IDs per second, the probability of a collision in 100 years is negligible.
NanoID is specified in its GitHub repository rather than an RFC, but the algorithm is stable and widely implemented across languages (JavaScript, Python, Go, Rust, Java, PHP, Ruby, Swift, and more).
How to use this tool
- Set the ID length using the slider. The default 21 matches the NanoID standard.
- Set the quantity if you need multiple IDs (up to 100).
- Click Generate New NanoID to produce fresh IDs.
- Copy a single ID or the entire batch using the Copy button.
- Use the IDs as database keys, URL slugs, or session tokens.
Real-world examples
URL shortener slug
A developer builds a URL shortening service. They use 10-character NanoIDs for slugs, producing IDs like "V1StGXR8_Z5". At 60 bits of entropy, the collision probability is low enough for a shortener with millions of URLs. The URL-safe alphabet means no percent-encoding is needed in the slug, keeping URLs clean: https://example.com/V1StGXR8_Z5.
React component keys
A frontend developer generates unique keys for dynamically created React list items. They use 12-character NanoIDs for each item. The IDs are short enough to not bloat the DOM, and the URL-safe alphabet avoids issues with HTML attribute parsing. Bulk generation produces 50 keys at once for initial list population.
Database primary key migration
A team migrates from UUID to NanoID for their PostgreSQL primary keys. UUIDs were causing index bloat because of their random byte distribution. NanoIDs are shorter (21 vs 36 bytes as text), and the team uses 26-character NanoIDs for even more entropy (156 bits). They generate test IDs with this tool to verify their schema migration scripts.
Comparison with similar methods
| Method | Complexity | Typical use |
|---|---|---|
| NanoID (21 chars) | Low | Compact URL-safe IDs for web applications |
| UUID v4 (36 chars) | Low | Standard unique identifiers, RFC 4122 compliant |
| ULID (26 chars) | Low | Sortable IDs with timestamp prefix |
Limitations or considerations
NanoIDs are not sortable by creation time because the entire ID is random. If you need time-ordered IDs, use ULID instead. NanoIDs use a 64-character alphabet that includes hyphens and underscores, which some systems interpret as operators or separators. If your target system has restrictions on these characters, you may need a custom alphabet. The tool generates IDs client-side and does not guarantee global uniqueness across distributed systems, though the collision probability is negligible for practical use.
Frequently asked questions
Is NanoID better than UUID?
For web applications, NanoID is more compact (21 vs 36 characters) and URL-safe. UUID has broader standards support (RFC 4122) and is recognized by more database systems. Choose based on your constraints.
How long should my NanoID be?
21 characters (the default) provides 126 bits of entropy, which is sufficient for virtually all applications. Use shorter IDs (10-12) for URL slugs where brevity matters and collision risk is low.
Can NanoIDs collide?
The probability is 1 in 2^126 for 21-character IDs. At 1 million IDs per second, you would need to generate for 10^24 years to have a 50% chance of collision.
Is the generation truly random?
Yes. It uses crypto.getRandomValues, the browser's CSPRNG. This is the same source used for TLS key generation.
Can I use a custom alphabet?
This tool uses the standard NanoID alphabet (A-Za-z0-9_-). For custom alphabets, use the nanoid npm package in your own code.
Conclusion
NanoID is the modern choice for compact, URL-safe unique identifiers in web applications. Use this generator for quick IDs during development or for production systems that need shorter keys than UUID. For sortable IDs, try the ULID Generator. For RFC-compliant identifiers, use the UUID Generator.