Last updated
What Is CUID2?
CUID2 (Collision-resistant Unique IDentifier v2) is a modern unique ID format designed for distributed systems. It improves on the original CUID by using SHA-3 hashing, a larger alphabet, and better entropy sources. CUID2 IDs are URL-safe, start with a letter (making them valid HTML IDs), and are designed to be secure against fingerprinting attacks.
CUID2 vs Other ID Formats
| Format | Length | Sortable | URL-safe | Secure |
|---|---|---|---|---|
| UUID v4 | 36 chars | No | No (hyphens) | Yes |
| UUID v7 | 36 chars | Yes (time-based) | No | Yes |
| ULID | 26 chars | Yes | Yes | Yes |
| NanoID | 21 chars | No | Yes | Yes |
| CUID2 | 24 chars | No | Yes | Yes |
| Snowflake | 18 chars | Yes | Yes | No (predictable) |
Generating CUID2
import { createId, isCuid } from '@paralleldrive/cuid2';
// Generate a CUID2
const id = createId();
console.log(id); // → 'clh3ik5yx0000356dcf3dn2wr'
// Validate a CUID2
console.log(isCuid(id)); // → true
// Custom length (default is 24)
import { init } from '@paralleldrive/cuid2';
const createShortId = init({ length: 10 });
console.log(createShortId()); // → 'clh3ik5yx0'
// Use as database primary key
const user = {
id: createId(),
name: 'Alice',
createdAt: new Date()
};