Sqids Python
Sqids (pronounced "squids") is a small, active, and community-maintained Python library that generates short, unique, and URL-safe IDs from non-negative numbers. It's commonly used for link shortening, generating IDs for public URLs or internal systems, and decoding for quicker database lookups. The current version is 0.5.2, and it follows a regular release cadence with ongoing development across various language ports.
Common errors
-
TypeError: 'list' object cannot be interpreted as an integer
cause The `encode` method expects a sequence of non-negative integers (e.g., `[1, 2, 3]`), but a single integer was passed without being wrapped in a list or tuple. This error is not specific to Sqids but a common Python mistake.fixEnsure that the numbers passed to `sqids.encode()` are always within a list or tuple, even if it's a single number. For example, `sqids.encode([123])` instead of `sqids.encode(123)`. -
ValueError: alphabet cannot contain multibyte characters
cause Attempting to initialize `Sqids` with a custom alphabet containing characters that are not single-byte (e.g., emojis or certain Unicode characters). This restriction was introduced in v0.3.0.fixWhen defining a custom `alphabet`, ensure all characters are single-byte ASCII compatible. Remove any emojis or multi-byte Unicode characters from the alphabet string.
Warnings
- gotcha Due to the algorithm's design, multiple distinct IDs might decode back into the same sequence of numbers. If canonical IDs are critical for your application, you must manually re-encode decoded numbers and verify that the newly generated ID matches the original.
- breaking Version 0.3.0 introduced significant breaking changes, primarily an algorithm fine-tuning that causes IDs generated from the same inputs to change. Additionally, the `min_length` upper limit increased to 255, `min_value()` and `max_value()` functions were removed, and the minimum alphabet length changed from 5 to 3. The alphabet can no longer contain multibyte characters.
- gotcha Sqids is not an encryption library and should not be used for sensitive data. Generated IDs can be easily decoded back into their original numbers, which could inadvertently reveal information such as user counts if used for user IDs.
- gotcha For optimal performance, especially with blocklist checks, it is highly recommended to instantiate the `Sqids` class once and reuse that instance throughout your application. Version 0.5.0 improved encoding speed by ~85% but requires more calculation during instantiation.
Install
-
pip install sqids
Imports
- Sqids
from sqids import Sqids
Quickstart
from sqids import Sqids
sqids = Sqids()
# Encode numbers into a Sqid
numbers_to_encode = [1, 2, 3]
id = sqids.encode(numbers_to_encode)
print(f"Encoded ID: {id}") # Example: 86Rf07
# Decode a Sqid back into numbers
decoded_numbers = sqids.decode(id)
print(f"Decoded numbers: {decoded_numbers}") # Example: [1, 2, 3]
# Enforce a minimum length for the ID
sqids_min_length = Sqids(min_length=10)
id_padded = sqids_min_length.encode([1, 2, 3])
print(f"Padded ID: {id_padded}") # Example: 86Rf07xd4z