Skip to content
uuidgen

UUID code examples

Making a random v4 and a time-ordered v7 UUID, and a name-based v5 UUID for the name example.com in the DNS namespace, in popular languages. The random ones differ every run; the v5 one is always:

cfbff0d1-9375-5685-968c-48ce8b15ae17

UUIDs in JavaScript (browser)

crypto.randomUUID() is built into every modern browser (in secure contexts: HTTPS or localhost). For v7 or v5, use the uuid package as in the Node.js example.

const id = crypto.randomUUID(); // v4
console.log(id);

// Check a string is a UUID (any version) and read its version digit
const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
console.log(UUID_RE.test(id), "version", id[14]);

UUIDs in Node.js

Install: npm install uuid

import { randomUUID } from "node:crypto";
import { v5, v7, validate, version, NIL } from "uuid";

console.log("v4:", randomUUID());
console.log("v7:", v7());

// v5: the DNS namespace UUID is 6ba7b810-9dad-11d1-80b4-00c04fd430c8 (v5.DNS)
console.log("v5:", v5("example.com", v5.DNS));

const id = v7();
console.log(validate(id), version(id), NIL); // true 7 00000000-0000-0000-0000-000000000000

UUIDs in Python

uuid.uuid7() is new in Python 3.14. On older versions, use the uuid6 package from PyPI.

import uuid

print("v4:", uuid.uuid4())
print("v7:", uuid.uuid7())  # Python 3.14+
print("v5:", uuid.uuid5(uuid.NAMESPACE_DNS, "example.com"))

u = uuid.UUID("{CFBFF0D1-9375-5685-968C-48CE8B15AE17}")  # braces and case are fine
print(u.version, u.hex, u.bytes_le.hex())  # bytes_le = Windows GUID byte order

UUIDs in Java

The JDK makes v4 (randomUUID) and v3 (nameUUIDFromBytes, MD5) UUIDs. For v7 or v5, use a library such as com.fasterxml.uuid:java-uuid-generator.

import java.util.UUID;

public class Main {
  public static void main(String[] args) {
    UUID id = UUID.randomUUID(); // v4
    System.out.println("v4: " + id);

    UUID parsed = UUID.fromString("cfbff0d1-9375-5685-968c-48ce8b15ae17");
    System.out.println("version " + parsed.version() + ", variant " + parsed.variant()); // version 5, variant 2
  }
}

UUIDs in C# / .NET

Guid.CreateVersion7() and the Version property need .NET 9 or later. .NET has no built-in v5.

using System;

Guid id = Guid.NewGuid();             // v4
Guid sortable = Guid.CreateVersion7(); // v7 (.NET 9+)

Console.WriteLine(id);                 // 3f2504e0-4f89-41d3-9a0c-0305e82c3301
Console.WriteLine(id.ToString("B").ToUpperInvariant()); // {3F2504E0-4F89-41D3-9A0C-0305E82C3301}
Console.WriteLine(sortable);

Guid parsed = Guid.Parse("{CFBFF0D1-9375-5685-968C-48CE8B15AE17}");
Console.WriteLine($"version {parsed.Version}"); // version 5

// ToByteArray() stores the first three groups little-endian (the Windows GUID layout)
Console.WriteLine(Convert.ToHexString(parsed.ToByteArray()));

UUIDs in Go

Install: go get github.com/google/uuid

package main

import (
	"fmt"

	"github.com/google/uuid"
)

func main() {
	fmt.Println("v4:", uuid.New())
	v7, err := uuid.NewV7()
	if err != nil {
		panic(err)
	}
	fmt.Println("v7:", v7)
	fmt.Println("v5:", uuid.NewSHA1(uuid.NameSpaceDNS, []byte("example.com")))

	u, err := uuid.Parse("urn:uuid:cfbff0d1-9375-5685-968c-48ce8b15ae17")
	if err != nil {
		panic(err)
	}
	fmt.Println("version", u.Version()) // version VERSION_5
}

UUIDs in Rust

Install: cargo add uuid --features v4,v5,v7

use uuid::Uuid;

fn main() {
    println!("v4: {}", Uuid::new_v4());
    println!("v7: {}", Uuid::now_v7());
    println!("v5: {}", Uuid::new_v5(&Uuid::NAMESPACE_DNS, b"example.com"));

    let u = Uuid::parse_str("CFBFF0D1-9375-5685-968C-48CE8B15AE17").unwrap();
    println!("version {}", u.get_version_num()); // version 5
    println!("{}", u.braced()); // {cfbff0d1-9375-5685-968c-48ce8b15ae17}
}

UUIDs in PostgreSQL

gen_random_uuid() is built in from PostgreSQL 13 and uuidv7() from PostgreSQL 18. v5 needs the uuid-ossp extension.

SELECT gen_random_uuid();  -- v4
SELECT uuidv7();           -- v7 (PostgreSQL 18+)

CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
SELECT uuid_generate_v5(uuid_ns_dns(), 'example.com');

-- A v7 primary key: new rows go at the end of the index
CREATE TABLE orders (
  id    uuid PRIMARY KEY DEFAULT uuidv7(),
  total numeric NOT NULL
);

UUIDs in Bash / Linux

uuidgen comes with util-linux on Linux (--time-v7 needs version 2.41 or later) and is built into macOS (v4 only, uppercase).

uuidgen                      # v4
uuidgen --time-v7            # v7
uuidgen --sha1 --namespace @dns --name example.com   # v5

cat /proc/sys/kernel/random/uuid   # v4 straight from the Linux kernel

UUIDs in PowerShell

New-Guid works in Windows PowerShell 5.1 and PowerShell 7 and makes v4 GUIDs.

New-Guid                                  # v4, as an object
(New-Guid).ToString()                     # 3f2504e0-4f89-41d3-9a0c-0305e82c3301
[guid]::NewGuid().ToString("B").ToUpper() # {3F2504E0-4F89-41D3-9A0C-0305E82C3301}

[guid]::Parse("cfbff0d1-9375-5685-968c-48ce8b15ae17")   # throws if it isn't a GUID