Library / SDK
skorokithakis/shortuuid avatar
skorokithakis/shortuuid

shortuuid re-encodes a Python UUID into 22 characters from a 57-symbol alphabet

A generator library for concise, unambiguous and URL-safe UUIDs.

2,197 stars121 forksPythonBSD-3-Clause

At a glance

What is it?
skorokithakis/shortuuid wraps Python's uuid module and writes the result in base57, dropping look-alike characters. The catch is what happens when you truncate, change the alphabet, or decode IDs stored before 1.0.0.
Who is it for?
Use shortuuid when users see or type your IDs and you want a 22-character string without l, 1, I, O or 0, on Python 3.6 or later. Skip it if you need IDs that are sortable by time or already stored in another short-ID scheme, because the library only re-encodes random or name-based UUIDs.
Can I use it commercially?
Yes. BSD-3-Clause is a permissive licence: you can use, modify and sell software built on it, as long as you keep its copyright and licence notices.
Is it still maintained?
Yes. The repository last received commits 29 days ago.
What is it written in?
Mainly Python, according to GitHub's language statistics.

Answers come from the project's GitHub data, last synced on November 23, 2026, and from our analysis. They are not legal advice.

From daily snapshots

Star history

  • skorokithakis/shortuuid2,197 ★ · +0
Star history
Repository2026-10-082026-11-23
skorokithakis/shortuuid2.2k2.2k

0 Stars in the last 46 days, measured from the daily GitHub snapshot. Compare with other projects

Editorial analysis

Base57 re-encoding of a stdlib UUID, nothing more

shortuuid generates a UUID with Python's built-in `uuid` module and then translates it into base57. The alphabet is lowercase and uppercase letters plus digits, with similar-looking characters such as l, 1, I, O and 0 removed. The result is a 22-character string that is safe in URLs and hard to misread aloud.

To see the exact alphabet, call `get_alphabet()`:

python
>>> shortuuid.get_alphabet()
'23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

The default output matches the regex `[2-9A-HJ-NP-Za-km-z]{22}`, which makes validation a one-line check. The pitch is non-sequential IDs in places where users will see them, so the library stays small: it adds no storage layer and no new randomness source of its own beyond what `uuid` provides.

Random IDs, name-based IDs and a secure random string

A plain call returns a random short ID:

python
>>> import shortuuid
>>> shortuuid.uuid()
'vytxeTZskVKR7C7WgdSP3d'

Pass `name` and you get a version 5 UUID, with the name used as a namespace (`uuid.NAMESPACE_DNS` or `uuid.NAMESPACE_URL`), so the same input gives the same output:

python
>>> shortuuid.uuid(name="example.com")
'exu3DTbj2ncsn9tLdLWspw'

The README's URL example prints the call text itself where an ID should be, so copy only the DNS form and test the URL form yourself. For a cryptographically secure random string, `shortuuid.ShortUUID().random(length=22)` uses `os.urandom()` internally. That is a separate path from `uuid()`, and you choose the length.

A custom alphabet is sorted and deduplicated unless you stop it

`set_alphabet()` swaps in your own symbols, but the library sorts them and removes duplicates first, so the result is consistent across machines. Passing `"aaaaabcdefgh1230123"` leaves the alphabet as `'0123abcdefgh'`.

If another ShortUUID implementation expects the order you wrote, pass `dont_sort_alphabet=True`:

python
>>> shortuuid.set_alphabet("aaaaabcdefgh1230123", dont_sort_alphabet=True)
>>> shortuuid.get_alphabet()
'abcdefgh1230'

The flag exists for compatibility with other implementations, and the README's sample shows the unsorted order surviving after duplicates are dropped. A smaller alphabet also lengthens the output. The README's eight-symbol example prints a 42-character string, so a tiny alphabet works against the goal of concise IDs.

Truncation shortens the ID and gives up uniqueness

If 22 characters is too many, the README's advice is to cut the string down. The IDs stop being universally unique, but the probability of a collision stays very low according to the README, which gives no figure for it.

Decoding a truncated ID still returns a valid UUID. The README cuts a 22-character string to seven characters, decodes it, and gets `UUID('00000000-0000-0000-0000-009a5b27f8b9')`. The leading zeros show that the short string was read as a small number, and `shortuuid.decode(shortuuid.encode(h)) == h` is True for that value.

