Last Updated: 3/9/2026
Quick Start Guide
Get up and running with Nano ID in under 2 minutes.
Installation
Install via npm:
npm install nanoidBasic Usage
Generate a unique ID:
import { nanoid } from 'nanoid'
const id = nanoid()
console.log(id)
// => "V1StGXR8_Z5jdHi6B-myT"That’s it! You now have a secure, unique, URL-friendly 21-character ID.
Common Use Cases
Database Records
import { nanoid } from 'nanoid'
const user = {
id: nanoid(),
name: 'Alice',
email: 'alice@example.com'
}
db.users.insert(user)URL Slugs
import { nanoid } from 'nanoid'
const shortUrl = {
slug: nanoid(10), // Shorter ID for URLs
destination: 'https://example.com/very/long/url'
}
// Access at: https://short.link/IRFa-VaY2bSession Tokens
import { nanoid } from 'nanoid'
function createSession(userId) {
return {
sessionId: nanoid(),
userId,
createdAt: Date.now()
}
}File Names
import { nanoid } from 'nanoid'
import fs from 'fs'
const tempFileName = `upload-${nanoid()}.tmp`
fs.writeFileSync(tempFileName, data)Adjusting ID Length
The default 21-character ID has extremely low collision probability. For different needs:
import { nanoid } from 'nanoid'
// Standard (21 chars) - safest
const standardId = nanoid()
// Short (10 chars) - for URLs, less safe
const shortId = nanoid(10)
// Long (30 chars) - extra paranoid
const longId = nanoid(30)Collision Safety
⚠️ Important: Shorter IDs = higher collision risk.
Use the ID Collision Calculator to check safety for your use case.
Rule of thumb:
- 21 chars (default): ~1B chance of collision with 103 trillion IDs
- 10 chars: Safe for thousands of IDs
- 8 chars: Safe for hundreds of IDs
Custom Alphabets
For specific requirements (hex, numbers-only, etc.):
import { customAlphabet } from 'nanoid'
// Hex IDs
const nanoidHex = customAlphabet('0123456789abcdef', 16)
const hexId = nanoidHex()
// => "4f90d13a42a8c7e1"
// Numbers only
const nanoidNumeric = customAlphabet('0123456789', 12)
const numericId = nanoidNumeric()
// => "382947561029"
// Lowercase alphanumeric
const nanoidLower = customAlphabet('abcdefghijklmnopqrstuvwxyz0123456789', 16)
const lowerId = nanoidLower()
// => "4f90d13a42c7e1kl"See Custom Alphabet for more options and safety considerations.
Non-Secure IDs (Faster)
For non-critical use cases where security isn’t important:
import { nanoid } from 'nanoid/non-secure'
const id = nanoid()
// Faster, but not cryptographically secure⚠️ Only use for:
- Development/testing
- Non-sensitive temporary IDs
- Performance-critical scenarios where security doesn’t matter
Never use for:
- User authentication
- Session tokens
- Payment or financial records
- Any security-sensitive context
Framework Integration
React
import { nanoid } from 'nanoid'
import { useState } from 'react'
function TodoList() {
const [todos, setTodos] = useState([])
const addTodo = (text) => {
setTodos([...todos, { id: nanoid(), text }])
}
return (
<ul>
{todos.map(todo => (
<li key={todo.id}>{todo.text}</li>
))}
</ul>
)
}⚠️ Don’t generate IDs during render - see React Integration for proper patterns.
Node.js Express
import express from 'express'
import { nanoid } from 'nanoid'
const app = express()
app.post('/api/items', (req, res) => {
const item = {
id: nanoid(),
...req.body,
createdAt: Date.now()
}
db.items.insert(item)
res.json(item)
})Troubleshooting
”Cannot find module ‘nanoid’”
Make sure you’ve installed it:
npm install nanoidCommonJS require() not working
See Installation - CommonJS for version-specific solutions.
React Native: “crypto.getRandomValues is not a function”
React Native needs a polyfill - see React Native Setup.
Next Steps
Now that you’re generating IDs, explore:
- Core API - All parameters and options
- Custom Alphabet - Customize ID format
- Security Details - How Nano ID ensures uniqueness
- React Integration - Best practices for React apps