Nano ID: Secure URL-friendly unique string ID generator
Compact 21-character cryptographic IDs with URL-safe encoding.
Learn more about Nano ID
Nano ID is a JavaScript library that generates unique string identifiers using cryptographic random number generation. It uses the Web Crypto API in browsers and the crypto module in Node.js to ensure unpredictability, with a custom algorithm that maintains uniform distribution across its alphabet. The library produces 21-character IDs with 126 random bits, comparable to UUID v4's 122 bits but in a more compact format. It is commonly used for database record IDs, session tokens, and other applications requiring collision-resistant unique identifiers.
Compact size
The library is 118 bytes when minified and brotli-compressed with no external dependencies. This is approximately 3 times smaller than the uuid/v4 package.
Larger alphabet encoding
Uses 64 characters (A-Za-z0-9_-) instead of UUID's hexadecimal format, reducing ID length from 36 to 21 characters while maintaining similar collision probability.
Uniform distribution algorithm
Implements a custom algorithm to ensure even distribution of characters across generated IDs, avoiding the bias that occurs with naive modulo-based approaches.
import express from 'express';
import { nanoid } from 'nanoid';
import { customAlphabet } from 'nanoid';
const app = express();
const createOrderId = customAlphabet('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', 8);
app.use(express.json());
app.post('/api/users', async (req, res) => {
const user = {
id: nanoid(),
email: req.body.email,
username: req.body.username,
createdAt: new Date().toISOString()
};
await db.users.create(user);
res.json({ success: true, userId: user.id });
});
app.post('/api/orders', async (req, res) => {
const order = {
id: createOrderId(),
userId: req.body.userId,
items: req.body.items,
total: req.body.total,
status: 'pending'
};
await db.orders.create(order);
res.json({ orderId: order.id, status: order.status });
});Fixed infinite loop on 0 size for customAlphabet.
- –Fixed infinite loop on 0 size for customAlphabet.
Fixed latest version on npm after 3.x release.
- –Fixed latest version on npm after 3.x release.
Fixed React Native support.
- –Fixed React Native support.
Top in Developer Tools
Related Repositories
Discover similar tools and frameworks used by developers
Video Speed Controller
Chrome extension adding speed controls and keyboard shortcuts to HTML5 videos on any website.
VS Code LeetCode
VS Code extension for solving LeetCode problems directly in the editor with testing and submission.
Oh My Zsh
Community-driven zsh configuration framework with modular plugins.
ExcelJS
Parse, modify, and generate XLSX files in Node.js.
HTTPX
Fast HTTP probing with response metadata extraction.