Short UUID vs UUID: length and formats
A short UUID is the same 128-bit number written with a bigger
alphabet, so a 36-character UUID becomes a 22-character short UUID
that converts back without loss. There is no single standard:
each library picks its own alphabet, and the same short string
decodes to a different UUID in each one. These are the four you are
most likely to meet, each shown for
f47ac10b-58cc-4372-a567-0e02b2c3d479:
| Format | Used by | Example |
|---|---|---|
| Flickr base58 | short-uuid on npm (default) |
wbZ3JKfFLhyPheLFGhcSwM |
| base57 | Python shortuuid (default) |
mWQEpU4e6KxNwyNiqnVzSw |
| base62 | Hand-rolled encoders, 0-9A-Za-z |
7RKE2sawAICsEsyZKHWW6r |
| base64url | Raw bytes, e.g. Buffer.toString("base64url") |
9HrBC1jMQ3KlZw4CssPUeQ |
To shorten a UUID for a URL, all four work: none of them use
characters that need escaping. base58 and base57 also skip
look-alike characters such as 0/O and
l/I, which helps when people read or type
the ID.
Which format is my short UUID?
Every Python shortuuid string is also valid Flickr
base58, because its alphabet is a subset. To tell them apart, the
converter checks which result is a real UUID: generated UUIDs carry
a version (4 for random, 7 for time-ordered) and variant bits, and
a decode in the wrong alphabet almost never does. The most likely
reading comes first, and the others are listed below it. If you know
your format, pick it above and the choice is remembered.
Generate and convert short UUIDs in code
JavaScript and Node, with the short-uuid package on npm:
import short from "short-uuid";
short.generate(); // a new short UUID, e.g. "jSsPzrF8BzqNbRk8QGaaJQ"
const translator = short(); // Flickr base58
translator.fromUUID("f47ac10b-58cc-4372-a567-0e02b2c3d479"); // "wbZ3JKfFLhyPheLFGhcSwM"
translator.toUUID("wbZ3JKfFLhyPheLFGhcSwM");
Python, with shortuuid (pip install shortuuid):
import uuid, shortuuid
shortuuid.uuid() # a new short UUID, e.g. "a46wtakVoH8igxN8DEmq6r"
shortuuid.encode(uuid.UUID("f47ac10b-58cc-4372-a567-0e02b2c3d479")) # "mWQEpU4e6KxNwyNiqnVzSw"
shortuuid.decode("mWQEpU4e6KxNwyNiqnVzSw")
UUID to base64 (URL-safe) and back needs no library:
// Node
Buffer.from(uuid.replaceAll("-", ""), "hex").toString("base64url");
Buffer.from(short, "base64url").toString("hex")
.replace(/(.{8})(.{4})(.{4})(.{4})/, "$1-$2-$3-$4-");
# Python
base64.urlsafe_b64encode(uuid.UUID(s).bytes).rstrip(b"=").decode()
uuid.UUID(bytes=base64.urlsafe_b64decode(short + "=="))
More small utilities live on the tools page.