How to Generate UUID v7 in JavaScript, Python, .NET, Go and SQL
By SimpleTools24Published 3 min read
To generate a UUID v7, use your platform's built-in function where one exists (Guid.CreateVersion7() in .NET 9+, uuid.uuid7() in Python 3.14+, uuidv7() in PostgreSQL 18) or a well-maintained library elsewhere, such as the uuid package's v7() in JavaScript and NewV7() in Go's google/uuid. Each returns a standard UUID whose first 48 bits are the current Unix time in milliseconds, so values sort roughly by creation time.
In this guide
Quick Reference
| Platform | Code | Notes |
|---|---|---|
| JavaScript / Node.js | v7() from the uuid package | crypto.randomUUID() only makes v4 |
| Python 3.14+ | uuid.uuid7() | Earlier versions need a package |
| .NET 9+ | Guid.CreateVersion7() | Optional timestamp argument |
| Go | uuid.NewV7() from github.com/google/uuid | Returns a value and an error |
| PostgreSQL 18+ | uuidv7() | Optional shift interval |
| Java | Third-party library | Standard library has v4 and v3 only |
JavaScript and TypeScript
npm install uuidimport { v7 as uuidv7 } from 'uuid';
const id = uuidv7(); // e.g. '0192f0c1-6a4b-7c3d-9e2f-0a1b2c3d4e5f'Browsers' and Node's built-in crypto.randomUUID() produces version 4 only. How to generate a UUID in JavaScript covers v4 and the rest of the uuid package.
Python
import uuid
u = uuid.uuid7() # Python 3.14+
print(str(u), u.version)On Python 3.13 and earlier, uuid doesn't include uuid7(); use a third-party package or upgrade. How to generate a UUID in Python covers conversions and framework defaults.
C# and .NET
Guid id = Guid.CreateVersion7(); // current UTC time
Guid at = Guid.CreateVersion7(DateTimeOffset.UtcNow); // explicit timestampAvailable from .NET 9. When storing these in SQL Server uniqueidentifier columns, remember SQL Server's unusual sort order, covered in GUID as a primary key.
Go
import "github.com/google/uuid"
id, err := uuid.NewV7()
if err != nil {
// handle error
}
fmt.Println(id.String())PostgreSQL
SELECT uuidv7();
CREATE TABLE events (
id uuid PRIMARY KEY DEFAULT uuidv7(),
payload jsonb
);Available from PostgreSQL 18. UUID v7 in PostgreSQL covers extracting timestamps and older versions.
Java
Java's java.util.UUID can parse and store any version, but its built-in generators are randomUUID() (v4) and nameUUIDFromBytes() (v3). For v7, use a maintained third-party library and check it against RFC 9562. Store the result as a UUID object, and in databases as a native UUID type.
What to Check in a v7 Library
RFC 9562 leaves some choices to implementations, so libraries differ. Check:
- RFC 9562 compliance: correct version (
7) and variant bits. - Random source: a cryptographically secure generator for the random bits.
- Monotonicity: whether values from one generator increase within the same millisecond (via a counter or sub-millisecond precision), if your use needs that.
- Clock handling: what happens if the system clock moves backwards.
- Maintenance: recent releases and tests.
Verifying Your Output
Whatever you generate, a quick sanity check catches mistakes:
- The 13th hex digit should be
7. - The 17th should be
8,9,aorb. - The first 12 hex digits, read as a millisecond timestamp, should be close to "now".
The UUID decoder shows all three: version, variant and the embedded timestamp. The UUID validator checks hundreds of values at once.
Without Writing Code
For fixtures, documentation or manual testing, the UUID v7 generator creates up to 1,000 values in your browser and can wrap them in quotes or output them as a JSON array, ready to paste into code. It uses the Web Crypto API for the random bits.
Testing Code That Uses UUID v7
Time-based IDs make tests harder to write deterministically. Approaches:
- Inject the generator: pass a function or interface that creates IDs, and use a fixed implementation in tests.
- Freeze time where your library reads the clock, if it supports a time provider (for example
Guid.CreateVersion7(timestamp)in .NET). - Assert on shape, not value: check the version digit and that values from one generator increase, rather than comparing exact strings.
Migrating an Existing System
- Switch the generator for new records to v7.
- Update any validation that only accepts v4.
- Leave existing IDs unchanged; they remain valid UUIDs.
- If you sort by ID anywhere, remember that old v4 rows won't be in time order.
UUID v4 vs UUID v7 covers the trade-offs behind the switch.
Frequently Asked Questions
Does crypto.randomUUID() support v7?
No. It only generates version 4. Use the uuid package for v7 in JavaScript.
Can I generate UUID v7 in older Python versions?
Not with the standard library before 3.14. Use a third-party package.
Are UUID v7 values from different libraries compatible?
Yes. They're all standard 128-bit UUIDs. Ordering details within a millisecond may differ.
Should I generate v7 in the app or the database?
Either. Database generation is simple; application generation lets you know the ID before inserting.
Summary
Use built-ins where available (.NET 9, Python 3.14, PostgreSQL 18) and trusted libraries elsewhere. Check the version digit, variant and timestamp to confirm your setup produces real v7 UUIDs.
Related tools
Sources
- RFC 9562 – UUID Version 7 – IETF (opens in a new tab)
- uuid (npm package) – uuidjs (opens in a new tab)
- uuid — UUID objects according to RFC 9562 – Python documentation (opens in a new tab)
- Guid.CreateVersion7 Method – Microsoft Learn (opens in a new tab)
Related articles
- What Is UUID v7?The layout of a version 7 UUID, how its timestamp makes IDs sortable, and the limits of that ordering.
- UUID v7 in PostgreSQLuuidv7() in PostgreSQL 18, extracting timestamps, using v7 on older versions and migrating from gen_random_uuid().
- Generate a UUID in JavaScriptcrypto.randomUUID() in browsers and Node.js, the uuid package for other versions, and why Math.random() snippets are a bad idea.