dylang /shortid

Short id generator. Url-friendly. Non-predictable. Cluster-compatible.

★ 5,708⑂ 250👁 55⑃ 38● 131▤ 0
JavaScriptNOASSERTIONCreated 2011-12-19www.npmjs.org/package/shortid
+0
日增长
5,708
stars
250
forks
55
watchers
38
prs
131
issues
0
releases

README

shortid   



 Amazingly short non-sequential url-friendly unique id generator.

 shortid is deprecated, because the architecture is unsafe. we instead recommend Nano ID, which has the advantage of also being significantly faster than shortid




ShortId creates amazingly short non-sequential url-friendly unique ids.  Perfect for url shorteners, MongoDB and Redis ids, and any other id users might see.

  By default 7-14 url-friendly characters: A-Z, a-z, 0-9, _-
  Supports cluster (automatically), custom seeds, custom alphabet.
  Can generate any number of ids without duplicates, even millions per day.
  Perfect for games, especially if you are concerned about cheating so you don't want an easily guessable id.
  Apps can be restarted any number of times without any chance of repeating an id.
  Popular replacement for Mongo ID/Mongoose ID.
  Works in Node, io.js, and web browsers.
  Includes Mocha tests.

ShortId does not generate cryptographically secure ids, so don't rely on it to make IDs which are impossible to guess.


 Usage

js
const shortid = require('shortid');

console.log(shortid.generate());
// PPBqWA9


Mongoose Unique Id
js
_id: {
  'type': String,
  'default': shortid.generate
},




 Browser Compatibility

The best way to use shortid in the browser is via browserify or webpack.

These tools will automatically only include the files necessary for browser compatibility.

All tests will run in the browser as well:

bash
 build the bundle, then open Mocha in a browser to see the tests run.
$ grunt build open




 Example

bash
~/projects/shortid ❯ node examples/examples.js
eWRhpRV
23TplPdS
46Juzcyx
dBvJIh-H
2WEKaVNO
7oet_d9Z
dogPzIz8
nYrnfYEv
a4vhAoFG
hwX6aOr7



 Real World Examples

shortId was created for Node Knockout 2011 winner for Most Fun Doodle Or Die.
Millions of doodles have been saved with shortId filenames. Every log message gets a shortId to make it easy
for us to look up later.

Here are some other projects that use shortId:

 bevy - A simple server to manage multiple Node services.
 capre - Cross-Server Data Replication.
 cordova-build - an alternative to phonegap build that runs on your servers/agents.
 couchdb-tools - A library of handy functions for use when working with CouchDB documents.
 CleverStack/clever-email - E-mail system for CleverStack.
 CloudTypes - JavaScript end2end implementation of the Cloud Types model for Eventual Consistency programming.
 dnode-tarantula - an asynchronous rpc and event system for node.js based on dnode-protocol and TCP sockets.
 mongoose-url-shortener - A simple URL Shortening library for NodeJS using Promises/A+ results.
 mozilla/smokejumper - The Smoke Jumper project is an effort to bring dead simple, secure, P2P file sharing to Firefox.
 shortness - Node based URL shortener that uses SQLite.
 file-db - Document database that uses directories and files to store its data, supporting nested key-value objects in named collections.
 resume-generator - Resume Generator.
 riffmint - Co

Top issues

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