Hashids.js turns numbers into short, URL-friendly strings so database IDs are not exposed directly, with salts, padding, and custom alphabets.
Overview
Hashids.js is a small JavaScript library that generates YouTube-style identifiers from one or more non-negative numbers. The goal is visual obfuscation of primary keys and similar counters, not encryption or secrecy.
Install via npm for Node, import in ESM or CommonJS setups, or load a browser build as a global where ES modules are available. TypeScript projects can import the package or the dedicated CommonJS entry; BigInt encoding is supported when the runtime provides BigInt.
Encoding accepts integers, arrays of numbers, hex strings, or BigInt values. Optional salt and minimum length settings make IDs unique to a project or longer than the default short form. You can replace the default alphanumeric alphabet, including emoji alphabets since v2.0. Decoding always returns an array of numbers; negative inputs are not supported.
The algorithm avoids common English profanity pairings by default and can skip predictable patterns in sequential IDs. Documentation stresses that bogus encode input yields empty strings and that the library must not be used to protect sensitive data.
Key features
- Encode numbers to short strings
- Decode back to number arrays
- Salt and minimum-length padding
- Custom and emoji alphabets
- Hex string encoding mode
- BigInt support when available
- ESM, CommonJS, and browser builds
- Profanity-avoiding default alphabet rules
Use cases
- Hide sequential database IDs in URLs
- Encode multiple integers in one token
- Obfuscate counters in public links
- Encode hex values like ObjectId strings
- Generate non-repeating-looking public IDs