Tutorials & Code 10 min read

How to Generate UUIDs in JavaScript, Python, Java, C# & PHP

By FastUUID Engineering Team

Whether you are building a REST API, configuring database primary keys, generating session tokens, or creating microservice tracking IDs, generating unique identifiers is a fundamental task in software development.

A UUID (Universally Unique Identifier) provides a standardized, collision-resistant 128-bit identifier that any application can generate independently without consulting a central database.

While the concept of a UUID is standardized across the industry, every major programming language provides its own native APIs, standard libraries, or ecosystem conventions for generating them.

In this practical, code-focused guide, you will learn how to generate UUIDs in JavaScript, Python, Java, C#, and PHP, understand the differences between native functions and third-party packages, avoid common generation pitfalls, and choose the right approach for your tech stack.


1. What Is a UUID?

A UUID is a 128-bit (16-byte) number standardized by the Internet Engineering Task Force (IETF) in RFC 9562 (and historically RFC 4122).

In software systems and APIs, a UUID is represented as a human-readable 36-character hexadecimal string divided into five groups separated by hyphens (the 8-4-4-4-12 format):

550e8400-e29b-41d4-a716-446655440000
[ 8 hex ] [4]  [4]  [4]  [  12 hex  ]

Why Developers Use UUIDs

  • Decentralized Generation: Any client, mobile app, background worker, or backend service can create a globally unique ID locally without waiting for a database sequence or coordinating with a central server.
  • Collision Resistance: A 128-bit number space contains $2^{128} \approx 3.4 \times 10^{38}$ possible values. The mathematical probability of generating two duplicate random UUIDs is virtually zero.
  • Opaque Identifiers: Unlike sequential integers (/orders/101, /orders/102), random UUIDs do not reveal business metrics, total customer counts, or record creation volume to public observers.

What Is UUID Version 4 (UUID v4)?

While UUIDs have multiple versions (v1 through v8), UUID Version 4 (UUID v4) is by far the most widely used in application code. UUID v4 identifiers are constructed from 122 cryptographically secure random bits, with 6 bits reserved to designate the version (4) and variant (RFC).

If you ever need a quick random UUID without opening an IDE, you can create one with our free browser-based UUID Generator.


2. How to Generate a UUID in JavaScript & TypeScript

Modern JavaScript provides built-in native support for generating cryptographically secure UUID Version 4 identifiers in both web browsers and Node.js without requiring third-party npm packages.

Modern Approach: crypto.randomUUID()

// Native Web Crypto API (Standard in modern browsers, Node.js 19+, Deno, & Bun)
const uuid = crypto.randomUUID();

console.log(uuid);
// Example Output: "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d"

How It Works:

  • crypto.randomUUID() is part of the standard Web Cryptography API.
  • It produces a cryptographically secure, RFC-compliant UUID Version 4 string directly.
  • Browser Requirements: crypto.randomUUID() requires a secure context (HTTPS or localhost) in modern web browsers.
  • Node.js Usage: In Node.js version 19.0.0 and above, crypto.randomUUID() is globally available. In Node.js 14.17.0 to 18.x, import it from the built-in crypto module:
// Node.js (CommonJS)
const crypto = require('crypto');
const id = crypto.randomUUID();

// Node.js / TypeScript (ES Modules)
import { randomUUID } from 'crypto';
const id = randomUUID();

Why You Should Avoid Homemade Math.random() Functions

In older tutorials, you may see custom helper functions using Math.random() and regex replacement. Never use Math.random() to generate UUIDs in production. Math.random() is not cryptographically secure, produces predictable sequences, and has a significantly higher risk of collision under heavy traffic.


3. How to Generate a UUID in Python

Python includes full, native UUID generation inside its standard library via the built-in uuid module. No external pip packages are required.

Standard Library Approach: uuid.uuid4()

import uuid

# Generate a random UUID (Version 4)
new_uuid = uuid.uuid4()

print(new_uuid)
# Example Output: 550e8400-e29b-41d4-a716-446655440000

# Check type
print(type(new_uuid))
# <class 'uuid.UUID'>

Working with UUID Objects in Python

Python’s uuid.uuid4() returns a specialized UUID object rather than a plain string. In real-world applications, you will often convert this object into different string or byte formats:

import uuid

