Hashids

1.3.1 · maintenance · verified Fri Apr 10

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.

Warnings

Install

Imports

Quickstart

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.

from hashids import Hashids

# Instantiate with optional salt and minimum hash length for better obfuscation
# Using a salt makes your hashes unique to your application.
# min_length ensures all generated hashes are at least that long.
# For production, consider getting the salt from environment variables.
hashids = Hashids(salt="my-super-secret-salt-from-env", min_length=8)

# Encode one or more integers into a hashid string
id_to_encode = 12345
encoded_id = hashids.encode(id_to_encode)
print(f"Encoded {id_to_encode}: {encoded_id}")

multiple_ids_to_encode = (1, 2, 3)
encoded_multiple = hashids.encode(*multiple_ids_to_encode)
print(f"Encoded {multiple_ids_to_encode}: {encoded_multiple}")

# Decode a hashid string back to its original integers (as a tuple)
decoded_ints = hashids.decode(encoded_id)
print(f"Decoded '{encoded_id}': {decoded_ints}")

decoded_multiple = hashids.decode(encoded_multiple)
print(f"Decoded '{encoded_multiple}': {decoded_multiple}")

view raw JSON →