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 Name | Nano ID |
|---|---|
| Created | 2017 by Andrey Sitnik |
| Specification | Official Specification |
How NanoID 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.
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
- Database primary keys
- URL-friendly slugs and short links
- Session and token identifiers
- File naming in uploads
- React component keys
Example
import { nanoid } from 'nanoid';
// Default 21-character ID
const id = nanoid();
// Output: V1StGXR8_Z5jdHi6B-myT
// Custom length
const shortId = nanoid(10);
// Output: IRFa-VaY2b
// Custom alphabet
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('0123456789', 8);
const numericId = nanoid();
// Output: 48293756