Skip to content

How to Generate a UUID in JavaScript, Python, Java, C#, Go and SQL

Most languages and databases can generate a UUID in one line, with no extra library. Below are copy-paste examples for the common ones. If you just need a few without writing code, the UUID Generator makes up to 100 v4 or v7 UUIDs at once in your browser. For v4 versus v7, see What is a UUID?

Do the calculation nowUUID Generator

JavaScript / TypeScript

crypto.randomUUID() is built into every current browser and Node.js and uses a cryptographically secure random source. In browsers it only works on HTTPS (or localhost) pages.

// Browsers and Node.js 19+
const id = crypto.randomUUID();
// "3b241101-e2bb-4255-8caf-4136c566a962"

// Older Node.js (14.17+)
import { randomUUID } from "node:crypto";
const id2 = randomUUID();

Python

The standard uuid module is all you need. uuid7() arrived in Python 3.14; on older versions use uuid4().

import uuid

str(uuid.uuid4())   # random, version 4
str(uuid.uuid7())   # time-ordered, version 7 (Python 3.14+)

Java

java.util.UUID generates random (version 4) UUIDs with no extra dependency.

import java.util.UUID;

String id = UUID.randomUUID().toString();

C# / .NET

Microsoft calls UUIDs GUIDs; the format is the same. Guid.CreateVersion7() was added in .NET 9.

var id = Guid.NewGuid();            // version 4
var ordered = Guid.CreateVersion7(); // version 7 (.NET 9+)

Go

Go's standard library has no UUID package; google/uuid is the usual choice.

// go get github.com/google/uuid
import "github.com/google/uuid"

id := uuid.NewString()        // version 4
v7, err := uuid.NewV7()       // version 7

SQL (PostgreSQL, SQL Server, MySQL)

Generating the ID in the database makes it easy to use as a column default. Note that MySQL's UUID() returns version 1 (time and machine based), not version 4.

-- PostgreSQL 13+
SELECT gen_random_uuid();
-- PostgreSQL 18+: time-ordered
SELECT uuidv7();

-- SQL Server
SELECT NEWID();

-- MySQL (returns a version 1 UUID)
SELECT UUID();

Terminal

For a quick value on the command line:

# Linux / macOS
uuidgen

# PowerShell
[guid]::NewGuid().ToString()

Frequently asked questions

Do the calculation nowUUID Generator