What is NanoID?

NanoID is a tiny, secure, URL-friendly unique string ID generator for JavaScript. It generates compact identifiers that are shorter than UUIDs while maintaining similar collision resistance.

Quick Facts

Full NameNano ID
Created2017 by Andrey Sitnik
SpecificationOfficial Specification

How It Works

NanoID generates 21-character IDs by default using a cryptographically secure random number generator. Unlike UUIDs, NanoID uses a larger alphabet (A-Za-z0-9_-) which makes IDs shorter while maintaining uniqueness. The default 21-character ID has similar collision probability to UUID v4. NanoID is popular in modern JavaScript applications for generating IDs for database records, URL slugs, and session tokens. It's smaller than UUID libraries and has no dependencies. Compared to UUID: NanoID is 40% faster to generate, 21 characters vs 36 (40% shorter), URL-safe by default, and uses a cryptographically secure random generator. For most applications requiring unique identifiers, NanoID offers a more compact and efficient alternative to UUID while maintaining collision resistance.

Key Characteristics

  • 21 characters by default (configurable)
  • URL-safe alphabet (A-Za-z0-9_-)
  • Cryptographically secure random generation
  • Smaller than UUID (21 vs 36 characters)
  • No dependencies, tiny size (~130 bytes)
  • Customizable alphabet and length

Common Use Cases

  1. Database primary keys
  2. URL-friendly slugs and short links
  3. Session and token identifiers
  4. File naming in uploads
  5. React component keys

Example

loading...
Loading code...

Frequently Asked Questions

What is the difference between NanoID and UUID?

NanoID generates shorter IDs (21 characters vs 36 for UUID) while maintaining similar collision resistance. NanoID uses a URL-safe alphabet (A-Za-z0-9_-) and is 40% faster to generate. UUID uses hexadecimal characters with dashes and follows a standardized format.

Is NanoID cryptographically secure?

Yes, NanoID uses a cryptographically secure random number generator (crypto.getRandomValues in browsers, crypto.randomBytes in Node.js) to generate IDs, making it suitable for security-sensitive applications like session tokens.

Can I customize the length and alphabet of NanoID?

Yes, NanoID supports customization. You can specify a custom length as an argument to nanoid(length), or use customAlphabet() to create a generator with a specific character set. For example, customAlphabet('0123456789', 8) creates numeric-only 8-character IDs.

What is the collision probability of NanoID?

With the default 21-character length and 64-character alphabet, NanoID has a collision probability similar to UUID v4. You would need to generate IDs at a rate of 1 billion per hour for about 149 billion years to have a 1% probability of at least one collision.

Can NanoID be used in databases as a primary key?

Yes, NanoID is commonly used as database primary keys. Its compact size saves storage space compared to UUIDs, and its URL-safe format makes it ideal for web applications. However, unlike auto-incrementing integers, random IDs may impact index performance in some databases.

Related Tools

Related Terms

Related Articles