NanoID Generator

1

Configure Settings

Set quantity, length, and optionally customize the alphabet

2

Generate

Click "Generate NanoIDs" to create compact unique identifiers

3

Copy or Download

Click any ID to copy, or download all as a text file

0
Generated
21
Characters
~1M years
Collision Risk
Click "Generate NanoIDs" to create compact unique identifiers...

Features

Generate up to 10,000 NanoIDs instantly
Customizable length (8-128 characters)
Custom alphabet support
60% smaller than UUID
URL-safe by default
Collision probability calculator
100% client-side processing
No data sent to servers
Download as text file
Works offline after loading

What is NanoID?

NanoID is a tiny, secure, URL-friendly unique string ID generator for JavaScript. Created as a modern alternative to UUID, NanoID is 60% smaller than UUID (21 characters vs 36), twice as fast, and uses a larger alphabet for better security. It's used by popular projects like Next.js, React, Vercel, and many others.

Unlike UUIDs which use hexadecimal encoding (0-9, a-f), NanoID uses a URL-safe alphabet (A-Za-z0-9_-) by default, making it perfect for use in URLs, file names, and anywhere you need a compact, unique identifier. The default length of 21 characters provides the same collision probability as UUID v4 (2^128) but in a much shorter string.

NanoID Structure

A NanoID is a random string generated using cryptographically secure random number generation. The default configuration uses:

Example NanoID: V1StGXR8_Z5jdHi6B-myT
Length: 21 characters
Alphabet: URL-safe (A-Za-z0-9_-)
Use case: Perfect for URLs, database IDs, file names

Why Use NanoID Instead of UUID?

NanoID offers several advantages over traditional UUIDs:

NanoID vs UUID Comparison

Feature NanoID UUID v4
Length 21 characters (default) 36 characters
Size Reduction 60% smaller Baseline
Alphabet Size 64 characters 16 characters
URL-safe Yes (by default) Yes
Customizable Yes (length & alphabet) No
Generation Speed 2x faster Baseline

How to Use NanoID Generator

Step 1: Configure Settings

Choose how many NanoIDs you need (1-10,000) and set the desired length (8-128 characters). The default length of 21 characters provides excellent collision resistance while keeping IDs compact. For most applications, the default settings are perfect.

Step 2: Customize Alphabet (Optional)

If you need a custom alphabet, enter it in the "Custom Alphabet" field. For example, use "0123456789" for numeric-only IDs, or "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for uppercase letters only. Leave empty to use the default URL-safe alphabet.

Step 3: Generate NanoIDs

Click "Generate NanoIDs" to create your unique identifiers. Generation happens instantly in your browser using cryptographically secure random number generation. The collision probability is displayed based on your chosen length.

Step 4: Copy or Download

Click any NanoID to copy it to your clipboard, or use the "Download" button to save all generated IDs as a text file. The file contains one NanoID per line for easy importing.

Common Use Cases for NanoID

1. URL Shorteners

NanoID is perfect for URL shorteners because it creates compact, URL-safe identifiers. A 10-character NanoID provides enough uniqueness for billions of URLs while keeping the shortened URL as short as possible. Services like Vercel and many URL shorteners use NanoID for this purpose.

2. Database Primary Keys

Use NanoID as primary keys in databases where you want shorter IDs than UUID but still need global uniqueness. The compact size reduces storage requirements and improves index performance. NanoID works great with MongoDB, PostgreSQL, and other databases.

3. File and Document Names

NanoID generates perfect file names because they're short, unique, and don't contain special characters that cause problems in file systems. Use NanoID to name uploaded files, generated reports, or temporary files without worrying about conflicts.

4. Session IDs and Tokens

Generate secure session IDs and authentication tokens with NanoID. The cryptographically secure random generation ensures tokens are unpredictable, and the compact size reduces payload sizes in cookies and headers.

5. React Component Keys

When you need unique keys for React components and don't have natural IDs, NanoID provides a perfect solution. It's fast enough to generate during render without performance impact, and the IDs are stable across re-renders if generated once.