new_uuid = uuid.uuid4()

# 1. Standard canonical string (36 characters with hyphens)
uuid_str = str(new_uuid)
print(uuid_str)
# Output: "550e8400-e29b-41d4-a716-446655440000"

# 2. Compact hexadecimal string (32 characters without hyphens)
uuid_hex = new_uuid.hex
print(uuid_hex)
# Output: "550e8400e29b41d4a716446655440000"

# 3. Raw 16-byte binary representation (for compact database storage)
uuid_bytes = new_uuid.bytes
print(len(uuid_bytes))
# Output: 16

Practical Example: FastAPI / Flask Database Model

from pydantic import BaseModel, Field
import uuid

class UserCreate(BaseModel):
    id: str = Field(default_factory=lambda: str(uuid.uuid4()))
    username: str
    email: str

4. How to Generate a UUID in Java

Java has featured native, enterprise-grade UUID generation since Java 5 through the standard java.util.UUID class.

Standard Approach: UUID.randomUUID()

import java.util.UUID;

public class UuidExample {
    public static void main(String[] args) {
        // Generate a cryptographically secure UUID v4
        UUID uuid = UUID.randomUUID();
        
        // Print the UUID object (automatically calls .toString())
        System.out.println("Generated UUID: " + uuid);
        // Example Output: "f47ac10b-58cc-4372-a567-0e02b2c3d479"
        
        // Convert explicitly to a String
        String uuidString = uuid.toString();
        System.out.println("String representation: " + uuidString);
    }
}

How It Works:

  • UUID.randomUUID() internally leverages java.security.SecureRandom, ensuring that the generated 122 random bits have strong cryptographic entropy.
  • The returned UUID object provides useful utility methods, such as uuid.getMostSignificantBits() and uuid.getLeastSignificantBits(), which are useful for storing identifiers as two 64-bit long integers in performance-critical applications.

Parsing a UUID String in Java

If your Java application receives a UUID from an API payload or database query, parse it safely using UUID.fromString():

UUID parsedUuid = UUID.fromString("550e8400-e29b-41d4-a716-446655440000");

5. How to Generate a UUID in C# & .NET

In the Microsoft and .NET ecosystem, UUIDs are universally referred to as GUIDs (Globally Unique Identifiers). Both terms describe the exact same 128-bit mathematical identifier format.

Standard Approach: Guid.NewGuid()

using System;

class Program
{
    static void Main()
    {
        // Generate a new GUID (UUID Version 4)
        Guid uuid = Guid.NewGuid();
        
        Console.WriteLine(uuid);
        // Example Output: "d8e3b74e-6e21-4f9a-8c9e-5f12e8b0a3c2"
        
        // Explicitly convert to string
        string standardString = uuid.ToString();
        Console.WriteLine(standardString);
    }
}

Formatting GUIDs in C#

.NET provides built-in format specifiers with ToString() to output GUIDs in different conventions:

Guid id = Guid.NewGuid();

// Standard 36-char hyphenated format (Default: "D")
Console.WriteLine(id.ToString("D")); 
// Output: "d8e3b74e-6e21-4f9a-8c9e-5f12e8b0a3c2"

// Compact 32-char format without hyphens ("N")
Console.WriteLine(id.ToString("N")); 
// Output: "d8e3b74e6e214f9a8c9e5f12e8b0a3c2"

// Windows COM / Registry format wrapped in braces ("B")
Console.WriteLine(id.ToString("B")); 
// Output: "{d8e3b74e-6e21-4f9a-8c9e-5f12e8b0a3c2}"

// Wrapped in parentheses ("P")
Console.WriteLine(id.ToString("P")); 
// Output: "(d8e3b74e-6e21-4f9a-8c9e-5f12e8b0a3c2)"

Native UUID v7 Support in .NET 9+

Starting with .NET 9, Microsoft introduced native support for time-ordered UUID Version 7 via Guid.CreateVersion7():

// Generates a time-ordered UUID v7 for high-performance database indexing
Guid timeOrderedId = Guid.CreateVersion7();
Console.WriteLine(timeOrderedId);

You can format Windows-style GUIDs instantly using our GUID Generator.


6. How to Generate a UUID in PHP

PHP offers both modern package-based approaches and native solutions.

