Generate YouTube-like ids from numbers. Use Hashids when you do not want to expose your database ids to the user.
Install Hashids via:
(or just use the code at
Use in the browser (wherever ES5 is supported; 5KB):
Use in Node.js:
var Hashids = ;var hashids = ;console;
var hashids = ;var id = hashids; // o2fXhVvar numbers = hashids; // [1, 2, 3]
A few more ways to pass to
var hashids = ;console; // o2fXhVconsole; // o2fXhVconsole; // o2fXhVconsole; // o2fXhV
Make your ids unique:
Pass a project name to make your ids unique:
var hashids = 'My Project';console; // Z4UrtWvar hashids = 'My Other Project';console; // gPUasb
Use padding to make your ids longer:
Note that ids are only padded to fit at least a certain length. It doesn't mean that your ids will be exactly that length.
var hashids = ; // no paddingconsole; // jRvar hashids = '' 10; // pad to length 10console; // VolejRejNm
Pass a custom alphabet:
var hashids = '' 0 'abcdefghijklmnopqrstuvwxyz'; // all lowercaseconsole; // mdfphx
Encode hex instead of numbers:
Useful if you want to encode Mongo's ObjectIds. Note that there is no limit on how large of a hex number you can pass (it does not have to be Mongo's ObjectId).
var hashids = ;var id = hashids; // y42LW46J9luq3Xq9XMlyvar hex = hashids; // 507f1f77bcf86cd799439011
When decoding, output is always an array of numbers (even if you encode only one number):
var hashids = ;var id = hashids;console; // 
Encoding negative numbers is not supported.
If you pass bogus input to
encode(), an empty string will be returned:
var hashids = ;var id = hashids;console; // true
Do not use this library as a security tool and do not encode sensitive data. This is not an encryption library.
The primary purpose of Hashids is to obfuscate ids. It's not meant or tested to be used as a security or compression tool. Having said that, this algorithm does try to make these ids random and unpredictable:
No repeating patterns showing there are 3 identical numbers in the id:
var hashids = ;console; // A6t1tQ
Same with incremented numbers:
var hashids = ;console; // wpfLh9iwsqt0uyCEFjHMconsole; // jRconsole; // k5console; // l5console; // mOconsole; // nR
This code was written with the intent of placing created ids in visible places, like the URL. Therefore, the algorithm tries to avoid generating most common English curse words by generating ids that never have the following letters next to each other:
c, f, h, i, s, t, u
MIT License. See the
LICENSE file. You can use Hashids in open source projects and commercial products. Don't break the Internet. Kthxbye.