Nano ID generates short random string identifiers for JavaScript apps. It aims for small bundle size, hardware-backed randomness, and URL-safe alphabets.
Overview
Nano ID produces unique string IDs suited to URLs, database keys, and client-side tokens. Default output uses 21 characters from an A–Z, a–z, 0–9, and hyphen alphabet, trading shorter strings against collision odds comparable to UUID v4.
The implementation favors a minimal dependency-free footprint and reports faster generation than native crypto.randomUUID in benchmarks cited by the project. Secure mode relies on Node’s crypto module or the browser Web Crypto API rather than Math.random, with algorithms tested for uniform symbol distribution.
Developers can fix custom alphabets and lengths through customAlphabet, plug alternate byte generators with customRandom, or opt into a non-secure path when hardware RNG is unavailable. TypeScript typings can mark generated strings as opaque branded types.
Beyond import in application code, npx nanoid offers terminal generation with size and alphabet flags. Official ports exist for many other languages so servers and clients can share the same ID format.
Key features
- URL-friendly default alphabet
- Hardware-backed secure randomness
- Custom alphabet and length
- Pluggable random byte sources
- Non-secure fallback generator
- npx CLI ID generation
- TypeScript opaque string casts
- Ports to many languages
Use cases
- Database primary keys
- Client-side correlation IDs
- Short public resource slugs
- Cross-stack consistent ID format