ai /nanoid

A tiny (118 bytes), secure, URL-friendly, unique string ID generator for JavaScript

★ 27,130⑂ 977👁 157⑃ 316● 270▤ 37
JavaScriptMITCreated 2017-08-05zelark.github.io/nano-id-cc/
Download: 6.0.26.0.2 ↓6.0.22026-10-05
+0
Daily
+10
Weekly
+26
Monthly
27,130
stars
977
forks
157
watchers
316
prs
270
issues
37
releases

README

Nano ID



English | 日本語 | Русский | 简体中文 | Bahasa Indonesia | 한국어 | العربية

A tiny, secure, URL-friendly, unique string ID generator for JavaScript.

 “An amazing level of senseless perfectionism,
 which is simply impossible not to respect.”

- Small. 117 bytes (minified and brotlied). No dependencies.
  [Size Limit] controls the size.
- Fast. Faster than native crypto.randomUUID().
- Safe. It uses hardware random generator. Can be used in clusters.
- Short IDs. It uses a larger alphabet than UUID (A-Za-z0-9_-).
  So ID size was reduced from 36 to 21 symbols.
- Portable. Nano ID was ported
  to over 20 programming languages.

js
import { nanoid } from 'nanoid'
model.id = nanoid() //= "V1StGXR8_Z5jdHi6B-myT"


---

  Nano ID is built by Evil Martians, an American design and engineering consultancy for developer tools, AI, and cybersecurity startups.

---

[Size Limit]: https://github.com/ai/size-limit

 Table of Contents

- Table of Contents
- Comparison with UUID
- Benchmark
- Security
- Install
  - CDN
- API
  - Custom Alphabet or Size
  - Custom Random Bytes Generator
  - Non-Secure
- Usage
  - React
  - React Native
  - PouchDB and CouchDB
  - CLI
  - TypeScript
  - Other Programming Languages
- Tools

 Comparison with UUID

Nano ID is quite comparable to UUID v4 (random-based).
It has a similar number of random bits in the ID
(126 in Nano ID and 122 in UUID), so it has a similar collision probability:

 For there to be a one in a billion chance of duplication,
 103 trillion version 4 IDs must be generated.

There are two main differences between Nano ID and UUID v4:

1. Nano ID uses a bigger alphabet, so a similar number of random bits
   are packed in just 21 symbols instead of 36.
2. Nano ID is faster than crypto.randomUUID and uuid/v4.

 Benchmark

rust
$ node ./test/benchmark.js
nope-id                 50,866,431 ops/sec
nanoid                  25,580,859 ops/sec
customAlphabet           3,955,574 ops/sec
crypto.randomUUID        6,583,303 ops/sec
uuid v4                  5,575,055 ops/sec
@napi-rs/uuid            8,369,981 ops/sec
uid/secure               7,965,602 ops/sec
@lukeed/uuid             6,301,692 ops/sec
nanoid for browser         626,943 ops/sec
secure-random-string       509,515 ops/sec
uid-safe.sync              491,164 ops/sec

Non-secure:
uid                     34,793,413 ops/sec
nanoid/non-secure        2,717,308 ops/sec
rndm                     2,779,850 ops/sec


 Security

_See a good article about random generators theory:
[Secure random values (in Node.js)]_

- Unpredictability. Instead of using the unsafe Math.random(), Nano ID
  uses the crypto module in Node.js and the Web Crypto API in browsers.
  These modules use unpredictable hardware random generator.
- Uniformity. random % alphabet is a popular mistake to make when coding
  an ID generator. The distribution will not be even; there will be a lower
  chance for some symbols to appear compared to others. So, it will reduce
  the number of tries when brute-f

Discussions

Latest release

6.0.2 6.0.2 2026-10-05
- Reduced JS bundle size (by @bayandin).
- Fixed floats in size of customRandom (by @lost-signals).
- Fixed performance of customRandom on changes size (by @00200200).
- Fixed docs (by @Likio3000 & @lenamonj).

Snapshot: 2026-11-16T09:23:14Z · Data license Apache-2.0 · JSON