Deno 2.x ships crypto.randomUUIDv7() as a built-in extension to the Web Crypto API. No import statement, no npm package, no JSR dependency — the function is available globally in every Deno 2.x script. This makes UUID v7 generation in Deno as ergonomic as Math.random(), while producing identifiers that are time-sortable, RFC 9562-compliant, and safe to use as database primary keys.
Basic usage
// No import needed in Deno 2.x
const id = crypto.randomUUIDv7();
console.log(id);
// "019236a7-b4f2-7000-8d3e-9c1a2b3d4e5f"
// Batch generation
const ids = Array.from({ length: 5 }, () => crypto.randomUUIDv7());
console.log(ids);
// The values are already in sorted order because each one has a
// millisecond timestamp prefix — newer values sort after older ones.
The return value is a lowercase hyphenated 36-character string conforming to RFC 9562 §5.7. The first 12 hex digits encode 48 bits of Unix timestamp in milliseconds, making these values sort chronologically when compared as strings or stored in a B-tree index.
Using UUID v7 in a Deno HTTP server
import { serve } from "https://deno.land/std@0.224.0/http/server.ts";
serve(async (req: Request): Promise<Response> => {
const requestId = crypto.randomUUIDv7();
const body = {
requestId,
timestamp: new Date().toISOString(),
path: new URL(req.url).pathname,
};
return new Response(JSON.stringify(body), {
headers: {
"Content-Type": "application/json",
"X-Request-Id": requestId,
},
});
}, { port: 8000 });
The requestId is generated at the top of each request handler. Because UUID v7 values are monotonically increasing within the same millisecond (the runtime uses a sub-millisecond counter per RFC 9562 §6.2 Method 1), request IDs from the same server also sort in arrival order — useful for log correlation.
Deno KV integration
Deno KV — the built-in key-value store available in Deno Deploy and local Deno 2.x — benefits directly from UUID v7 keys. KV stores entries in lexicographic key order, so UUID v7 primary keys naturally give you chronological iteration:
const kv = await Deno.openKv();
async function createEvent(payload: Record<string, unknown>) {
const id = crypto.randomUUIDv7();
await kv.set(["events", id], { id, ...payload, createdAt: Date.now() });
return id;
}
async function listRecentEvents(limit = 20) {
const entries = kv.list<{ id: string }>({ prefix: ["events"] }, {
limit,
reverse: true, // newest first
});
const results = [];
for await (const entry of entries) {
results.push(entry.value);
}
return results;
}
With UUID v4 keys, listRecentEvents with reverse: true would return the events in reverse-alphabetical UUID order — which has no meaningful relationship to creation time. With UUID v7 keys, it returns the most recently created events first. No secondary index, no createdAt sort — the key itself encodes the ordering. This mirrors the same benefit UUID v7 brings to relational database B-tree indexes.
Comparing Deno UUID options
Before crypto.randomUUIDv7() was built in, the options for UUID v7 in Deno were:
| Method | Import required | UUID version | Time-sortable |
|---|---|---|---|
crypto.randomUUID() | No | v4 | No |
crypto.randomUUIDv7() | No | v7 | Yes |
@std/uuid generate() | jsr:@std/uuid | v1, v4, v5 | v1 only |
npm:uuid v7() | npm:uuid | v7 | Yes |
The built-in crypto.randomUUIDv7() is the correct default for any new code in Deno 2.x. The @std/uuid module and npm:uuid remain useful for UUID v1, v3, v5, or for code that needs to run in environments without the built-in API.
Extracting the timestamp from a UUID v7
One advantage of UUID v7 over UUID v4 is that the creation time can be recovered from the UUID itself:
function extractTimestamp(uuidv7: string): Date {
// Remove hyphens, take first 12 hex chars = 48-bit timestamp
const hex = uuidv7.replace(/-/g, "").slice(0, 12);
const ms = parseInt(hex, 16);
return new Date(ms);
}
const id = crypto.randomUUIDv7();
const created = extractTimestamp(id);
console.log(`UUID created at: ${created.toISOString()}`);
This is useful for log analysis, debugging, and building time-based range queries without storing a separate timestamp column. The precision is 1 millisecond — sufficient for most use cases. For higher precision, RFC 9562 §5.7 defines additional sub-millisecond bits in the rand_a field, though these are not exposed directly by the built-in API.
Migration from @std/uuid or npm:uuid
If your Deno project already generates UUID v7 via jsr:@std/uuid or npm:uuid, you can migrate incrementally:
// Before — using npm:uuid
import { v7 as uuidv7 } from "npm:uuid";
const id = uuidv7();
// After — built-in, no import needed
const id = crypto.randomUUIDv7();
The output format is identical. Both return a lowercase hyphenated 36-character string. Remove the import line and the dependency from deno.json if it is no longer needed for other UUID versions.
Further reading
- UUID v7 specification and generator — RFC 9562 §5.7 breakdown and in-browser UUID v7 generation
- UUID in JavaScript — uuid npm package, Node.js crypto module, Cloudflare Workers, and Deno patterns
- Entropy in UUID generation — how randomness is sourced and why it matters for uniqueness guarantees
- UUID validator — verify that a UUID string is well-formed and identify its version
External references
- Deno Web Crypto API docs: official Deno documentation for the Crypto interface including randomUUID() and randomUUIDv7()
- Deno 2.0 release blog post: the Deno 2.0 announcement covering runtime API additions including Web Crypto extensions
- jsr:@std/uuid: the Deno standard library UUID module — still useful for v1, v3, and v5 generation
- github.com/denoland/deno: Deno runtime source — the crypto.randomUUIDv7() implementation
Frequently asked questions
Is crypto.randomUUIDv7() available in Deno Deploy?
Yes. Deno Deploy runs the same V8-based runtime as local Deno 2.x, so crypto.randomUUIDv7() is available without any configuration. There is no compatibility flag or permission flag required.
Does @std/uuid in the Deno standard library still work?
Yes. The @std/uuid module continues to export generate() and other utilities for UUID v1, v3, v4, and v5. For UUID v7 specifically, crypto.randomUUIDv7() is the recommended approach in Deno 2.x — it is faster, requires no import, and uses the same cryptographic entropy source as the rest of the Web Crypto API.
What is the difference between crypto.randomUUID() and crypto.randomUUIDv7() in Deno?
crypto.randomUUID() generates a version 4 UUID — 122 bits of random data with no timestamp. crypto.randomUUIDv7() generates a version 7 UUID whose first 48 bits are a Unix millisecond timestamp, making values sort chronologically. See UUID v7 for the full specification breakdown.
How do I validate a UUID v7 value generated in Deno?
Use the UUID validator tool to confirm the version bits (should be 7) and variant bits (should be 10xx). You can also check programmatically in Deno by parsing the version nibble from the 13th character of the UUID string.