Why You Should NEVER Use uniqid() as a UUID Generator

Many older PHP tutorials suggest uniqid(). uniqid() is NOT a UUID generator.

  • uniqid() is based on the current server timestamp in microseconds.
  • It is only 13 or 23 characters long (not 36 characters).
  • It does not conform to RFC 9562 or RFC 4122.
  • It is not cryptographically secure and is vulnerable to collisions under concurrent requests.

For modern production PHP applications (Laravel, Symfony, WordPress, or standalone), use an established, battle-tested package like symfony/uid or ramsey/uuid:

composer require symfony/uid
<?php
require 'vendor/autoload.php';

use Symfony\Component\Uid\Uuid;

// Generate a cryptographically secure UUID v4
$uuid = Uuid::v4();

echo $uuid->toRfc4122();
// Example Output: "f47ac10b-58cc-4372-a567-0e02b2c3d479"

// Generate a time-ordered UUID v7
$uuidV7 = Uuid::v7();
echo $uuidV7->toRfc4122();
// Example Output: "018d3b7d-3b7d-7bad-9bdd-2b0d7b3dcb6d"
?>

Native PHP Approach (Zero Dependencies)

If you are working in a legacy environment where Composer packages cannot be installed, you can generate an RFC-compliant UUID v4 using PHP 7+‘s built-in random_bytes():

<?php
function generateUuidV4(): string {
    // Generate 16 cryptographically secure random bytes
    $data = random_bytes(16);

    // Set version to 0100 (UUID Version 4)
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40);
    
    // Set variant to 10 (RFC 4122/9562 standard)
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80);

    // Format as 8-4-4-4-12 hexadecimal string
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

$myUuid = generateUuidV4();
echo $myUuid;
// Example Output: "a591a6d4-28b4-4b52-9764-1065c71b694b"
?>

7. Programming Language Comparison Table

LanguageRecommended MethodUUID VersionTypeRequires Package?
JavaScriptcrypto.randomUUID()UUID v4Built-in API❌ No
Pythonuuid.uuid4()UUID v4Standard Library (uuid)❌ No
JavaUUID.randomUUID()UUID v4Standard Library (java.util)❌ No
C# (.NET)Guid.NewGuid()UUID v4 (GUID)Standard Library (System)❌ No
PHPUuid::v4() / random_bytes()UUID v4Package or Native Functionsymfony/uid or zero-dependency

8. UUID v4 vs. Other UUID Versions

While this guide focuses primarily on UUID v4 (random generation), RFC 9562 defines several versions tailored for specific engineering needs:

┌────────────────────────────────────────────────────────────────────────┐
│ UUID VERSION OVERVIEW:                                                 │
│                                                                        │
│ • UUID v4 (Random):             122 random bits. Best for general use, │
│                                 API tokens, public resource URLs.      │
│                                                                        │
│ • UUID v7 (Time-Ordered):       48-bit Unix ms timestamp + random bits.│
│                                 Best for database primary keys.        │
│                                                                        │
│ • UUID v5 (Deterministic SHA-1): Namespace + String hash. Generates    │
│                                 identical UUIDs for identical inputs.  │
└────────────────────────────────────────────────────────────────────────┘

When building new database schemas with millions of records, developers frequently prefer UUID v7 over UUID v4 because its chronological timestamp ordering prevents database B-Tree index fragmentation.


9. UUID vs. GUID: Is There a Difference?

Developers often ask: “What is the difference between a UUID and a GUID?”

In modern software engineering, UUID and GUID refer to the exact same 128-bit identifier specification.

  • UUID (Universally Unique Identifier) is the term standardized by the Internet Engineering Task Force (IETF) in RFC 9562 and ISO/IEC 9834-8.
  • GUID (Globally Unique Identifier) is the terminology historically adopted by Microsoft for Windows, COM, and the .NET framework.

Both represent 16-byte binary values formatted in the canonical 8-4-4-4-12 hexadecimal notation.


