How to Generate UUID in Python, JavaScript & More

UUIDs are the standard for generating unique identifiers. Here's how to create them in every major language — with the right version for your use case.

⚡ Generate UUIDs Instantly

Need a UUID right now? Generate single or bulk UUIDs with one click — no code required.

Open UUID Generator →

Quick Reference: UUID Generation by Language

Here's the fastest way to generate a UUID v4 (random) in each language:

# Python
import uuid
print(uuid.uuid4())

// JavaScript (browser)
console.log(crypto.randomUUID());

// Node.js
const { randomUUID } = require('crypto');
console.log(randomUUID());

// Java
import java.util.UUID;
System.out.println(UUID.randomUUID());

// Go
import "github.com/google/uuid"
fmt.Println(uuid.New())

// C# / .NET
Console.WriteLine(Guid.NewGuid());

// Ruby
require 'securerandom'
puts SecureRandom.uuid

// PHP
echo \Ramsey\Uuid\Uuid::uuid4();

# Command line (Linux/macOS)
uuidgen

Python UUID Generation (In-Depth)

Python's built-in uuid module supports all standard UUID versions:

UUID v4 — Random (Most Common)

import uuid

# Generate a single UUID v4
my_uuid = uuid.uuid4()
print(my_uuid)          # e.g., 7b2d8c4f-5e1a-4b3c-9d6e-8f0a1b2c3d4e
print(str(my_uuid))     # Same, as string
print(my_uuid.hex)      # Without hyphens: 7b2d8c4f5e1a4b3c9d6e8f0a1b2c3d4e

# Generate multiple UUIDs
uuids = [str(uuid.uuid4()) for _ in range(10)]

UUID v1 — Time-Based

import uuid

# Includes timestamp and MAC address
my_uuid = uuid.uuid1()
print(my_uuid)
print(my_uuid.time)   # Timestamp component
print(my_uuid.node)   # MAC address as integer

Warning: UUID v1 leaks your MAC address and creation time. Use v4 for most cases, or v7 if you need time-ordering without exposing hardware info.

UUID v5 — Name-Based (SHA-1)

import uuid

# Deterministic: same namespace + name → same UUID every time
namespace = uuid.NAMESPACE_DNS
my_uuid = uuid.uuid5(namespace, 'devtoolkit.cloud')
print(my_uuid)  # Always: a3c4b2d1-... (same input = same output)

# Custom namespace
custom_ns = uuid.UUID('12345678-1234-5678-1234-567812345678')
my_uuid = uuid.uuid5(custom_ns, 'my-resource')

UUID v7 — Time-Ordered Random (Modern)

UUID v7 was standardized in RFC 9562 (2024). Python 3.14+ includes native support:

# Python 3.14+
import uuid
my_uuid = uuid.uuid7()
print(my_uuid)

# For Python < 3.14, use the uuid7 package:
# pip install uuid7
import uuid7
print(uuid7.create())

UUID v7 embeds a Unix timestamp in the first 48 bits, making UUIDs naturally sortable by creation time — ideal for database primary keys.

JavaScript UUID Generation

Browser (Native API)

// Modern browsers — the best option
const id = crypto.randomUUID();
console.log(id); // "3b241101-e2bb-4d52-8c6a-136c10a498db"

// Check support (available in all modern browsers since 2022)
if (typeof crypto.randomUUID === 'function') {
  // Use native API
} else {
  // Fallback needed
}

Node.js

// Node.js 19+ (built-in)
const { randomUUID } = require('node:crypto');
console.log(randomUUID());

// Or using the uuid package (supports v1, v3, v4, v5, v7)
// npm install uuid
const { v4: uuidv4, v7: uuidv7 } = require('uuid');
console.log(uuidv4());  // Random
console.log(uuidv7());  // Time-ordered

Without Dependencies (Fallback)

// Not cryptographically secure — use only as last resort
function uuidv4Fallback() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
    const r = Math.random() * 16 | 0;
    const v = c === 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

Java UUID Generation

import java.util.UUID;

// UUID v4 (random)
UUID uuid = UUID.randomUUID();
System.out.println(uuid.toString());

// Parse a UUID string
UUID parsed = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");

// Get version
System.out.println(uuid.version());  // 4

// Generate from name (v3 — MD5 based)
UUID named = UUID.nameUUIDFromBytes("devtoolkit.cloud".getBytes());

Go UUID Generation

package main

import (
    "fmt"
    "github.com/google/uuid"
)

func main() {
    // UUID v4
    id := uuid.New()
    fmt.Println(id.String())

    // UUID v7 (time-ordered)
    id7, _ := uuid.NewV7()
    fmt.Println(id7.String())

    // Parse
    parsed, err := uuid.Parse("550e8400-e29b-41d4-a716-446655440000")
    if err != nil {
        fmt.Println("Invalid UUID")
    }
    fmt.Println(parsed)
}

Command Line UUID Generation

# macOS / Linux
uuidgen

# Linux (alternative)
cat /proc/sys/kernel/random/uuid

# Python one-liner
python3 -c "import uuid; print(uuid.uuid4())"

# Node.js one-liner
node -e "console.log(crypto.randomUUID())"

# Generate 10 UUIDs
for i in $(seq 1 10); do uuidgen; done

Which UUID Version Should You Use?

🎲 Need UUIDs Without Code?

Generate single or bulk UUIDs instantly. Copy with one click — perfect for testing and config files.

Open UUID Generator →

Recommended Tools & Resources

Level up your workflow with these developer tools:

MongoDB Atlas Free Tier → Supabase (Postgres) → Designing Data-Intensive Applications →

Dev Tools Digest

Get weekly developer tools, tips, and tutorials. Join our developer newsletter.

Frequently Asked Questions

Are UUID v4 values truly unique?

For all practical purposes, yes. With 122 random bits, you'd need to generate 2.7 quintillion UUIDs to have a 50% chance of one collision. That's generating 1 billion UUIDs per second for 86 years.

Should I use UUID or auto-increment IDs in databases?

UUIDs are better for distributed systems (no coordination needed), API exposure (no enumeration attacks), and microservices. Auto-increment is simpler and more storage-efficient. UUID v7 gives you the best of both — unique and sortable.

How do I store UUIDs in a database efficiently?

Store as a native UUID type (PostgreSQL) or BINARY(16) (MySQL) — not as VARCHAR(36). This saves 20 bytes per row and improves index performance significantly.

Related reading: UUID v4 Generator Guide · GUID vs UUID Difference

Frequently Asked Questions

How do I generate a UUID in Python?

Use the built-in uuid module: import uuid; my_id = uuid.uuid4(). This generates a random UUID v4. For a string: str(uuid.uuid4()). For UUID v5 (name-based): uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com').

How do I generate a UUID in JavaScript?

In Node.js: const { randomUUID } = require('crypto'); randomUUID(). In browsers: crypto.randomUUID(). For older browsers, use the uuid npm package: import { v4 as uuidv4 } from 'uuid'; uuidv4().

Is UUID v4 truly unique?

For all practical purposes, yes. UUID v4 has 2^122 possible values (5.3 × 10^36). The probability of a collision after generating 1 billion UUIDs per second for 100 years is essentially zero.