explaingit

dylang/shortid

5,717JavaScriptAudience · developerComplexity · 1/5Setup · easy

TLDR

JavaScript library that generates short random-looking IDs safe for use in URLs and database keys, now deprecated, with the author recommending Nano ID for any new projects.

Mindmap

mindmap
  root((repo))
    What it does
      Short ID generation
      URL-safe output
      Non-sequential IDs
    Features
      Custom alphabet
      Multi-process safe
      Browser and Node
    Limitations
      Deprecated library
      Not cryptographic
    Alternatives
      Nano ID
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Generate short URL-safe IDs for database records in a Node.js app instead of long UUIDs.

USE CASE 2

Create unique IDs in browser-side JavaScript without any server round trip.

USE CASE 3

Customize the character set used for IDs to meet specific URL or system encoding requirements.

Tech stack

JavaScriptNode.js

Getting it running

Difficulty · easy Time to first run · 5min

The author recommends Nano ID for new projects due to architectural safety concerns with shortid.

In plain English

Shortid is a JavaScript library that generates short, random-looking IDs suitable for use in URLs, database record keys, and anywhere else you need a compact unique identifier. A typical output looks something like PPBqWA9, using letters, numbers, hyphens, and underscores by default so the result is safe to paste directly into a web address without percent-encoding. The README opens with a deprecation notice. The author now recommends a different library called Nano ID instead, citing architectural safety concerns. Shortid is still widely used in older projects and the code remains available, but new projects are encouraged to look elsewhere. For existing users, the library works by calling shortid.generate() to get a new ID. The IDs are non-sequential, meaning they do not count up in order, so someone looking at a series of them cannot easily guess other valid IDs. The library also works across multiple server processes running in parallel, as long as each process is assigned a different worker number between 0 and 16. A seed value can also be set to make the internal random number generator harder to predict, though the library explicitly notes it does not produce cryptographically secure IDs and should not be used where guessability is a strong security requirement. The character set used for IDs can be customized. You provide any 64 unique characters and the library uses those instead of the default alphanumeric-plus-punctuation set. An isValid function checks whether a given string matches the format that shortid would produce. The library runs in Node.js and in browsers. It was originally built for a 2011 hackathon project and went on to be adopted by many open-source packages, several of which are listed in the README.

Copy-paste prompts

Prompt 1
I'm maintaining an older Node.js project that uses shortid. Show me how to generate an ID, validate an existing string with isValid(), and set a custom 64-character alphabet.
Prompt 2
My app runs on multiple Node.js worker processes and uses shortid. How do I assign worker IDs to prevent ID collisions across processes, and what are the limits on worker count?
Prompt 3
I want to migrate from shortid to Nano ID in my Express app. Show me the before-and-after code change and explain any behavioral differences I need to handle in existing stored data.
Open on GitHub → Explain another repo

← dylang on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.