NanoID Generator: The Complete Guide to URL-Friendly Unique IDs
Learn how to generate secure, compact, URL-safe NanoIDs online. Customize length and alphabet, generate in bulk, and understand the collision math behind NanoID.
Table of Contents
NanoID Generator: The Complete Guide to URL-Friendly Unique IDs
If you've ever needed a unique identifier that's small, secure, and safe to drop straight into a URL, file name, or HTML attribute, NanoID is likely exactly what you're looking for. Created by Andrey Sitnik (known online as ai), NanoID is a tiny 130-byte string ID generator designed to be a faster, shorter, and simpler alternative to UUIDs. Its default output is a 21-character string drawn from a URL-safe alphabet, which means you never have to worry about escaping hyphens or special characters when you use it on the web.
Developers reach for NanoID because it solves a real problem: UUID v4 strings are 36 characters long, include hyphens that complicate URLs and database keys, and add visual noise with no extra uniqueness benefit. A NanoID is roughly 40% shorter, generated with cryptographic randomness from the Web Crypto API, and customizable to fit almost any use case β from short session tokens to human-readable codes that skip ambiguous characters like 0 and O.
Our free NanoID Generator puts all of this in your browser. You can customize the ID length from 1 to 100 characters, choose between six preset alphabets, and generate up to 100 IDs in a single click β all without signing up and without any data ever leaving your device. Everything runs client-side, so your IDs are generated locally with the same cryptographic primitives the official nanoid package uses.
Why Use a NanoID Generator?
- Compact and tiny β A default NanoID is just 21 characters, compared to 36 for a UUID v4 with its four hyphens. Shorter IDs mean smaller URLs, leaner database rows, and more efficient storage in indexes and logs.
- URL-safe by design β The default alphabet (AβZ, aβz, 0β9, -, _) contains no characters that need escaping in URLs, file paths, or HTML attributes. You can paste a NanoID directly into a query string or use it as a filename without surprises.
- Fast to generate β NanoID is noticeably faster than UUID v4 because it skips hyphen formatting and uses a leaner algorithm. Generating thousands of IDs per second is trivial.
- Cryptographically random β IDs are produced with crypto.getRandomValues() (the Web Crypto API), not Math.random(). That makes them suitable for security-sensitive contexts like session tokens where predictability would be a bug.
- Fully customizable β Tweak the length for your collision tolerance and swap the alphabet to match your needs: numbers only, hex, alphanumeric, or a "no lookalikes" set that removes confusing characters for humans.
- No signup, no tracking β The tool runs entirely in your browser. There's no account to create, no API key to manage, and no data sent to a server.
Key Features
| Feature | Description |
|---|---|
| Default length | 21 characters (configurable from 1 to 100) |
| Bulk generation | 1 to 100 IDs in a single click |
| URL-safe alphabet | AβZ, aβz, 0β9, -, _ (default, via the official nanoid package) |
| Alphanumeric | AβZ, aβz, 0β9 |
| Numbers only | 0β9 |
| Hex (lowercase) | 0β9, aβf |
| Hex (uppercase) | 0β9, AβF |
| No lookalikes | Omits ambiguous characters like 0, O, 1, I, and l (uses customAlphabet) |
- Custom alphabets β The "No lookalikes" option is built on NanoID's customAlphabet helper, which lets you define exactly which characters can appear in an ID. That's invaluable when IDs will be read, typed, or dictated by humans.
- Bulk generation β Generating 100 IDs at once is ideal for seeding test databases, pre-allocating token pools, or populating fixtures. Each ID is independently random, so collisions are astronomically unlikely even at scale.
- Copy individual or copy-all β Copy a single ID with one click, or grab the whole batch at once for pasting into a script, migration file, or spreadsheet.
How to Use the NanoID Generator
- Choose the ID size β Use the size control to set the number of characters per ID, from 1 to 100. The default of 21 is a safe choice for almost all production use cases.
- Pick an alphabet β Select one of the six presets. URL-safe is the best default; switch to "No lookalikes" for human-facing codes, or "Numbers only" when you need purely numeric IDs.
- Set the count β Decide how many IDs you want to generate, from 1 up to 100. A live preview updates as you change this value.
- Click Generate β Your IDs appear instantly, generated locally in the browser with cryptographic randomness. No network request is made.
- Copy what you need β Use the copy button next to any ID for a single value, or the copy-all button to grab the entire batch.
Ready to try it? Head to the NanoID Generator and start producing IDs in seconds.
Understanding NanoID: Collision Math and Alphabet Design
NanoID's default length of 21 characters isn't arbitrary β it's the result of a careful trade-off between compactness and collision safety. The key is the size of the alphabet and the length of the string, which together determine how many possible IDs exist.
The default URL-safe alphabet has 64 characters. A 21-character ID therefore has 64Β²ΒΉ possible values β an astronomically large space. The probability that any two randomly generated IDs collide follows the classic birthday-bound calculation:
P(collision) β nΒ² / (2 Β· N^L)
where n is the number of IDs generated, N is the alphabet size, and L is the ID length. Plugging in the defaults (N = 64, L = 21), generating 1000 IDs per second gives you roughly 1 billion years before you'd expect a single collision. That's why 21 characters is more than enough for virtually any application β databases, session tokens, shareable links β without wasting space.
Shorter lengths are fine for lower-stakes use cases, but the collision probability rises quickly. For example, a 10-character NanoID still has a huge value space, but if you're generating millions of IDs, you'll want to verify the math for your specific scale using the formula above. The golden rule: choose the shortest length whose collision probability stays comfortably below your tolerance.
| Property | NanoID (default) | UUID v4 |
|---|---|---|
| Length | 21 characters | 36 characters (with hyphens) |
| Format | URL-safe string | Hex with - separators |
| Randomness source | crypto.getRandomValues() | crypto.getRandomValues() |
| URL-safe without escaping | Yes | No (hyphens are fine, but format is fixed) |
| Customizable alphabet | Yes | No |
| Typical use | URLs, DB keys, tokens | Legacy IDs, system identifiers |
Practical Use Cases
Database Primary Keys
NanoID shines as a primary or surrogate key. Its compact size keeps indexes small, and because it's URL-safe you can expose it directly in REST or GraphQL APIs without translation. Unlike sequential integers, NanoIDs don't leak information about record counts or creation order β useful when you don't want users guessing how many accounts or orders exist.
Session and Token IDs
Because NanoID uses crypto.getRandomValues(), it's a solid choice for session IDs, CSRF tokens, or short-lived API keys. A 21-character ID from the full URL-safe alphabet has enough entropy for most session lifetimes. For longer-lived or higher-value secrets, simply bump the length β 32 or 48 characters gives you a comfortable margin.
Shareable URLs and File Names
The default alphabet means a NanoID works unescaped in a URL path, a query parameter, or a filename on any modern filesystem. That makes it perfect for shareable links (think https://example.com/d/V1StGXR8_Z5jdHi6B-myT), uploaded-file names, and cache keys β no percent-encoding required.
Per-Environment Prefixes
A common pattern is to prepend a short environment or resource tag, like usr_, prod_, or dev_, to a NanoID. This makes IDs self-describing at a glance and prevents accidentally mixing records between environments. While the generator produces raw IDs, you can easily wrap them with a prefix in your application code.
Best Practices
- Choose length for your scale β Use the collision formula to pick the shortest length that keeps P(collision) well below your tolerance. 21 characters is a great default; shorter is fine for ephemeral or low-volume IDs.
- Use the no-lookalikes alphabet when humans read IDs β Characters like 0/O and 1/l/I cause real confusion in support tickets, typed URLs, and printed codes. The "No lookalikes" preset removes them automatically.
- Never use Math.random() for IDs β Math.random() is not cryptographically secure and can produce predictable outputs. Always rely on crypto.getRandomValues() or the official nanoid package, which the generator does under the hood.
- Prefix IDs per environment β Adding dev_, staging_, or prod_ prefixes makes it obvious where an ID originated and protects you from cross-environment data leaks.
- Test for collisions at your expected volume β For high-throughput systems, run a batch-generation test and verify the collision rate matches the theoretical math before going to production.
- Store and log IDs consistently β Because NanoIDs are short and URL-safe, they're easy to grep in logs and compare across systems. Keep them unmodified in storage to preserve these benefits.
Start Generating NanoIDs
Whether you're building a database schema, minting session tokens, or just need a few unique strings for a prototype, the NanoID Generator has you covered β fast, private, and fully customizable. Open it, pick your alphabet and length, and generate your first batch in seconds.
Related Tools You Might Like
Happy generating!