Developer's Corner

Generate UUIDs in your favorite programming language. Use these snippets in your app instead of calling our API for faster, offline generation. When you need one-off UUIDs in the browser or from a script, our generator and API are available; for bulk generation or validation use the bulk tool or validator.

Below are code samples for v4 (random) and, where supported, v1 (time-based) or v5 (name-based). All produce RFC 4122–compliant UUIDs. For more on when to use which version, read our article When to Use UUID v4 vs v5?.

Python

Use the built-in uuid module (Python 3.x).

import uuid

# Version 4 (random)
uid = uuid.uuid4()
print(uid)  # e.g. 550e8400-e29b-41d4-a716-446655440000

# Version 1 (time-based)
uid1 = uuid.uuid1()

JavaScript / Node.js

Node 14.17+ and modern browsers support crypto.randomUUID().

// Version 4 (random) — Node and browser
const uuid = crypto.randomUUID();
console.log(uuid);

// Node: uuid package for v1, v4, v5
// npm install uuid
const { v4: uuidv4, v1: uuidv1 } = require('uuid');
console.log(uuidv4());
console.log(uuidv1());

To call the UUIDFactory API from TypeScript or Node.js, use the official client: uuidfactory-ts on GitHub · uuidfactory-ts on npm.

Java

Java 7+ or java.util.UUID; for v1/v4 use a library.

import java.util.UUID;

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

C#

System.Guid generates v4-style GUIDs.

using System;

Guid guid = Guid.NewGuid();
Console.WriteLine(guid.ToString());  // e.g. 550e8400-e29b-41d4-a716-446655440000

Go

Use github.com/google/uuid.

import "github.com/google/uuid"

u := uuid.New()
fmt.Println(u.String())

u1 := uuid.NewMD5(uuid.NameSpaceDNS, []byte("example.com"))
fmt.Println(u1.String())  // v5

PHP

PHP 7+ has ramsey/uuid or native in PHP 8.2+.

// PHP 8.2+
$uuid = \Symfony\Component\Uid\Uuid::v4();

// Or ramsey/uuid
use Ramsey\Uuid\Uuid;
$uuid = Uuid::uuid4();
echo $uuid->toString();

To call the UUIDFactory API from PHP, use the official client: uuidfactory-php on Packagist.

Ruby

Use the uuid or securerandom gem.

require 'securerandom'

uuid = SecureRandom.uuid
puts uuid  # e.g. 550e8400-e29b-41d4-a716-446655440000

Rust

Use the uuid crate.

use uuid::Uuid;

let id = Uuid::new_v4();
println!("{}", id);

Need UUIDs over HTTP? Use our API. For bulk generation in the browser, use the Bulk Generator.