Hashids is a small library that generates short, unique, and non-sequential IDs from numbers. It's often used for obfuscating database IDs in URLs, tracking, or invitation codes, providing a user-friendly and URL-safe representation of integers without exposing their underlying numeric values. The current version is 1.3.1. It is mostly in maintenance mode, as a new version called Sqids is the recommended successor.
pip install hashidsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize the `Hashids` class with a salt and minimum length, then encode single or multiple integers into a hashid string, and finally decode the hashid back to the original integers.
Always combine Hashids with proper authorization and never treat the generated IDs as secure tokens. For cryptographic needs, use libraries like `passlib` or `cryptography`.
Ensure your Python `hashids` version matches the expected JavaScript `hashids` version for cross-language compatibility. For new projects, stick to the latest versions.
Always initialize `Hashids` with a strong, unique, and secret salt string. Ideally, retrieve this salt from environment variables or a secure configuration system.
For new projects, consider using 'Sqids' (pip install sqids) instead of 'Hashids'. For existing projects, be aware that Hashids is largely in maintenance mode.
Always implement robust authorization checks on your backend endpoints, regardless of whether you're using plain IDs or Hashids. Hashids is a layer of obfuscation, not a security control.
Install the library using pip: ```bash pip install hashids ```
Ensure all arguments passed to `encode` are integers or floats: ```python from hashids import Hashids hashids = Hashids() encoded_id = hashids.encode(123, 456) # Floats are also accepted and truncated encoded_float_id = hashids.encode(123.5, 456.7) ```
Provide the `alphabet` as a string of unique characters: ```python from hashids import Hashids # Correct usage hashids = Hashids(alphabet="abcdefghijklmnopqrstuvwxyz1234567890") ```
Access the decoded integers from the tuple, either by unpacking it or by indexing: ```python from hashids import Hashids hashids = Hashids() encoded_id = hashids.encode(123) # Correct usage: unpack the tuple (for a single expected number) decoded_number, = hashids.decode(encoded_id) print(decoded_number + 1) # Example: 124 # Correct usage: access elements by index decoded_numbers = hashids.decode(encoded_id) print(decoded_numbers[0] + 1) # Example: 124 ```
No dependency data recorded yet.