What is Sqids?

Sqids (pronounced "squids") is an open-source library that lets you generate short unique identifiers from numbers. These IDs are URL-safe, can encode several numbers, and do not contain common profanity words. Read more .

This is what they look like:

https://example.com/Lqj8a0

Quick encode & decode example:

val sqids = Sqids.default
val id = sqids.encodeUnsafeString(1, 2, 3) // "86Rf07"
val numbers = sqids.decode(id) // List(1, 2, 3)

If IDs are too short, you can pad them to a certain length:

Sqids
  .withMinLength(10)
  .foreach { sqids =>
    val id = sqids.encodeUnsafeString(1, 2, 3) // "86Rf07xd4z"
    val numbers = sqids.decode(id) // List(1, 2, 3)
  }

Create unique IDs by shuffling the alphabet:

Alphabet("k3G7QAe51FCsPW92uEOyq4Bg6Sp8YzVTmnU0liwDdHXLajZrfxNhobJIRcMvKt")
  .flatMap(Sqids.forAlphabet)
  .foreach { sqids =>
    val id = sqids.encodeUnsafeString(1, 2, 3) // "XRKUdQ"
    val numbers = sqids.decode(id) // List(1, 2, 3)
  }

Full documentation is at https://github.com/sqids/sqids-scala

If you're looking for the original Hashids Scala, you can find it here: https://github.com/newhoggy/hashids-scala

Use Cases

The main use of Sqids is purely visual. If you'd like to use IDs instead of numbers in your project, Sqids could be a good choice.

Good For

  • Encoding primary keys If you're using a relational database
  • Saving database lookups By encoding multiple objects
  • Temporary login tokens Profanity-free and URL-safe

Not Good For

  • Sensitive data This is not an encryption library
  • User IDs Can reveal user count if anyone finds out the encoding alphabet

Features

  • ๐Ÿ†” Generate short IDs from non-negative numbers
  • โœ… Easy encoding & decoding
  • ๐Ÿซฃ Auto-generated IDs do not contain common profanity
  • ๐ŸŽฒ Support for custom IDs through shuffled alphabet
  • ๐Ÿฅณ 45 integrations (84% using new design)
  • ๐Ÿ“Œ Every version produces the same IDs
  • ๐Ÿป Small library with a permissive license

Example / Playground