nanoid

What is Nano ID

Nano ID is a tiny, secure, URL-friendly unique string ID generator. It produces 21 characters by default, drawn from a 64-symbol alphabet (A–Z, a–z, 0–9, _ and -), packing more randomness into fewer characters than a UUID while staying safe to use in URLs.

How it is generated

Every character comes from a cryptographically secure random source (the Web Crypto API in browsers, the crypto module in Node.js). There is no timestamp, counter, or machine fingerprint involved — the id is pure randomness. The algorithm also avoids the classic modulo bias, so every symbol in the alphabet has exactly the same probability of appearing.

Denser than a UUID

A UUID v4 uses 16 symbols (hex) and needs 36 characters to carry 122 bits of randomness. Nano ID uses 64 symbols, so each character carries 6 bits of entropy — 21 characters add up to 126 bits, slightly more than a UUID v4 in barely more than half the length.

Collision resistance

With 126 bits of randomness, collisions are a practical impossibility: generating a thousand ids per second, you would need roughly billions of years to reach a 1% probability of a single collision. No coordination between machines is required — every generator is independent.

Configurable length and alphabet

The size is adjustable: shorter ids save space at the cost of a higher collision risk, longer ids push the odds even lower. Beyond length, the customAlphabet API lets you restrict the symbol set — for example, lowercase-only ids for case-insensitive systems, or digits-only codes — with the same unbiased, secure generation.

Not sortable — by design

Since a Nano ID encodes no timestamp, it reveals nothing about when or where it was created — but it also cannot be sorted by creation time. If you need chronological ordering, add a separate timestamp column or reach for a time-ordered format like UUID v7.

One caveat: HTML ids

Unlike CUID2, a Nano ID can start with a digit, a hyphen, or an underscore, which makes it invalid as a bare HTML id or CSS selector. If you use it in the DOM, add a letter prefix (for example id-V1StGXR8_Z5jdHi6B-myT).

When to use it

A great default when you want short, URL-safe, unguessable ids with minimal footprint — share links, public tokens, file names, client-generated keys. If you need ids that double as sortable database keys, prefer UUID v7; if you need HTML-safe ids out of the box, CUID2 fits better.

© 2026 Unique ID Generator by meluiz