Definition

Base62 encoding represents a UUID’s 128 bits as a string using 62 characters: digits 0–9, uppercase letters A–Z, and lowercase letters a–z. The standard UUID format uses 32 hex digits plus 4 hyphens (36 characters total). Base62 reduces this to 22 characters — a 39% reduction — while remaining URL-safe and human-typeable.

Standard UUID:  018fbe3a-4c5d-7b12-8abc-0123456789ab  (36 chars, only hex + hyphens)
Base62 UUID:    2Bj9kFmNpQrSuVwXyZ4A7c                (22 chars, alphanumeric)
Base64url UUID: AY--OkxdexKKvAEjRWiA  (24 chars with == padding stripped)

See UUID URL-Safe Encoding for a full comparison of Base62, Base64url, and Base58.

Encoding and decoding

// Base62 encode/decode for UUID v7
const ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
const BASE = BigInt(62);

export function uuidToBase62(uuid: string): string {
  // Convert UUID hex to a 128-bit integer
  let n = BigInt('0x' + uuid.replace(/-/g, ''));
  const chars: string[] = [];

  while (n > 0n) {
    chars.unshift(ALPHABET[Number(n % BASE)]);
    n = n / BASE;
  }

  // Pad to 22 characters
  return chars.join('').padStart(22, '0');
}

export function base62ToUuid(encoded: string): string {
  let n = 0n;
  for (const ch of encoded) {
    n = n * BASE + BigInt(ALPHABET.indexOf(ch));
  }

  const hex = n.toString(16).padStart(32, '0');
  return `${hex.slice(0,8)}-${hex.slice(8,12)}-${hex.slice(12,16)}-${hex.slice(16,20)}-${hex.slice(20)}`;
}

// Usage
import { v7 as uuidv7 } from 'uuid';
const id  = uuidv7();                   // '018fbe3a-4c5d-7b12-8abc-0123456789ab'
const b62 = uuidToBase62(id);           // '2Bj9kFmNpQrSuVwXyZ4A7c'
const back = base62ToUuid(b62);         // '018fbe3a-4c5d-7b12-8abc-0123456789ab'

Python implementation

import uuid

ALPHABET = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
BASE = 62

def uuid_to_base62(uid: uuid.UUID) -> str:
    n = uid.int
    chars = []
    while n:
        chars.append(ALPHABET[n % BASE])
        n //= BASE
    return ''.join(reversed(chars)).zfill(22)

def base62_to_uuid(encoded: str) -> uuid.UUID:
    n = 0
    for ch in encoded:
        n = n * BASE + ALPHABET.index(ch)
    return uuid.UUID(int=n)

# Usage with uuid.uuid7() (Python 3.13+)
uid = uuid.uuid7()
b62 = uuid_to_base62(uid)        # 22-character string
back = base62_to_uuid(b62)       # reconstructed UUID

Comparison of compact UUID formats

FormatLengthURL-safeSort-preservingCharacters
Standard UUID36No (hyphens OK, but verbose)Yes (v7)Hex + hyphens
Base64url24YesNoA–Z, a–z, 0–9, -, _
Base6222YesYes (with correct alphabet)0–9, A–Z, a–z
Base5822YesNoExcludes 0, O, I, l
Hex (no hyphens)32YesYes (v7)0–9, a–f

Base62 is the shortest URL-safe format that preserves sort order for UUID v7. Base58 excludes visually ambiguous characters (better for human transcription) but sacrifices sort order. See UUID URL-Safe Encoding.

Use cases

For the full cursor pagination pattern, see UUID v7 for Cursor Pagination.

External references

Frequently asked questions

When should I use Base62 encoding instead of the standard UUID string?

Use Base62 when UUID values appear in URLs, short codes, or user-visible identifiers where length matters — 22 characters vs 36 is a meaningful difference for QR codes, SMS links, and mobile UIs. Base62 preserves the full 128 bits of the UUID, so no information is lost. The trade-off is that Base62 strings are not directly parseable by standard UUID libraries — you need to decode back to UUID format before validation. See UUID URL-Safe Encoding for Base64url and Base58 alternatives.

Does Base62 encoding work with UUID v7's time-sortable property?

Yes, if you encode the UUID's bytes in big-endian order (MSB first) — which is the standard UUID byte order. A Base62-encoded UUID v7 preserves lexicographic sort order only if the Base62 alphabet is ordered numerically (0–9 before A–Z before a–z). Most implementations use this alphabet ordering. Verify your library's alphabet before relying on Base62 sort order matching UUID v7's chronological order. See Lexicographical Order.