Nanoid
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
Import
Function Signature
Basic Usage
Overloads
Default Generation
Custom Size
Length of the ID (0-2048 characters).
Nanoid string with the specified length using the default alphabet
Custom Options
Configuration options for nanoid generation
Nanoid string with the specified options
Constants
URL_ALPHABET
The default URL-safe alphabet (64 characters):- Contains 64 characters (power of 2, optimal for performance)
- Is URL-safe (no encoding needed)
- Is case-sensitive
- Does not contain special characters that need escaping
Static Methods
isValid()
Validate a nanoid string against the default URL-safe alphabet (type guard).Value to validate
Type predicate:
true if the value is a valid nanoid string with the default alphabet, false otherwiseisValid() only validates IDs against the default URL-safe alphabet (A-Za-z0-9_-). IDs generated with custom alphabets cannot be validated with this method.
Validation rules:
- Must be a string
- Length must be greater than 0
- Must match the pattern:
/^[A-Za-z0-9_-]+$/
Custom Alphabets
Hexadecimal
Numbers Only
Lowercase Letters Only
Custom Alphabet
Alphabet Constraints
Custom alphabets must meet these requirements:- Minimum length: 2 characters
- Maximum length: 256 characters
- Character range: Printable ASCII (codes 32-126)
- No duplicates: Each character must be unique
Size and Entropy
| Size | Bits (64-char alphabet) | Collision Probability |
|---|---|---|
| 8 | ~48 bits | 1 in 281 trillion |
| 10 | ~60 bits | 1 in 1.15 quintillion |
| 21 (default) | ~126 bits | 1 in 85 undecillion |
| 32 | ~192 bits | Astronomically low |
Performance
Ultra-Fast Path
When called without arguments,nanoid() uses an optimized code path:
- Simple pooled random bytes (npm nanoid style)
- No rejection sampling
- Direct character mapping
Fast Path
Power-of-2 alphabets (2, 4, 8, 16, 32, 64, 128, 256) use direct byte-to-character mapping:- No rejection sampling needed
- Each byte maps to exactly one character
- Optimal performance
Slow Path
Non-power-of-2 alphabets use rejection sampling:- Additional random bytes may be needed
- Some bytes are rejected to avoid modulo bias
- Slightly slower but still secure
Advanced Usage
Deterministic Generation (Testing)
Provide custom random bytes for reproducible IDs:- Power-of-2 alphabets: exactly
sizebytes - Other alphabets: approximately
size × 1.6bytes (due to rejection sampling)
Zero-Length IDs
Errors
Thrown when options are invalid:
- Code:
NANOID_SIZE_INVALID- Size must be a non-negative integer - Code:
NANOID_SIZE_TOO_LARGE- Size must not exceed 2048 - Code:
NANOID_ALPHABET_TOO_SHORT- Alphabet must contain at least 2 characters - Code:
NANOID_ALPHABET_TOO_LONG- Alphabet must not exceed 256 characters - Code:
NANOID_ALPHABET_INVALID_CHAR- Alphabet must contain only printable ASCII (32-126) - Code:
NANOID_ALPHABET_DUPLICATE- Duplicate character in alphabet - Code:
NANOID_RANDOM_BYTES_INSUFFICIENT- Not enough random bytes provided
Type Definitions
Binary Representation
Note: Nanoid does not providetoBytes()/fromBytes() because it is a string-native format with no canonical binary representation.
Nanoid is designed as a compact string identifier. While you could convert the characters to bytes, there’s no standardized binary format, and the conversion would lose semantic meaning.
Use Cases
Nanoid is ideal for:- URL shorteners: Compact IDs that don’t need escaping
- File names: Safe characters for all filesystems
- API keys: Short, secure tokens
- Invite codes: Human-readable (with custom alphabet)
- Session IDs: URL-safe, unpredictable
- Database primary keys: Consider UUID v7 or ULID for time-ordered indexes
- Sortable IDs: Nanoid is random, not time-ordered
Comparison with Other IDs
| Feature | Nanoid | UUID v4 | ULID |
|---|---|---|---|
| Default length | 21 chars | 36 chars | 26 chars |
| URL-safe | Yes | No (hyphens) | Yes |
| Time-ordered | No | No | Yes |
| Customizable alphabet | Yes | No | No |
| Binary format | No | Yes (16 bytes) | Yes (16 bytes) |
| Entropy (default) | 126 bits | 122 bits | 128 bits |
