Navigate:
Nano ID
~$NANOI0.1%

Nano ID: Secure URL-friendly unique string ID generator

Compact 21-character cryptographic IDs with URL-safe encoding.

LIVE RANKINGS • 10:20 AM • STEADY
OVERALL
#345
50
DEVELOPER TOOLS
#72
16
30 DAY RANKING TREND
ovr#345
·Devel#72
STARS
26.6K
FORKS
834
7D STARS
+22
7D FORKS
+1
See Repo:
Share:

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.

Nano ID

1

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.

2

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.

3

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 });
});

v5.1.6

Fixed infinite loop on 0 size for customAlphabet.

  • Fixed infinite loop on 0 size for customAlphabet.
v5.1.5

Fixed latest version on npm after 3.x release.

  • Fixed latest version on npm after 3.x release.
v3.3.11

Fixed React Native support.

  • Fixed React Native support.


[ EXPLORE MORE ]

Related Repositories

Discover similar tools and frameworks used by developers