10. Common Practical Use Cases

  1. Database Primary Keys: Providing globally unique row IDs in PostgreSQL, MySQL, SQLite, and MongoDB.
  2. REST & GraphQL API Resource Identifiers: Exposing opaque IDs in URLs (e.g., /api/v1/orders/550e8400-e29b-41d4-a716-446655440000) to prevent enumeration attacks.
  3. Distributed Microservice Tracing: Assigning a unique correlation ID (X-Request-ID) to follow a user’s transaction across multiple independent servers.
  4. Cloud Storage & File Uploads: Renaming user-uploaded files (e.g., 550e8400.jpg) in AWS S3 or Google Cloud Storage to prevent file overwrite collisions.
  5. Offline Client Synchronization: Allowing mobile apps to create records offline with permanent unique IDs before syncing to the cloud.

If you need to generate batches of mock data for testing databases or APIs, try our Bulk UUID Generator.


11. Common Mistakes to Avoid When Generating UUIDs

  • Mistake 1: Using Insecure Math Randomness. Never construct UUIDs using Math.random() in JavaScript or rand() in PHP. Always use cryptographically secure sources like crypto.randomUUID() or random_bytes().
  • Mistake 2: Relying on uniqid() in PHP. uniqid() does not produce RFC-compliant UUIDs and creates high collision risks under concurrent execution.
  • Mistake 3: Assuming UUIDs Are 100% Collision-Proof Without Database Constraints. While collisions are astronomically unlikely, database tables should always enforce a PRIMARY KEY or UNIQUE constraint on UUID columns.
  • Mistake 4: Storing UUIDs as Inefficient 36-Byte Text Strings. In high-scale databases, store UUIDs in native binary types (PostgreSQL UUID, MySQL BINARY(16)) to save over 50% disk and RAM index capacity.
  • Mistake 5: Treating UUIDs as Secret Passwords or API Keys. UUIDs are designed for uniqueness, not secrecy. Never use a UUID alone as an authentication token without cryptographic signatures (such as JWTs) or secure session hashing.

12. Which Generation Method Should You Choose?

  • Frontend JavaScript (React, Vue, Angular): Use native crypto.randomUUID().
  • Node.js Backend: Use native crypto.randomUUID() for v4, or the uuid package if you need time-sorted UUID v7.
  • Python (Django, FastAPI, Flask): Use uuid.uuid4() from the standard library.
  • Java / Kotlin / Android: Use UUID.randomUUID().
  • .NET / C#: Use Guid.NewGuid() (or Guid.CreateVersion7() in .NET 9).
  • PHP (Laravel, Symfony): Use Symfony\Component\Uid\Uuid::v4() or ramsey/uuid.

13. Generate UUIDs Online Instantly

If you need a UUID quickly for configuration files, manual testing, or database seeding without writing code, you can use our free browser-based UUID Generator to generate and copy random UUIDs instantly.

All generation on FastUUIDGenerator.com runs 100% client-side using your browser’s Web Cryptography API for maximum privacy and zero network latency.


14. Frequently Asked Questions (FAQ)

How do I generate a UUID?

You can generate a UUID programmatically using standard library functions such as crypto.randomUUID() in JavaScript, uuid.uuid4() in Python, UUID.randomUUID() in Java, or Guid.NewGuid() in C#. For instant manual creation, you can also use an online UUID Generator.

How do I generate a UUID v4?

UUID v4 is the standard random UUID type generated by default in most programming languages. Functions like crypto.randomUUID() in JS, uuid.uuid4() in Python, and Guid.NewGuid() in C# all produce UUID Version 4 identifiers.

Is UUID v4 truly unique?

UUID v4 provides practical uniqueness rather than absolute mathematical certainty. With 122 random bits ($3.4 \times 10^{38}$ combinations), the probability of generating a duplicate in any real-world application is so low that it is effectively zero.

Is a UUID the same as a GUID?

Yes. UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier) refer to the exact same 128-bit identifier format defined by RFC 9562. GUID is simply the term used primarily in the Microsoft and .NET ecosystem.

How do I validate a generated UUID string?

You can validate any UUID string to confirm its format, version, and variant compliance using our free UUID / GUID Validator.


15. Conclusion & Developer Tools

Generating UUIDs is an essential building block in modern software architecture. By utilizing the built-in cryptographic functions available in JavaScript, Python, Java, C#, and PHP, you can generate secure, collision-resistant identifiers with minimal code.

Explore our full suite of free developer utilities:

Need to generate UUIDs right now?

Generate cryptographically secure UUID v4 or time-ordered UUID v7 identifiers instantly in your browser.