Nanoid
Compact, URL-safe random strings with configurable length and alphabet.
Nanoid is the compact choice for short URLs, invite codes, and session tokens. Its default alphabet is URL-safe; pass a length or options only when the boundary needs a different shape.
import { nanoid } from 'uniku/nanoid'
const inviteCode = nanoid()
// 'tCDaBJOIwLpjLMxN_DYdU' (string)nanoid.isValid() only checks that a value is a non-empty run of the default URL-safe alphabet. A nanoid carries neither a length nor an alphabet, so it cannot verify either: any length is accepted, and an ID built from a custom alphabet passes whenever that alphabet is a subset of the default one — a hex-only alphabet, for example. Compare id.length yourself when the boundary needs one size.
Generated API reference
Public API
Generate a URL-friendly unique string ID. Nanoid is a tiny, secure, URL-friendly unique string ID generator. It uses a URL-safe alphabet (A-Za-z0-9_-) and generates 21-character IDs by default with 126 bits of entropy. Unlike UUID v7 or ULID, nanoid is NOT time-ordered. Use it for: - URL shorteners - Session tokens - Invite codes - Any case where you need short, random IDs
- nanoid
Generate a Nanoid with the default URL-safe alphabet and 21-character length.
nanoid(): stringGenerate a Nanoid with the default alphabet and a custom length.
nanoid(size: number): stringGenerate a Nanoid with a custom alphabet, length, or deterministic random bytes.
nanoid(options: NanoidOptions): stringExamplenanoid()// => 'tCDaBJOIwLpjLMxN_DYdU'- isValid
Validate a nanoid string against the default URL-safe alphabet. Nanoids carry no self-describing length or alphabet, so all this can check is that the value is a non-empty run of the default URL-safe characters. It cannot verify the generated length, and an ID built from a custom alphabet passes whenever that alphabet is a subset of the default one. Check `id.length` yourself when the boundary requires a specific size.
isValid(id: unknown): id is stringExamplenanoid.isValid('tCDaBJOIwLpjLMxN_DYdU')// => true