UUIDs are widely used in modern software applications to identify users, database records, API resources, files, transactions, and other objects.
If you need to create a unique identifier in your application, you usually don't need to build a UUID algorithm yourself. Most popular programming languages provide libraries or built-in APIs for generating UUIDs.
In this guide, you'll learn how to generate a UUID in JavaScript, Python, PHP, Java, and C#, with practical examples you can copy into your projects.
You can also generate a UUID instantly using our free online UUID Generator without writing any code.
What Is a UUID?
A UUID (Universally Unique Identifier) is a 128-bit identifier designed to provide practical uniqueness without requiring a central system to assign identifiers.
A typical UUID looks like this:
550e8400-e29b-41d4-a716-446655440000
UUIDs are commonly used for:
-
Database IDs
-
API resources
-
User IDs
-
Orders and transactions
-
Files and objects
-
Distributed systems
-
Session and application objects
-
Event and message identifiers
There are several UUID versions, including v1, v3, v4, v5, v6, v7, and v8.
For many applications, UUID v4 is a simple choice when you need a randomly generated identifier. UUID v7 is particularly useful when time ordering is important.
Generate a UUID in JavaScript
Modern JavaScript environments provide a convenient way to generate a UUID v4 using the Web Crypto API.
Using crypto.randomUUID()
The simplest approach is:
const uuid = crypto.randomUUID();
console.log(uuid);
Example output:
550e8400-e29b-41d4-a716-446655440000
The randomUUID() method generates a UUID using a cryptographically secure random number generator where supported.
Create a Function
You can wrap it in a reusable function:
function generateUUID() {
return crypto.randomUUID();
}
console.log(generateUUID());
Generate Multiple UUIDs
function generateUUIDs(count) {
return Array.from(
{ length: count },
() => crypto.randomUUID()
);
}
console.log(generateUUIDs(5));
This produces an array containing five UUIDs.
JavaScript UUID for Node.js
In modern Node.js versions, you can use the built-in crypto module:
const { randomUUID } = require('crypto');
const uuid = randomUUID();
console.log(uuid);
With ES modules:
import { randomUUID } from 'node:crypto';
console.log(randomUUID());
This avoids adding an external dependency when your Node.js version provides the API.
Generate a UUID in Python
Python includes the built-in uuid module, making UUID generation straightforward.
Generate UUID v4
import uuid
my_uuid = uuid.uuid4()
print(my_uuid)
Example:
550e8400-e29b-41d4-a716-446655440000
The result is a UUID object. If you need it as a string:
import uuid
my_uuid = str(uuid.uuid4())
print(my_uuid)
Generate UUID v1 in Python
Python also provides UUID v1 generation:
import uuid
my_uuid = uuid.uuid1()
print(my_uuid)
UUID v1 is time-based and has different properties from UUID v4.
Generate UUID v7 in Python
Python's standard uuid module support for newer UUID versions depends on the Python version you are using. If your environment does not provide the UUID version you need, use a library that supports it or generate it through an appropriate implementation.
For new applications requiring time-ordered UUIDs, UUID v7 is worth considering.
Generate Multiple UUIDs in Python
import uuid
uuids = [str(uuid.uuid4()) for _ in range(10)]
for value in uuids:
print(value)
This generates ten UUID v4 values.
Generate a UUID in PHP
PHP applications can generate UUIDs using a UUID package or an application/library implementation.
A popular approach is to use Symfony UID.
Using Symfony UID
Install the package with Composer:
composer require symfony/uid
Then:
<?php
use Symfony\Component\Uid\Uuid;
$uuid = Uuid::v4();
echo $uuid;
Example output:
550e8400-e29b-41d4-a716-446655440000
Symfony's UID component also supports other identifier formats and UUID versions.
Generate a UUID v7 in PHP
If your installed Symfony UID version supports UUID v7:
<?php
use Symfony\Component\Uid\Uuid;
$uuid = Uuid::v7();
echo $uuid;
UUID v7 can be useful when you want identifiers that contain timestamp information and have time-oriented ordering characteristics.
Store a UUID as a String
You can convert the UUID to a string:
$uuidString = (string) Uuid::v4();
echo $uuidString;
This is useful when passing the identifier to a database layer, API, or other application component.
Generate a UUID in Java
Java provides UUID generation through the java.util.UUID class.
Generate UUID v4
The simplest approach is:
import java.util.UUID;
public class Main {
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
System.out.println(uuid);
}
}
Example output:
550e8400-e29b-41d4-a716-446655440000
UUID.randomUUID() is the standard Java approach for generating a random UUID.
Convert UUID to a String
If you need the UUID as a string:
String uuid = UUID.randomUUID().toString();
System.out.println(uuid);
Example:
550e8400-e29b-41d4-a716-446655440000
Generate Multiple UUIDs in Java
import java.util.UUID;
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
System.out.println(UUID.randomUUID());
}
}
}
This generates ten UUIDs.
Generate a UUID in C#
C# and .NET provide UUID functionality through the System.Guid structure.
Although .NET commonly calls these values GUIDs, they use the same general 128-bit identifier concept.
Generate a GUID
using System;
class Program
{
static void Main()
{
Guid id = Guid.NewGuid();
Console.WriteLine(id);
}
}
Example output:
550e8400-e29b-41d4-a716-446655440000
Convert GUID to String
string id = Guid.NewGuid().ToString();
Console.WriteLine(id);
Generate Multiple GUIDs
using System;
class Program
{
static void Main()
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(Guid.NewGuid());
}
}
}
This creates ten GUID values.
JavaScript vs Python vs PHP vs Java vs C#
Here's a quick comparison:
| Language | Common API | Typical UUID |
|---|---|---|
| JavaScript | crypto.randomUUID() |
v4 |
| Python | uuid.uuid4() |
v4 |
| PHP | Uuid::v4() |
v4 |
| Java | UUID.randomUUID() |
v4 |
| C# | Guid.NewGuid() |
GUID / random |
The exact UUID versions and APIs available can depend on the runtime, framework, library, and version you are using.
Which UUID Version Should You Use?
The UUID version you choose should depend on what your application needs.
UUID v4
Use UUID v4 when you primarily need a random identifier.
Example:
550e8400-e29b-41d4-a716-446655440000
Good for:
-
Database records
-
API resources
-
Object identifiers
-
Distributed applications
UUID v7
Consider UUID v7 when time ordering is useful.
UUID v7 combines a timestamp component with random data, making it particularly interesting for modern applications where identifiers may benefit from chronological ordering.
Good use cases can include:
-
Database records
-
Event systems
-
Distributed applications
-
High-volume data generation
-
Time-ordered identifiers
UUID Generation Best Practices
Generating a UUID is easy, but there are several things developers should keep in mind.
1. Use a Trusted Implementation
Don't create your own UUID algorithm unless you have a specific reason.
Use your language's standard library or a well-maintained UUID library.
2. Use the Right UUID Version
Don't automatically use UUID v4 for every application.
If your system benefits from time ordering, consider UUID v7.
If deterministic identifiers are required from a namespace and name, consider an appropriate name-based UUID version.
3. Don't Treat UUIDs as Passwords
A UUID is primarily an identifier.
It should not automatically be considered a replacement for:
-
Passwords
-
API secrets
-
Encryption keys
-
Authentication credentials
Use security-specific primitives when you need a secret.
4. Store UUIDs Consistently
Choose one representation for your application and database.
Depending on your database, you may use:
Native UUID type
or:
Binary representation
or:
Text representation
The best choice depends on the database and application architecture.
5. Validate UUIDs When Accepting External Input
If your API receives a UUID from a client, validate the input before using it.
For example:
550e8400-e29b-41d4-a716-446655440000
can be checked for valid UUID syntax and, when required, an expected version.
You can use our UUID Validator to quickly check a UUID.
Generate a UUID Without Code
You don't always need to write code to create a UUID.
If you simply need a UUID for:
-
Testing
-
Development
-
Database records
-
API testing
-
Documentation
-
Sample data
you can generate one instantly using our online tool.
UUID Generator
You can also generate specific UUID versions:
Frequently Asked Questions
How do I generate a UUID in JavaScript?
Use the Web Crypto API:
const uuid = crypto.randomUUID();
For Node.js, the built-in crypto module can also provide randomUUID().
How do I generate a UUID in Python?
Import Python's built-in uuid module and call:
import uuid
uuid.uuid4()
How do I generate a UUID in PHP?
You can use a UUID library such as Symfony UID:
use Symfony\Component\Uid\Uuid;
$uuid = Uuid::v4();
How do I generate a UUID in Java?
Use Java's UUID class:
UUID.randomUUID();
How do I generate a UUID in C#?
Use the .NET Guid structure:
Guid.NewGuid();
Is UUID v4 completely random?
UUID v4 is based on random or pseudorandom data, with required bits reserved for the UUID version and variant. The quality of the random source matters.
Is UUID v7 better than UUID v4?
Not universally. UUID v4 is useful for random identifiers, while UUID v7 is useful when time ordering is desirable.
Can I generate UUIDs offline?
Yes. UUID generation can be performed locally using programming-language libraries and APIs. You don't need a central UUID registration service.
Are UUID and GUID the same?
They are closely related terms. UUID means Universally Unique Identifier, while GUID means Globally Unique Identifier. In many programming environments, the terms are used interchangeably.
Conclusion
Generating a UUID is straightforward in most modern programming languages.
The common approaches are:
JavaScript → crypto.randomUUID()
Python → uuid.uuid4()
PHP → Uuid::v4()
Java → UUID.randomUUID()
C# → Guid.NewGuid()
For a simple random identifier, UUID v4 is a widely used option. When time ordering is important, UUID v7 is a modern alternative worth considering.
Whether you're building an API, database, web application, distributed system, or testing workflow, UUIDs provide a convenient way to create identifiers without relying on a central sequence.
If you need a UUID immediately, use our free online UUID Generator to generate one instantly.