uniku
Reference

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() validates the default alphabet, not values generated with a custom alphabet.

Generated API reference

Public methods

Read source

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(): string

Generate a Nanoid with the default alphabet and a custom length.

nanoid(size: number): string

Generate a Nanoid with a custom alphabet, length, or deterministic random bytes.

nanoid(options: NanoidOptions): string

NanoidOptions

random?

Random bytes for deterministic output (testing). For power-of-2 alphabets (2, 4, 8, 16, 32, 64, 128, 256): exactly `size` bytes needed. For other alphabets: ~size * 2 bytes needed (rejection sampling).

Uint8Array
alphabet?

Custom alphabet to use. Default: URL-safe A-Za-z0-9_- Must be 2-256 printable ASCII characters (32-126) with no duplicates.

string
size?

Length of generated ID. Default: 21. Maximum: 2048.

number
Examplenanoid()// => 'tCDaBJOIwLpjLMxN_DYdU'
isValid

Validate a nanoid string against the default URL-safe alphabet. Note: Does not validate IDs generated with custom alphabets.

isValid(id: unknown): id is string
Examplenanoid.isValid('tCDaBJOIwLpjLMxN_DYdU')// => true