How to Generate UUIDs in TypeScript

Generate UUIDs in TypeScript with native runtime support for v4 and a package-based approach for v7 when you need sortable identifiers.

Recommendation: keep UUID generation tied to your runtime capabilities. Use native v4 where possible, and standardize on one package for v7 across your codebase.

How to generate UUID v4 in TypeScript

const id: string = crypto.randomUUID();
console.log(id);

How to generate UUID v7 in TypeScript

// npm install uuid
import { v7 as uuidv7 } from 'uuid';

const id: string = uuidv7();
console.log(id);

Native support notes

  • TypeScript adds types, not new UUID generation primitives.
  • UUID v4 support depends on browser or Node runtime support.
  • UUID v7 is typically handled through the uuid package.

Practical notes

  • Keep server and browser UUID generation aligned if code is shared across environments.
  • Prefer one package import style across ESM and CommonJS boundaries.
  • Compare v4 vs v7 before locking in a storage strategy.

Frequently Asked Questions

TypeScript does not add new UUID APIs. It uses the runtime's JavaScript capabilities and libraries.

Use runtime crypto for v4 when supported. Use the uuid package when you need v7 or a consistent cross-runtime API.

Learn more

These articles expand on related concepts, formats and practical considerations.

By using this site, you agree to our Privacy Policy and Terms of Service. You are not permitted to use the GUIDs (also known as UUIDs) generated by this site or use any other content, services and information available if you do not agree to these terms.
Disclaimer: All information is provided for general educational and technical reference only. While we aim to keep the content accurate, current and aligned with published standards, no guarantees are made regarding completeness, correctness or suitability for any specific use case.
GUID specifications, best practices, security guidance, database behavior and ecosystem conventions (including cloud platforms and identifier formats) may change over time or differ by implementation. Examples, recommendations and comparisons are illustrative and may not apply universally.
This content should not be considered legal, security, compliance or architectural advice. Before making critical design, security or production decisions, always consult the latest official standards and vendor-specific documentation.
Always evaluate behavior in your own environment.
Standards Compliance: The GUIDs generated by this site conform to RFC 4122 and RFC 9562 specifications whenever possible, using cryptographically secure random number generation.