Need help with nanoid?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

14.4K Stars 510 Forks MIT License 832 Commits 5 Opened issues


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

Services available


Need anything else?

Contributors list

Nano ID

Nano ID logo by Anton Lovchikov

English | Русский | 简体中文

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. 130 bytes (minified and gzipped). No dependencies. Size Limit controls the size.
  • Fast. It is 2 times faster than UUID.
  • Safe. It uses hardware random generator. Can be used in clusters.
  • Short IDs. It uses a larger alphabet than UUID (
    ). So ID size was reduced from 36 to 21 symbols.
  • Portable. Nano ID was ported to 19 programming languages.
import { nanoid } from 'nanoid' = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"

Supports modern browsers, IE with Babel, Node.js and React Native.

Sponsored by Evil Martians

Table of Contents

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 three 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 code is 4 times less than
    package: 130 bytes instead of 483.
  3. Because of memory allocation tricks, Nano ID is 2 times faster than UUID.


$ node ./test/benchmark.js
crypto.randomUUID         28,387,114 ops/sec
uid/secure                 8,633,795 ops/sec
@lukeed/uuid               6,888,704 ops/sec
nanoid                     6,166,399 ops/sec
customAlphabet             3,290,342 ops/sec
uuid v4                    1,662,373 ops/sec
secure-random-string         415,340 ops/sec
uid-safe.sync                400,875 ops/sec
cuid                         212,669 ops/sec
shortid                       53,453 ops/sec

Async: nanoid/async 102,823 ops/sec async customAlphabet 101,574 ops/sec async secure-random-string 96,540 ops/sec uid-safe 93,395 ops/sec

Non-secure: uid 70,055,975 ops/sec nanoid/non-secure 2,985,368 ops/sec rndm 2,800,961 ops/sec

Test configuration: ThinkPad X1 Carbon Gen 9, Fedora 34, Node.js 16.10.


See a good article about random generators theory: Secure random values (in Node.js)

  • Unpredictability. Instead of using the unsafe
    , Nano ID uses the
    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-forcing. Nano ID uses a better algorithm and is tested for uniformity.

Nano ID uniformity

  • Well-documented: all Nano ID hacks are documented. See comments in the source.
  • Vulnerabilities: to report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.


npm install --save nanoid

For quick hacks, you can load Nano ID from CDN. Though, it is not recommended to be used in production because of the lower loading performance.

import { nanoid } from ''

Nano ID provides ES modules. You do not need to do anything to use Nano ID as ESM in webpack, Rollup, Parcel, or Node.js.

import { nanoid } from 'nanoid'

In Node.js you can use CommonJS import:

const { nanoid } = require('nanoid')


Nano ID has 3 APIs: normal (blocking), asynchronous, and non-secure.

By default, Nano ID uses URL-friendly symbols (

) and returns an ID with 21 characters (to have a collision probability similar to UUID v4).


The safe and easiest way to use Nano ID.

In rare cases could block CPU from other work while noise collection for hardware random generator.

import { nanoid } from 'nanoid' = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"

If you want to reduce the ID size (and increase collisions probability), you can pass the size as an argument.

nanoid(10) //=> "IRFa-VaY2b"

Don’t forget to check the safety of your ID size in our ID collision probability calculator.

You can also use a custom alphabet or a random generator.


To generate hardware random bytes, CPU collects electromagnetic noise. For most cases, entropy will be already collected.

In the synchronous API during the noise collection, the CPU is busy and cannot do anything useful (for instance, process another HTTP request).

Using the asynchronous API of Nano ID, another code can run during the entropy collection.

import { nanoid } from 'nanoid/async'

async function createUser () { = await nanoid() }

Read more about entropy collection in


Unfortunately, you will lose Web Crypto API advantages in a browser if you use the asynchronous API. So, currently, in the browser, you are limited with either security or asynchronous behavior.


By default, Nano ID uses hardware random bytes generation for security and low collision probability. If you are not so concerned with security and more concerned with performance, you can use the faster non-secure generator.