Two consequences follow. A truncated ID cannot be turned back into the original UUID. And a unique constraint in your database becomes the real guard against collisions, because the library will not check one for you.

encode and decode convert UUIDs you already have

For existing UUIDs, `encode()` and `decode()` round-trip between the standard object and the short string:

python
>>> import uuid
>>> u = uuid.uuid4()
>>> s = shortuuid.encode(u)
>>> shortuuid.decode(s) == u
True

This is the route when you keep standard UUID columns in the database and only want short strings in URLs. Both directions depend on the alphabet in force, so an alphabet changed after IDs were issued breaks decoding of the old ones.

For per-thread alphabets, use the `ShortUUID` class: `shortuuid.ShortUUID(alphabet="01345678")` gives an instance with its own alphabet, and `su.set_alphabet()` changes only that instance. The module-level `set_alphabet()` is the global setting.

IDs stored before 1.0.0 were written reversed

Versions of ShortUUID before 1.0.0 generated UUIDs with the most significant bit last, meaning reversed. That was fixed in 1.0.0, released 2020-03-06, but strings stored under the old scheme need `legacy=True` when decoded.

The README warns that the option will go away. Its migration line is:

python
>>> new_uuid_str = encode(decode(old_uuid_str, legacy=True))

Run that over stored values once and you no longer depend on the flag. Skip it, and a future release can silently turn your old IDs into the wrong UUIDs. The release list shows v1.0.0, v0.5.0 and v0.4.3 as its most recent tags, while pyproject.toml declares version 1.0.13, so check the installed version rather than the tag list.

A Django field and a terminal command ship in the package

From the shell, a bare `shortuuid` command prints one ID. In `pyproject.toml` it is registered as a script pointing at `shortuuid.cli:cli`.

The Django side is `ShortUUIDField` from `shortuuid.django_fields`. It behaves like `CharField` with `length` and `alphabet` arguments added and `default` removed, and everything else, such as `index`, `help_text` and `max_length`, works as usual.

python
from shortuuid.django_fields import ShortUUIDField

class MyModel(models.Model):
    # A primary key ID of length 16 and a short alphabet.
    id = ShortUUIDField(
        length=16,
        max_length=40,
        prefix="id_",
        alphabet="abcdefg1234",
        dont_sort_alphabet=False,
        primary_key=True,
    )

Note the pair `length=16` and `max_length=40`: the `prefix` counts toward the stored string, so size `max_length` for prefix plus ID. The package targets Python 3.6 and later, with classifiers through 3.10, is BSD-3-Clause, and had a push on 2026-10-11.

Editorial conclusion

Use shortuuid when users see or type your IDs and you want a 22-character string without l, 1, I, O or 0, on Python 3.6 or later. Skip it if you need IDs that are sortable by time or already stored in another short-ID scheme, because the library only re-encodes random or name-based UUIDs. Before shipping, decide on your alphabet and truncation length once, since changing either later makes old strings decode to different UUIDs.

Frequently asked questions

How to generate a short UUID with shortuuid?

Import shortuuid and call shortuuid.uuid(). It returns a 22-character string such as 'vytxeTZskVKR7C7WgdSP3d'. Pass name="example.com" for a version 5 UUID.

What is a short UUID in shortuuid?

It is a standard Python UUID translated to base57 using letters and digits, with look-alike characters such as l, 1, I, O and 0 removed. The default result is 22 characters and URL-safe.

How does shortuuid differ from the standard uuid module?

shortuuid generates through Python's built-in uuid module, then encodes the value in base57. encode() and decode() convert between the two forms, and decode(encode(u)) returns the original UUID.

Official sources

  1. License: BSD-3-Clause
  2. Project website
  3. README
  4. Releases
  5. skorokithakis/shortuuid on GitHub
Add this badge to your README

If you maintain this project, the badge below links readers to this analysis and shows its maintenance status from the daily GitHub snapshot. Paste the markdown into your README; add ?metric=license or ?metric=stars to the image URL for a different field.

Add this badge to your README

markdown
[![Hysen Labs](https://hysenlabs.com/badge/skorokithakis-shortuuid.svg)](https://hysenlabs.com/projects/skorokithakis-shortuuid)

Star history chart for your README

markdown
[![Star history](https://hysenlabs.com/chart/skorokithakis-shortuuid.svg)](https://hysenlabs.com/projects/skorokithakis-shortuuid)

Was this useful?