Generate secure, URL-friendly unique string IDs using NanoID.
For decades, UUIDs (Universally Unique Identifiers) were the gold standard for generating IDs. However, standard UUIDs (like 123e4567-e89b-12d3-a456-426614174000) are exactly 36 characters long.
This is because they are strictly limited to a small hexadecimal alphabet (0-9 and a-f). This length makes database indexing slightly slower and produces long, unwieldy REST API endpoints.
NanoID solves the length problem by using a much larger alphabet. By utilizing 64 different characters (A-Z, a-z, 0-9, _, -) instead of just 16, NanoID can pack the exact same amount of randomness (entropy) into a much smaller footprint.
A standard 21-character NanoID has the exact same collision probability as a 36-character UUIDv4, making it 40% smaller to store in databases and transit over the network.
Why not just use standard Base64 encoding to get a large alphabet? Because standard Base64 uses the + and / characters.
If you put a / in a URL, the router thinks it's a new path segment. If you put a +, it often gets interpreted as a space. Standard Base64 strings must be URL-encoded (becoming %2B and %2F), which destroys their compactness. NanoID explicitly swaps these out for URL-safe _ and - characters.
How does NanoID achieve the same level of randomness (collision resistance) as a UUIDv4 while being 40% shorter?