Last updated
Generating Default NanoIDs
The default configuration produces 21-character URL-safe IDs:
V1StGXR8_Z5jdHi6B-myT
DuSx8s_4XcYmP2kLqN7aW
Kj3mR9vT_nBpQeL5wXcYs
These use the alphabet A-Za-z0-9_- (64 characters). At 21 characters, the total possible IDs is 64^21 ≈ 2.8 × 10^37 — effectively impossible to exhaust or collide.
NanoID vs UUID Comparison
Same uniqueness, shorter string:
UUID v4: 550e8400-e29b-41d4-a716-446655440000 (36 chars)
NanoID: V1StGXR8_Z5jdHi6B-myT (21 chars)
NanoID is 36% shorter and URL-safe without encoding.
UUID requires encoding the hyphens in some contexts.
NanoID uses 126 bits of randomness vs UUID v4's 122 bits — slightly more collision-resistant in a shorter string.
Using NanoID in Node.js
// Install
npm install nanoid
// ESM (Node.js 14+)
import { nanoid } from 'nanoid';
const id = nanoid(); // 'V1StGXR8_Z5jdHi6B-myT'
const shortId = nanoid(10); // 'IRFa-VaY2b'
const longId = nanoid(32); // longer for extra collision resistance
// CommonJS
const { nanoid } = require('nanoid');
Custom Length IDs
Shorter IDs for URLs, longer for high-security tokens:
// 10-character ID for short URLs
nanoid(10) → 'IRFa-VaY2b'
// 16-character ID for session tokens (still very safe)
nanoid(16) → 'Uakgb_J5m9g-0JDe'
// 32-character ID for API keys (maximum security)
nanoid(32) → 'V1StGXR8_Z5jdHi6B-myTDuSx8s_4XcY'
Collision Probability Calculator
How many IDs can you generate before a 1% collision probability?
Length Alphabet Possible IDs Safe to generate
10 64 1.15 × 10^18 ~34 million
16 64 7.21 × 10^28 ~2.7 billion
21 64 2.82 × 10^37 ~1.7 × 10^18
32 64 1.16 × 10^57 ~3.4 × 10^27
For most applications generating thousands or millions of IDs, even a 10-character NanoID is safe. Use 21+ characters for systems that might generate billions of IDs.
Custom Alphabet — Lowercase Only
For case-insensitive systems (URLs, filenames on case-insensitive filesystems):
import { customAlphabet } from 'nanoid';
const nanoidLower = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 21);
nanoidLower() → 'k7m2p9xqr4n8vb3wj5yt6'
The alphabet has 36 characters. To maintain the same collision resistance as the default 21-char NanoID, you'd need about 25 characters with this smaller alphabet.
Custom Alphabet — Digits Only
For numeric IDs compatible with legacy systems:
import { customAlphabet } from 'nanoid';
const numericId = customAlphabet('0123456789', 12);
numericId() → '847392015648'
With only 10 possible characters, a 12-digit ID has 10^12 = 1 trillion possible values. For higher collision resistance, increase the length to 16 or 20 digits.
Human-Readable Alphabet (No Ambiguous Characters)
Exclude visually similar characters (0/O, 1/l/I) for IDs that humans read and type:
import { customAlphabet } from 'nanoid';
// Excludes: 0, O, 1, l, I
const readableId = customAlphabet(
'abcdefghjkmnpqrstuvwxyz23456789ABCDEFGHJKMNPQRSTUVWXYZ',
12
);
readableId() → 'Kj3mR9vTnBpQ'
This is useful for invite codes, voucher codes, and any ID that users need to read and type manually.
Using NanoID in React for List Keys
import { nanoid } from 'nanoid';
// Generate stable IDs when creating items (not during render)
const createTodo = (text) => ({
id: nanoid(),
text,
completed: false
});
function TodoList({ todos }) {
return (
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.text}</li>
))}
</ul>
);
}
Never call nanoid() directly in the render function — generate IDs when creating data, not when rendering.
Using NanoID for API Keys
import { nanoid } from 'nanoid';
// Generate a prefixed API key
function generateApiKey(prefix = 'sk') {
return `${prefix}_${nanoid(32)}`;
}
generateApiKey() → 'sk_V1StGXR8_Z5jdHi6B-myTDuSx8s_4XcY'
generateApiKey('pk') → 'pk_Kj3mR9vT_nBpQeL5wXcYsDuSx8s_4XcY'
The prefix makes it easy to identify the key type and revoke specific key categories. The 32-character NanoID portion provides 192 bits of randomness — suitable for security-sensitive tokens.
Browser Usage
<!-- Via CDN -->
<script type="module">
import { nanoid } from 'https://cdn.jsdelivr.net/npm/nanoid/nanoid.js';
const id = nanoid();
console.log(id); // 'V1StGXR8_Z5jdHi6B-myT'
</script>
NanoID uses crypto.getRandomValues() in the browser — the same cryptographically secure API used for security-sensitive operations. It is safe for generating session tokens and other security-critical identifiers in browser applications.