Last Updated: 3/12/2026
Custom Alphabet & Size
Nanoid allows you to customize both the character set (alphabet) and default ID size using customAlphabet.
Basic Custom Alphabet
Create a custom ID generator with your own alphabet and size:
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10)
const id = nanoid() //=> "4f90d13a42"This creates a function that generates 10-character IDs using only hexadecimal characters.
Override Size at Call Time
You can override the default size when calling the function:
const nanoid = customAlphabet('1234567890abcdef', 10)
const shortId = nanoid(5) //=> "f01a2"Non-Secure Custom Alphabet
For environments without hardware random generators, use the non-secure version:
import { customAlphabet } from 'nanoid/non-secure'
const nanoid = customAlphabet('1234567890abcdef', 10)
const id = nanoid()Alphabet Safety
Important: Alphabets must contain 256 symbols or less. Larger alphabets compromise the internal generator’s security guarantees.
Always verify your custom alphabet and ID size in the ID collision calculator to ensure adequate collision resistance.
Common Custom Alphabets
Numeric only:
const nanoid = customAlphabet('0123456789', 12)Lowercase alphanumeric:
const nanoid = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 16)No ambiguous characters (removes 0, O, I, l):
const nanoid = customAlphabet('123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz', 21)For more alphabet options, check out nanoid-dictionary.
Access Default Alphabet
If you want to use the default URL-safe alphabet with customRandom:
import { customRandom, urlAlphabet } from 'nanoid'
const nanoid = customRandom(urlAlphabet, 10, customRandomFunction)What’s Next
- Security Considerations: Learn about Nanoid’s security model
- CLI Reference: Generate custom IDs from the command line