UUID Generator

A UUID is a 128-bit identifier written as 32 hex digits in five groups, such as 3f2b8c1e-9a4d-4e7b-b0c2-5d6e7f8a9b0c. Version 4 is fully random; version 7 starts with a timestamp so IDs sort by creation time, which makes it the better choice for database keys. Pick a version and how many you need.

Anatomy of a UUID

xxxxxxxx-xxxx-Vxxx-Nxxx-xxxxxxxxxxxx

V is the version digit (4 or 7). N is the variant, and is always 8, 9, a or b for standard UUIDs. In a v4 UUID, the other 122 bits are random. In a v7 UUID (RFC 9562), the first 12 hex digits are the Unix time in milliseconds, followed by random bits.

VersionBased onSortableUse it for
v1Time + MAC addressPartlyLegacy systems; leaks the machine address
v4RandomNoGeneral IDs that reveal nothing
v5SHA-1 of a nameNoThe same ID every time for the same input
v7Time + randomYesDatabase primary keys, event IDs

Generate a UUID in code

crypto.randomUUID();                 // browser and Node 19+: v4
import { v7 } from 'uuid'; v7();     // npm package "uuid"
import uuid; uuid.uuid4()            # Python
SELECT gen_random_uuid();            -- PostgreSQL 13+: v4
SELECT uuidv7();                     -- PostgreSQL 18+

Frequently asked questions

Should I use UUID v4 or v7?

Use v7 for database primary keys. Its first 48 bits are a millisecond timestamp, so new IDs sort in creation order and insert at the end of a B-tree index instead of at random places, which keeps indexes compact and inserts fast. Use v4 when you want an ID that reveals nothing, not even roughly when it was made.

Can two UUIDs ever be the same?

In theory, yes; in practice, no. A v4 UUID has 122 random bits. You would need to generate about 2.7 quintillion of them to have a 50% chance of a single duplicate.

Are these UUIDs secure to use as tokens?

They come from the browser’s cryptographically secure random generator (crypto.getRandomValues), so they are unpredictable. Even so, for session tokens or password reset links, prefer a purpose-built token with at least 128 random bits and an expiry.

What is the difference between a UUID and a GUID?

Nothing meaningful. GUID is Microsoft’s name for the same 128-bit identifier format.

Related tools