Nano ID generator

Generate Nano IDs: short, URL-safe unique ids with the alphabet and size you choose, using the secure random generator of your browser.

loading Nano ID generator
About this tool

What is a Nano ID?

A Nano ID is a short, random, URL-safe identifier. The default is 21 characters long, for example a string like V1StGXR8_Z5jdHi6B-myT, and it comes from the open source nanoid library. People use it for database keys, short links, file names, session handles and anything else that needs a unique name without a central counter. It is smaller than a UUID (21 characters against 36), and you choose the alphabet and the length.

This page generates Nano IDs in your browser with the secure random generator. You pick an alphabet, a size and how many ids you want. Nothing is sent or stored. The code follows the algorithm of the library: it reads random bytes, keeps only the ones that fall inside the alphabet using a bit mask, and discards the others, so every symbol is equally likely. There is no bias toward the first symbols of the alphabet, which a plain remainder operation would cause. The results match the reference package when it is fed the same bytes.

The alphabets come as presets: the default of 64 URL-safe symbols (A-Z a-z 0-9 _ -), letters and digits, lowercase letters, lowercase and digits, digits, hexadecimal, and a set without look-alike characters (no 0, O, 1, l, I), which is good for codes people read aloud or type by hand. You can also type your own alphabet, with up to 255 symbols, and each symbol must appear once. Emoji and accented letters count as one symbol each.

The important question with random ids is collisions: what is the chance that two ids are equal? The page answers it with the birthday formula. It shows the bits of randomness per id, the number of possible ids, how many ids you can make before the risk of one repeat reaches 1% or one in a billion, and the risk after a year at the rate you enter. Note that a Nano ID is unique by probability, not by guarantee, and it is not a secret by itself. For secrets, such as tokens that give access, use a longer id and see the token generator.

How to generate Nano IDs

  1. Choose the Alphabet: from the presets. Use Custom to type your own in Your alphabet (each symbol once):. The counter shows how many symbols it has.
  2. Set the Size:, from 1 to 512. The default alphabet with 21 characters is a good default for general use.
  3. Set How many:, from 1 to 1000.
  4. Click Generate again to draw a new set. The IDs list shows the result, and Copy all copies one id per line.
  5. Open Collision risk. Check Randomness per id (bits): and Possible ids:, then enter Ids made per hour: to see in how many years the risk of one repeat reaches 1%.

Examples

The default alphabet and size

With the 64-symbol default alphabet and 21 characters, one id has 21 times 6 = 126 bits of randomness. That is a space of about 8.5 times 10 to the power of 37 ids. To have a 1% chance of a single repeat you would need more than 10 to the power of 18 ids.

Digits only, 12 characters

A numeric id of 12 digits has about 39.9 bits, so a space of 10 to the power of 12. The risk is very different: 1% is reached after only about 140 thousand ids. This is fine for a small table, not for a large system. Use more digits or a larger alphabet.

A check against the reference package

The tool's tests feed a fixed byte sequence to the algorithm and compare the result with the nanoid package. For one test sequence, the digit alphabet with size 12 gives 815223739596, the lowercase alphabet with size 10 gives pxvznrfjbv, and the alphabet ab with size 16 gives aabbbbaaaaaaaaab. The default alphabet with size 21 gives eCPLNg6eCPatFmX37j7j7. On the page the bytes come from the secure generator, so you will see different ids.

The birthday paradox

Among 23 people there is about a 50% chance that two share a birthday, though there are 365 days. The same square-root effect hits ids: the number of ids needed for a collision is about the square root of the size of the space, not the size itself.

Common mistakes

  • "The alphabet needs at least 2 different symbols." One symbol cannot make random ids. Add more.
  • "The alphabet can have at most 255 symbols." Shorten the alphabet.
  • "The symbol "x" appears twice. Each symbol of the alphabet must be different, or some would be more likely than others." Remove the repeated symbol.
  • "The size must be a whole number from 1 to 512." Use a whole number inside the range.
  • "Ask for 1 to 1000 ids at a time." Lower the count.
  • Using a short numeric id as a secret. An id of 8 digits has only 100 million values and is guessable. Do not use ids to protect access.
  • Assuming no repeats ever. Nano IDs are unique with a very high probability. If a repeat would be a disaster, put a unique constraint in the database and retry on conflict.

Frequently asked questions

How is a Nano ID different from a UUID?

A UUID v4 has 122 bits of randomness in 36 characters. A default Nano ID has 126 bits in 21 characters, and the alphabet is yours to change. A UUID is a standard recognised by every database and tool; if you need that, use the UUID generator.

Can Nano IDs be sorted by time?

No. They are fully random. If you need ids that sort by creation time, look at the ULID generator.

Is it safe to generate ids in the browser?

Yes. The page uses the secure random generator of your browser and sends nothing. The ids shown here are as good as ids made by the library, but make them in your own application, because an id shared in a web page is no longer yours alone.

How long should the id be?

It depends on how many ids you will make. Check the collision risk on the page with your rate. For 21 characters of the default alphabet the risk is negligible for any realistic use.

Generate Nano IDs in code

JavaScript

import { nanoid, customAlphabet } from 'nanoid';

nanoid();       // 21 characters, default alphabet
nanoid(10);     // 10 characters
const digits = customAlphabet('0123456789', 12);
digits();

Python

import secrets
import string

alphabet = string.ascii_letters + string.digits + "_-"
id_ = "".join(secrets.choice(alphabet) for _ in range(21))
Report a problem