How to Generate a UUID in JavaScript, Python and SQL
Every modern runtime can generate a UUID without a library. Here are the one-liners for JavaScript, Python and PostgreSQL, a tested UUID v7 function, and two common mistakes.
To generate a UUID in JavaScript, Python or SQL you no longer need a package. The UUID Generator creates them in the browser, and its page explains how the versions differ and when to use each. This guide shows how to create them in code.
Generate a UUID in JavaScript
crypto.randomUUID();
// "7e338c66-c54b-426e-b18a-030cb358d321"crypto.randomUUID() returns a random version 4 UUID, built from a cryptographically secure random source. It is available in Node and in all current browsers, with one condition in the browser: the page must be served over HTTPS or from localhost, because the function only exists in secure contexts.
Generate a UUID v7 in JavaScript
Version 7 puts a millisecond timestamp in the first 48 bits, so IDs created later sort later, which keeps database indexes compact. There is no built-in function for it in JavaScript yet, but RFC 9562 makes it short to write:
function uuidv7() {
const bytes = crypto.getRandomValues(new Uint8Array(16));
const ms = BigInt(Date.now());
for (let i = 0; i < 6; i++) bytes[i] = Number((ms >> BigInt(8 * (5 - i))) & 0xffn);
bytes[6] = (bytes[6] & 0x0f) | 0x70; // version 7
bytes[8] = (bytes[8] & 0x3f) | 0x80; // RFC variant
const hex = [...bytes].map((b) => b.toString(16).padStart(2, "0")).join("");
return [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20)].join("-");
}
uuidv7(); // "01a14e9b-ce01-7b63-b660-535fa2569f01"Two IDs created five milliseconds apart compared in creation order as plain strings, and the first 12 hex digits decode back to the creation time. IDs created within the same millisecond are not ordered among themselves, because the rest is random.
Generate a UUID in Python
import uuid
uuid.uuid4() # random
uuid.uuid7() # time-ordered, Python 3.14 and later
str(uuid.uuid4())uuid.uuid4() uses the operating system's secure random source. uuid.uuid7() arrived in Python 3.14; on older versions, use the JavaScript algorithm above ported to Python, or a maintained package. Call str() before putting a UUID into JSON, because json.dumps cannot serialise a UUID object directly.
Generate a UUID in PostgreSQL
select gen_random_uuid();
-- 1463912a-2315-4085-8671-89381ca976c3
create table orders (
id uuid primary key default gen_random_uuid(),
created_at timestamptz not null default now()
);gen_random_uuid() is built into PostgreSQL 13 and later and returns a version 4 UUID. PostgreSQL 18 adds uuidv7() for time-ordered keys. Store UUIDs in a uuid column, not in text: it takes 16 bytes instead of 36 and compares faster.
Two common mistakes
- Building UUIDs from Math.random(). It is not a secure random source, and some older snippets that use it produce IDs that collide far more often than real UUIDs. Use
crypto.randomUUID()orcrypto.getRandomValues(). - Treating UUIDs as secrets. A random UUID is hard to guess, but it is not a password or a token: it appears in URLs, logs and analytics. For a password reset link or an API key, generate a dedicated secret and store only its hash.
Related tools
Create compact, URL-safe Nano IDs with a custom size and alphabet.
Compute SHA-1, SHA-256, SHA-384, and SHA-512 hashes of text or files.
Generate strong, random passwords with a live strength estimate.
Convert between Unix timestamps and human-readable dates, both ways.
Related guides
Every new table and every public URL needs an ID scheme. Here is how Nano ID, UUID and auto-increment compare, with the collision maths worked out and a tested Nano ID function.
A published SHA-256 checksum only helps if you compare it. Here are the exact commands for each operating system, a one-step check against a checksum file, and code for Node.js and Python.
NIST's guidance dropped the rules most sign-up forms still enforce. Here is what it recommends instead, why length beats symbols in numbers, and how to generate passwords securely in code.
JSON has no date type, so every API picks a format for dates. Here is how Unix timestamps and ISO 8601 strings compare, the bugs each one invites, and what to use where.