NanoID is a tiny, secure, URL-friendly unique string generator. It uses a 64-character URL-safe alphabet (A-Za-z0-9_-) by default and produces 21-character strings with equivalent collision resistance to UUIDv4 while saving 40% of character length.
Standard nanoId()
Generates a standard 21-character string using the native Web Cryptography API with uniform distribution:
js
import { nanoId } from '@readytools/id';
const id = nanoId();
// Returns: "V1StGXR8_Z5jdHi6B-myT"nanoId()
Generating...
customNanoId(alphabet, size)
The customNanoId function produces unique identifiers using a custom character dictionary and custom string length with zero modulo bias. Ideal for OTP codes, booking references, and custom tokens:
js
import { customNanoId } from '@readytools/id';
// 6-digit numeric SMS verification code
const otp = customNanoId('0123456789', 6);
// e.g. "491823"
// 8-character uppercase booking reference
const bookingCode = customNanoId('ABCDEFGHJKLMNPQRSTUVWXYZ23456789', 8);
// e.g. "K9M2P4X7"
// 16-character lowercase hex token
const hexToken = customNanoId('0123456789abcdef', 16);
// e.g. "a3f89012c45e67b9"customNanoId()
Generating...
Collision Probability Analysis
A standard 21-character NanoID with an alphabet of 64 characters provides $\approx 126$ bits of entropy. Generating 1 billion IDs per second for over 4,000 years would only yield a 1% probability of encountering a single collision.