Every language does UUIDs slightly differently. Some ship it in the standard library. Some need a package. Here is the working code for five languages, with the gotchas that bite people.
Node.js
Use the crypto module — it is built in, no dependency needed.
javascript const crypto = require('crypto');
// v4 (random) const id = crypto.randomUUID(); // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
// v5 (namespace + name) const { randomUUID } = crypto; // Node 19+ supports crypto.hash() but for v5 you need a helper or the uuid package
For v5 in Node, the uuid npm package is still the cleanest path:
javascript const { v5, v4 } = require('uuid'); const dnsNamespace = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; const id = v5('example.com', dnsNamespace); // '74738ff5-5367-5dab-9e9f-3a2b1c8e4d6a'
crypto.randomUUID() uses the OS CSPRNG. It is safe for tokens. Do not use Math.random() to build UUIDs — the output is predictable.
Python
Python 3.6+ has uuid in the standard library.
python import uuid
# v4 id4 = uuid.uuid4() # UUID('f47ac10b-58cc-4372-a567-0e02b2c3d479')
# v1 id1 = uuid.uuid1() # UUID('63b4c2a0-1f3e-11ec-9f3a-3c8e2d5a1b7f')
# v5 (namespace + name) dns_ns = uuid.NAMESPACE_DNS id5 = uuid.uuid5(dns_ns, 'example.com') # UUID('74738ff5-5367-5dab-9e9f-3a2b1c8e4d6a')
uuid4() uses os.urandom() under the hood on most platforms, which is cryptographically secure. Good.
Java
Java has java.util.UUID.
java import java.util.UUID;
// v4 UUID id4 = UUID.randomUUID(); // f47ac10b-58cc-4372-a567-0e02b2c3d479
// v3 (name-based, MD5) — v5 is not in the standard library UUID id3 = UUID.nameUUIDFromBytes("example.com".getBytes());
Java's standard library only gives you v4 and v3. For v1 or v5, use the com.fasterxml.uuid package or java-uuid-generator from Amazon. This trips up teams moving from Python or Node, where v5 is one call away.
PHP
PHP has uniqid(), but it is not a UUID. Use the ramsey/uuid Composer package.
php use Ramsey\Uuid\Uuid;
// v4 $id4 = Uuid::uuid4(); // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
// v5 $dnsNs = Uuid::fromString('6ba7b810-9dad-11d1-80b4-00c04fd430c8'); $id5 = Uuid::uuid5($dnsNs, 'example.com'); // '74738ff5-5367-5dab-9e9f-3a2b1c8e4d6a'
Do not build UUIDs by string concatenation in PHP. The format checks in ramsey/uuid catch version and variant bits that hand-rolled code gets wrong.
C#
C# calls them GUIDs. The System.Guid struct generates v4 UUIDs.
csharp using System;
Guid id = Guid.NewGuid(); // f47ac10b-58cc-4372-a567-0e02b2c3d479
string str = id.ToString(); // 'f47ac10b-58cc-4372-a567-0e02b2c3d479'
Guid.NewGuid() uses CryptGenRandom on Windows and /dev/urandom on .NET Core/Linux. Secure. For v5 in C#, you need a third-party library or a manual SHA-1 implementation following RFC 4122 section 4.3.
The Common Mistake Across All Languages
People format UUIDs inconsistently. The canonical form is lowercase with hyphens: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx. Some databases uppercase them. Some systems strip hyphens. Pick one format at the boundary and normalize there. Store lowercase with hyphens internally.
Quick Reference
| Language | v4 | v5 | Standard Library? | |----------|----|----|--------------------| | Node.js | crypto.randomUUID() | uuid package | v4 yes, v5 no | | Python | uuid.uuid4() | uuid.uuid5() | Yes | | Java | UUID.randomUUID() | third-party | v4 yes, v5 no | | PHP | Ramsey\Uuid::uuid4() | Ramsey\Uuid::uuid5() | No (use ramsey/uuid) | | C# | Guid.NewGuid() | third-party | v4 yes, v5 no |
If you just need a UUID fast without writing code, use our online UUID generator. For choosing between versions, read UUID v1 vs v4 vs v5. For generating many at once, see bulk UUID generation.