NanoID Examples

Example 1: JavaScript/Node.js

import { nanoid } from 'nanoid';

// Generate a NanoID
const id = nanoid();
console.log(id); // V1StGXR8_Z5jdHi6B-myT

// Custom length
const shortId = nanoid(10);
console.log(shortId); // IRFa-VaY2b

// Custom alphabet
import { customAlphabet } from 'nanoid';
const nanoid = customAlphabet('0123456789', 10);
const numericId = nanoid(); // 4193608123

Example 2: React Component

import { nanoid } from 'nanoid';

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

Example 3: URL Shortener

import { customAlphabet } from 'nanoid';

// Use only URL-safe characters, 8 chars long
const nanoid = customAlphabet(
  'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
  8
);

const shortUrl = `https://short.link/${nanoid()}`;
console.log(shortUrl); // https://short.link/aB3dE5fG

Example 4: Database Schema

// MongoDB with Mongoose
const userSchema = new Schema({
  _id: {
    type: String,
    default: () => nanoid()
  },
  email: String,
  name: String
});

// PostgreSQL
CREATE TABLE users (
  id VARCHAR(21) PRIMARY KEY DEFAULT nanoid(),
  email VARCHAR(255) NOT NULL
);

Example 5: File Upload

import { nanoid } from 'nanoid';
import path from 'path';

function saveUploadedFile(file) {
  const ext = path.extname(file.originalname);
  const filename = `${nanoid()}${ext}`;
  const filepath = `./uploads/${filename}`;
  
  // Save file with unique name
  fs.writeFileSync(filepath, file.buffer);
  return filename;
}

Frequently Asked Questions

What does NanoID stand for?
NanoID doesn't stand for anything specific - "Nano" refers to its tiny size compared to UUID. It's a compact, secure unique ID generator that's 60% smaller than UUID.
Is NanoID secure enough for production use?
Yes, NanoID uses cryptographically secure random number generation (crypto.getRandomValues in browsers, crypto.randomBytes in Node.js). It's used by major companies like Vercel, Next.js, and many others in production.
What's the collision probability with default settings?
With the default 21-character length, NanoID has the same collision probability as UUID v4 (2^128). You'd need to generate 1 billion IDs per second for ~1 million years to have a 1% chance of collision.
Can I use NanoID in URLs?
Yes! NanoID is URL-safe by default. It uses only characters that don't need URL encoding (A-Za-z0-9_-), making it perfect for use in URLs, file names, and anywhere special characters cause problems.
How do I choose the right length?
The default 21 characters is suitable for most applications. Use shorter lengths (8-10) for URL shorteners or when you need very compact IDs. Use longer lengths (30+) for extremely high-volume applications or when you need extra security.
Can I use custom alphabets?
Yes! You can use any custom alphabet. Common use cases include numeric-only IDs (0-9), uppercase-only (A-Z), or removing similar-looking characters (removing 0, O, I, l) to avoid confusion.
Is NanoID faster than UUID?
Yes, NanoID is approximately 2x faster than UUID generation. This is because it uses a simpler algorithm and doesn't need to format the output with hyphens like UUID does.
Can I use NanoID in other programming languages?
Yes! NanoID has been ported to 20+ programming languages including Python, Go, Ruby, PHP, Java, C#, Rust, and many others. Check the official NanoID GitHub repository for implementations.
Should I use NanoID or ULID?
Use NanoID when you need compact, URL-safe IDs and don't need time-sorting. Use ULID when you need IDs that are naturally sortable by creation time. Both are excellent choices depending on your requirements.
Is NanoID generation truly random?
Yes, NanoID uses cryptographically secure random number generation. In browsers it uses crypto.getRandomValues(), and in Node.js it uses crypto.randomBytes(). This ensures IDs are unpredictable and secure.

Related Tools

Explore our other unique identifier generators:

💙

Support TechConverter

Get $200 free DigitalOcean credit or sponsor us on GitHub!