Nanoid Custom Alphabet Short Identifiers

Generate compact, URL-safe identifiers with nanoid, sized from collision probability math instead of guesswork, with a custom alphabet option.

JavascriptData Integrity

Code

snippet
import { customAlphabet, nanoid } from 'nanoid';

const id = nanoid(21);

const shortUrl = customAlphabet('23456789ABCDEFGHJKLMNPQRSTUVWXYZ', 10);
const token = customAlphabet('0123456789abcdef', 32);

function collisionChance(bitsOfEntropy, generatedCount) {
  const probability = (generatedCount ** 2) / (2 ** (bitsOfEntropy + 1));
  return probability;
}

console.log('default id:', id);
console.log('short url code:', shortUrl());
console.log('hex token:', token());
console.log('collision chance:', collisionChance(126, 1e6));