import { nanoid } from 'nanoid/non-secure'
const id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"

Custom Alphabet or Size

allows you to create
with your own alphabet and ID size.
import { customAlphabet } from 'nanoid'
const nanoid = customAlphabet('1234567890abcdef', 10) = nanoid() //=> "4f90d13a42"

Check the safety of your custom alphabet and ID size in our ID collision probability calculator. For more alphabets, check out the options in 


Alphabet must contain 256 symbols or less. Otherwise, the security of the internal generator algorithm is not guaranteed.

Customizable asynchronous and non-secure APIs are also available:

import { customAlphabet } from 'nanoid/async'
const nanoid = customAlphabet('1234567890abcdef', 10)
async function createUser () { = await nanoid()
import { customAlphabet } from 'nanoid/non-secure'
const nanoid = customAlphabet('1234567890abcdef', 10) = nanoid()

Custom Random Bytes Generator

allows you to create a
and replace alphabet and the default random bytes generator.

In this example, a seed-based generator is used:

import { customRandom } from 'nanoid'

const rng = seedrandom(seed) const nanoid = customRandom('abcdef', 10, size => { return (new Uint8Array(size)).map(() => 256 * rng()) })

nanoid() //=> "fbaefaadeb"

callback must accept the array size and return an array with random numbers.

If you want to use the same URL-friendly symbols with

, you can get the default alphabet using the 
const { customRandom, urlAlphabet } = require('nanoid')
const nanoid = customRandom(urlAlphabet, 10, random)

Asynchronous and non-secure APIs are not available for




If you support IE, you need to transpile

by Babel and add

// polyfills.js
if (!window.crypto) {
  window.crypto = window.msCrypto
import './polyfills.js'
import { nanoid } from 'nanoid'


There’s no correct way to use Nano ID for React

prop since it should be consistent among renders.
function Todos({todos}) {
  return (
    { => (
  • /* DON’T DO IT */ {todo.text}
  • ))}
) }

You should rather try to reach for stable ID inside your list item.

const todoItems = =>
  • {todo.text}
  • )

    In case you don’t have stable IDs you'd rather use index as

    instead of
    const todoItems =, index) =>
  • /* Still not recommended but preferred over nanoid(). Only do this if items have no stable IDs. */ {text}
  • )


    If you want to use Nano ID in the

    prop, you must set some string prefix (it is invalid for the HTML ID to start with a number).

    React Native

    React Native does not have built-in random generator. The following polyfill works for plain React Native and Expo starting with

    1. Check
      docs and install it.
    2. Import it before Nano ID.
    import 'react-native-get-random-values'
    import { nanoid } from 'nanoid'


    For Rollup you will need

    to bundle browser version of this library and
    to replace

      plugins: [
          browser: true
          'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)

    PouchDB and CouchDB

    In PouchDB and CouchDB, IDs can’t start with an underscore

    . A prefix is required to prevent this issue, as Nano ID might use a
    at the start of the ID by default.

    Override the default ID with the following option:

      _id: 'id' + nanoid(),


    const mySchema = new Schema({
      _id: {
        type: String,
        default: () => nanoid()

    Web Workers

    Web Workers do not have access to a secure random generator.

    Security is important in IDs when IDs should be unpredictable. For instance, in "access by URL" link generation. If you do not need unpredictable IDs, but you need to use Web Workers, you can use the non‑secure ID generator.

    import { nanoid } from 'nanoid/non-secure'
    nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"

    Note: non-secure IDs are more prone to collision attacks.


    You can get unique ID in terminal by calling

    npx nanoid
    . You need only Node.js in the system. You do not need Nano ID to be installed anywhere.
    $ npx nanoid
    npx: installed 1 in 0.63s

    If you want to change alphabet or ID size, you should use


    Other Programming Languages

    Nano ID was ported to many languages. You can use these ports to have the same ID generator on the client and server side.

    For other environments, CLI is available to generate IDs from a command line.


    We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.