How to Generate UUIDs in Python, JavaScript, PHP, Java and Go
Copy-paste code examples for generating UUID v4 and UUID v7 in Python, JavaScript, Node.js, PHP, Java and Go. Covers built-in modules, popular libraries, and bulk generation.
A UUID (Universally Unique Identifier) is a 128-bit identifier that can be generated independently on any machine without a central coordinator. This post is a code reference — pick your language and copy the snippet. For a deeper look at UUID versions, database trade-offs, and when to use each type, see What Is a UUID and When Should You Use One?.
Python
The uuid module is part of the Python standard library — no install needed.
import uuid
# UUID v4 (random) — most common
uid = uuid.uuid4()
print(uid) # e.g. 550e8400-e29b-41d4-a716-446655440000
print(str(uid)) # string representation
print(uid.hex) # without hyphens: 550e8400e29b41d4a716446655440000
# UUID v1 (time-based, includes MAC address)
uid_v1 = uuid.uuid1()
print(uid_v1)
# UUID v3 (name-based, MD5)
uid_v3 = uuid.uuid3(uuid.NAMESPACE_DNS, "devgizmo.co.uk")
print(uid_v3)
# UUID v5 (name-based, SHA-1) — preferred over v3
uid_v5 = uuid.uuid5(uuid.NAMESPACE_DNS, "devgizmo.co.uk")
print(uid_v5)
Bulk generation in Python
import uuid
uuids = [str(uuid.uuid4()) for _ in range(100)]
print("\n".join(uuids))
UUID v7 in Python (time-ordered)
UUID v7 is not yet in the standard library (as of Python 3.13). Use the uuid6 package:
pip install uuid6
import uuid6
uid = uuid6.uuid7()
print(uid) # e.g. 018e7adf-5b12-7349-8a24-6c8b7f4b2a13
JavaScript (Browser)
All modern browsers expose crypto.randomUUID() natively — no library needed.
// UUID v4, built-in (Chrome 92+, Firefox 95+, Safari 15.4+)
const uid = crypto.randomUUID();
console.log(uid); // "110e8400-e29b-41d4-a716-446655440000"
// Bulk generation
const uuids = Array.from({ length: 10 }, () => crypto.randomUUID());
console.log(uuids);
Node.js
Built-in (Node.js 14.17+)
const { randomUUID } = require("crypto");
const uid = randomUUID();
console.log(uid);
TypeScript
import { randomUUID } from "crypto";
const uid: string = randomUUID();
console.log(uid);
UUID v7 in Node.js
npm install uuidv7
import { uuidv7 } from "uuidv7";
const uid = uuidv7();
console.log(uid); // "018e7adf-5b12-7349-8a24-6c8b7f4b2a13"
uuid npm package (all versions)
npm install uuid
import { v1, v3, v4, v5, NAMESPACE_DNS } from "uuid";
console.log(v4()); // random
console.log(v1()); // time-based
console.log(v5("devgizmo.co.uk", NAMESPACE_DNS)); // name-based
PHP
PHP 7.0 and below — manual generation
<?php
function uuid4(): string {
$data = random_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // version 4
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // variant bits
return vsprintf("%s%s-%s-%s-%s-%s%s%s", str_split(bin2hex($data), 4));
}
echo uuid4();
PHP 8.3+ — Uuid class (RFC 9562)
<?php
// Available from PHP 8.3 via the ramsey/uuid package or ext-uuid
// Most projects use Ramsey's library:
// composer require ramsey/uuid
use Ramsey\Uuid\Uuid;
$uid = Uuid::uuid4()->toString();
echo $uid;
// UUID v7
$uid7 = Uuid::uuid7()->toString();
echo $uid7;
Java
Standard library (java.util.UUID)
import java.util.UUID;
public class UuidExample {
public static void main(String[] args) {
// UUID v4 (random)
UUID uid = UUID.randomUUID();
System.out.println(uid);
System.out.println(uid.toString());
// UUID from string
UUID parsed = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");
System.out.println(parsed.version()); // 4
// UUID v3 (name-based, MD5)
UUID uid3 = UUID.nameUUIDFromBytes("devgizmo.co.uk".getBytes());
System.out.println(uid3);
}
}
Bulk generation in Java
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
List<String> uuids = IntStream.range(0, 100)
.mapToObj(i -> UUID.randomUUID().toString())
.collect(Collectors.toList());
UUID v7 in Java
# Maven
<dependency>
<groupId>com.github.f4b6a3</groupId>
<artifactId>uuid-creator</artifactId>
<version>5.3.7</version>
</dependency>
import com.github.f4b6a3.uuid.UuidCreator;
String uid7 = UuidCreator.getTimeOrderedEpoch().toString(); // UUID v7
Go
Standard library (crypto/rand + manual RFC 4122 construction)
Go's standard library doesn't include a UUID package, but generating a v4 UUID manually is straightforward:
package main
import (
"crypto/rand"
"fmt"
)
func newUUIDv4() string {
b := make([]byte, 16)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
b[6] = (b[6] & 0x0f) | 0x40 // version 4
b[8] = (b[8] & 0x3f) | 0x80 // variant bits
return fmt.Sprintf("%08x-%04x-%04x-%04x-%012x",
b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
}
func main() {
fmt.Println(newUUIDv4())
}
Google UUID package (recommended)
go get github.com/google/uuid
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
// UUID v4
uid := uuid.New()
fmt.Println(uid.String())
// UUID v7
uid7, err := uuid.NewV7()
if err != nil {
panic(err)
}
fmt.Println(uid7.String())
}
UUID Format Reference
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
| Position | Meaning |
|---|---|
M | Version digit (4 = random, 7 = time-ordered) |
N | Variant bits (8, 9, a, or b for RFC 4122) |
A UUID is always 36 characters including hyphens, or 32 hex characters without.
UUID Version Cheat Sheet
| Version | Algorithm | Use case |
|---|---|---|
| v1 | Time + MAC | Sortable; leaks MAC address |
| v3 | MD5 name-based | Reproducible from known input |
| v4 | Random | General purpose — most widely used |
| v5 | SHA-1 name-based | Reproducible; preferred over v3 |
| v7 | Time + random | New default — sortable, DB-friendly |
Generate UUIDs instantly in your browser with the UUID Generator.