Sqids (pronounced

Search code, repositories, users, issues, pull requests...

submited by
Style Pass
2023-09-18 20:30:09

Sqids (pronounced "squids") is a small library that lets you generate YouTube-like IDs from numbers. It encodes numbers like 127 into strings like yc3, which you can then decode back into the original numbers. Sqids is useful for when you want to hide numbers (like sequential numeric IDs) into random-looking strings to be used in URLs and elsewhere.

Note You can use any integral numeric type (e.g. long, byte, short, etc.) as the type argument. int is just the most common one, but if you need to encode/decode larger numbers, for example, you could use long/ulong instead.

If you're targeting an older framework than .NET 7, SqidsEncoder only supports int, and there is no generic type parameter you need to supply, so just:

You can easily customize the alphabet (the characters that Sqids uses to encode the numbers), the minimum length of the IDs (how long the IDs should be at minimum), and the blocklist (the words that should not appear in the IDs), by passing an instance of SqidsOptions to the constructor of SqidsEncoder.

Note It's recommended that you at least provide a shuffled alphabet when using Sqids — even if you want to use the same characters as those in the default alphabet — so that your IDs will be unique to you. You can use an online tool like this one to do that.

Leave a Comment