A tiny (141 bytes), secure URL-friendly unique string ID generator

Downloads in past


2.1.0-esm.45 years ago5 years agoMinified + gzip package size for nanoid-esm in KB


Nano ID
alt="Nano ID logo by Anton Lovchikov" width="180" height="94">A tiny, secure, URL-friendly, unique string ID generator for JavaScript.
  • Small. 141 bytes (minified and gzipped). No dependencies.
Size Limit controls the size.
  • Safe. It uses cryptographically strong random APIs
and tests distribution of symbols.
  • Fast. It’s 16% faster than UUID.
  • Compact. It uses a larger alphabet than UUID (A-Za-z0-9_-).
So ID size was reduced from 36 to 21 symbols.
var nanoid = require('nanoid') = nanoid() //=> "V1StGXR8_Z5jdHi6B-myT"

The generator supports Node.js, React Native, and all browsers.
alt="Sponsored by Evil Martians" width="236" height="54">


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


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.


random % alphabet is a popular mistake to make when coding an ID generator. The spread 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 uniformitywidth="340" height="135">

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.
  1. Nano ID code is 3 times less than uuid/v4 package:
141 bytes instead of 435.


$ ./test/benchmark
nanoid                    693,132 ops/sec
nanoid/generate           624,291 ops/sec
uid.sync                  487,706 ops/sec
uuid/v4                   471,299 ops/sec
secure-random-string      448,386 ops/sec
shortid                    66,809 ops/sec

nanoid/async              105,024 ops/sec
nanoid/async/generate     106,682 ops/sec
secure-random-string       94,217 ops/sec
uid                        92,026 ops/sec

nanoid/non-secure       2,555,814 ops/sec
rndm                    2,413,565 ops/sec



The main module uses URL-friendly symbols (A-Za-z0-9_-) and returns an ID with 21 characters (to have a collision probability similar to UUID v4).
const nanoid = require('nanoid') = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"

Symbols -,.() are not encoded in the URL. If used at the end of a link they could be identified as a punctuation symbol.
If you want to reduce ID length (and increase collisions probability), you can pass the length as an argument.
nanoid(10) //=> "IRFa-VaY2b"

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


Do not use a nanoid for key prop. In React key should be consistence between renders. This is bad code:
<Item key={nanoid()} /> /* DON’T DO IT */

This is good code. Note, that we added "input" string in front of id, because Nano ID could be started from number. HTML ID can’t be started from the number.
id = 'input' + nanoid()

render () {
  return <>
    <label htmlFor={}>Label text</label>
    <input id={} type="text"/>

React Native

To generate secure random IDs in React Native, you must use a native random generator and asynchronous API:
const generateSecureRandom = require('react-native-securerandom').generateSecureRandom
const format = require('nanoid/async/format')
const url = require('nanoid/url')

async function createUser () { = await format(generateSecureRandom, url, 21);


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

Web Workers

Web Workers don’t 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 don’t need unpredictable IDs, but you need Web Workers support, you can use non‑secure ID generator.
const nanoid = require('nanoid/non-secure') = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqLJ"


To generate hardware random bytes, CPU will collect electromagnetic noise. During the collection, CPU doesn’t work.
If we will use asynchronous API for random generator, another code could be executed during the entropy collection.
const nanoid = require('nanoid/async')

async function createUser () { = await nanoid()

Unfortunately, you will not have any benefits in a browser, since Web Crypto API doesn’t have asynchronous API.

Custom Alphabet or Length

If you want to change the ID's alphabet or length you can use the low-level generate module.
const generate = require('nanoid/generate') = generate('1234567890abcdef', 10) //=> "4f90d13a42"

Check the safety of your custom alphabet and ID length in our ID collision probability calculator. You can find popular alphabets in nanoid-dictionary.
Alphabet must contain 256 symbols or less. Otherwise, the generator will not be secure.
Asynchronous and non-secure API is also available:
const generate = require('nanoid/async/generate')
async function createUser () { = await generate('1234567890abcdef', 10)

const generate = require('nanoid/non-secure/generate') = generate('1234567890abcdef', 10)

Custom Random Bytes Generator

You can replace the default safe random generator using the format module. For instance, to use a seed-based generator.
const format = require('nanoid/format')

function random (size) {
  const result = []
  for (let i = 0; i < size; i++) {
  return result

format(random, "abcdef", 10) //=> "fbaefaadeb"

random callback must accept the array size and return an array with random numbers.
If you want to use the same URL-friendly symbols with format, you can get the default alphabet from the url file.
const url = require('nanoid/url')
format(random, url, 10) //=> "93ce_Ltuub"

Asynchronous API is also available:
const format = require('nanoid/async/format')
const url = require('nanoid/url')

function random (size) {
  return new Promise(…)

async function createUser () { = await format(random, url, 10)


Other Programming Languages

Also, CLI tool is available to generate IDs from a command line.