How to Generate a UUID in Any Language (JS, Python, Java, Go, SQL)
Almost every language and database can mint a UUID with a single built-in call — you rarely need a third-party library. Below is the one line for each of the common ones, the difference between the random (v4) and time-ordered (v7) versions, and when a UUID is the wrong choice. Need one right now without writing code? The UUID generator makes them in your browser.
Open the UUID Generator →
What a UUID is (in one paragraph)
A UUID (Universally Unique Identifier) is a 128-bit number, written as 32 hexadecimal digits in a 8-4-4-4-12 pattern, e.g. 1e7c4a9b-4f3a-4c2e-8b1a-2d6f0e9c5a71. The point of a UUID is that any machine can generate one independently, with no coordination, and still be confident it will never collide with a UUID generated anywhere else. A version-4 UUID gets that guarantee from 122 bits of randomness; there are so many possible values that you could generate billions per second for a lifetime and never repeat one. That is why UUIDs are the default identifier for distributed systems, where a central auto-incrementing counter is impractical.
JavaScript and TypeScript
Modern browsers and Node (16.7+) have crypto.randomUUID() built in — no npm install, and it uses a cryptographically secure random source:
const id = crypto.randomUUID();
// '1e7c4a9b-4f3a-4c2e-8b1a-2d6f0e9c5a71'
It must be called in a secure context (HTTPS or localhost). In older Node versions, reach it through the crypto module: require('crypto').randomUUID(). Avoid the old trick of building a UUID string from Math.random() — it is not uniformly random and can collide.
Python
The standard library's uuid module needs no install:
import uuid
str(uuid.uuid4()) # '1e7c4a9b-4f3a-4c2e-8b1a-2d6f0e9c5a71'
uuid4() is the random version you want by default. The module also offers uuid1() (time + MAC address, which can leak the host) and the name-based uuid3()/uuid5() for deterministic IDs from a namespace.
Java, Kotlin and C#
JVM languages use java.util.UUID; .NET calls it a Guid but it is the same 128-bit value:
// Java / Kotlin
String id = java.util.UUID.randomUUID().toString();
// C# / .NET
string id = Guid.NewGuid().ToString();
Go, Ruby and Bash
Go's standard library has no UUID type, so the near-universal choice is Google's package. Ruby and the shell have built-ins:
// Go — go get github.com/google/uuid
id := uuid.NewString()
# Ruby
require 'securerandom'; SecureRandom.uuid
# Bash
uuidgen # macOS, most Linux
cat /proc/sys/kernel/random/uuid # Linux without uuidgen
In the database
When the UUID is a table's primary key, generating it in the database keeps inserts self-contained:
-- PostgreSQL 13+
SELECT gen_random_uuid();
-- SQL Server
SELECT NEWID();
-- MySQL 8 (time-based, v1-style)
SELECT UUID();
In Postgres you can even set DEFAULT gen_random_uuid() on the column so every insert gets one automatically.
v4 vs v7: which version to generate
Every snippet above (except MySQL's) produces a version 4, fully random UUID — the right default for most uses. But random UUIDs have a real downside as database keys: because each one is unpredictable, new rows scatter throughout the primary-key index instead of appending to the end, which fragments the index and slows inserts on large tables.
Version 7 fixes this by putting a millisecond timestamp in the high bits, so new UUIDs sort roughly in creation order and behave like an auto-increment key while staying globally unique. If you are choosing an identifier for a database primary key, read UUID v4 vs. v7 for database IDs before you commit. For the whole family — v1 through v8, plus the nil and max sentinels — see the UUID versions explained.
When not to use a UUID
UUIDs are not free. They are 128 bits versus a 32- or 64-bit integer, they are not human-friendly, and a random v4 in a URL leaks nothing but also sorts by nothing. For a single-database app that will never federate, a plain auto-increment integer is smaller and faster. Reach for UUIDs when IDs must be generated in many places at once, must not be guessable or enumerable, or must be merged across systems without collisions.
When you just need one
For a quick identifier — a test fixture, a config value, a correlation ID for a ticket — you do not need to open a shell. The UUID generator creates one or a batch in your browser with crypto.randomUUID(), with copy-all and optional uppercase / no-dashes formatting. Nothing is requested from or sent to a server.
Ready to try it? Open the UUID Generator →