Developer Documentation
TypeScript
Updated September 19, 2026

Generate Nano IDs in TypeScript

Generate compact URL-friendly random identifiers with Nano ID libraries in TypeScript.

Nano ID is a compact random identifier format commonly used when shorter URL-friendly IDs are preferable to canonical UUID strings. Use a cryptographically secure implementation rather than Math.random-style generators.

Quick Start#

The following example uses the standard TypeScript API or a widely used package for this task.

Quick start#

TypeScript
1// npm install nanoid2import { nanoid } from "nanoid";34const id: string = nanoid();5console.log(id);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Custom generator#

TypeScript
1import { customAlphabet } from "nanoid";23const createOrderId: () => string = customAlphabet(4  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",5  6);78const orderId = createOrderId();

Important Notes#

Keep ID size and alphabet centralized so collision assumptions do not drift between services.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement NanoID in other languages: