Identity
Generate unique IDs in the common formats. The time-ordered ones take an optional timestamp, to match a backdated created_at.
Random values follow the run’s seed, so a run can be repeated; see random values and seeds.
cuid2
cuid2(length...) → stringGenerate a CUID2: a lowercase, letter-first, hash-based ID, 24 characters by default.
| Parameter | Type | Description |
|---|---|---|
length... | number | From 2 to 32. Optional; defaults to 24. |
cuid2()→ "tz4a98xxat96iws9zmbrgj3a"
ksuid
ksuid(timestamp...) → stringGenerate a KSUID: a 27-character, time-ordered, base62 ID (Segment’s format).
| Parameter | Type | Description |
|---|---|---|
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
ksuid()→ "34iLqKcLmVGVZTQ3PjmCsPXxzmg"
nanoid
nanoid(length...) → stringGenerate a Nano ID: a URL-safe random string, 21 characters by default.
For another alphabet, use randstring(length, charset).
| Parameter | Type | Description |
|---|---|---|
length... | number | Optional; defaults to 21. |
nanoid()→ "V1StGXR8_Z5jdHi6B-myT"
objectid
objectid(timestamp...) → stringGenerate a MongoDB ObjectId as a 24-character hex string.
The first 4 bytes are the Unix time in seconds; the rest are random.
| Parameter | Type | Description |
|---|---|---|
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
objectid()→ "68f9a1c27d3e4b0a91c2e5f4"
snowflake
snowflake(node, epoch...) → stringGenerate a 64-bit Snowflake ID as a decimal string.
41 bits of milliseconds since epoch, a 10-bit node, and a 12-bit sequence, as Twitter’s IDs. epoch defaults to Twitter’s (1288834974657, in Unix milliseconds); Discord’s is 1420070400000. IDs from one orchard process are unique and increasing. It’s a string because JSON and JavaScript can’t hold 64-bit integers exactly; databases convert it on insert.
| Parameter | Type | Description |
|---|---|---|
node, epoch... | number | Node ID (0–1023, default 0), then the epoch in Unix milliseconds. Optional. |
snowflake()→ "1981467329371627520"
snowflake(3, 1420070400000)typeid
typeid(prefix, timestamp...) → stringGenerate a TypeID: a type prefix and a base32 v7 UUID, like user_01k8e5wygg4q8f3t6n2vj9zb7c.
| Parameter | Type | Description |
|---|---|---|
prefix | string | Lowercase letters and underscores, up to 63; empty for no prefix. |
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
typeid("user")→ "user_01k8e5wygg4q8f3t6n2vj9zb7c"
ulid
ulid(timestamp...) → stringGenerate a ULID: a 26-character, time-ordered, Crockford base32 ID.
| Parameter | Type | Description |
|---|---|---|
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
ulid()→ "01K8E5WYGG4Q8F3T6N2VJ9ZB7C"
uuid
uuid() → stringGenerate a random v4 UUID string.
Each call produces a new value. Use for primary keys, correlation IDs, or any field that needs a unique identifier.
uuid()→ "a3f29e5b-1234-4abc-9def-0123456789ab"
uuidv7
uuidv7(timestamp...) → stringGenerate a time-ordered v7 UUID string.
The first 48 bits are the Unix time in milliseconds, so v7 UUIDs sort by creation time. Pass a timestamp to backdate one to match a created_at.
| Parameter | Type | Description |
|---|---|---|
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
uuidv7()→ "019a1c5e-7a10-7c3e-9b1f-3d2a6c0e4f81"
uuidv7("2026-01-01T00:00:00Z")xid
xid(timestamp...) → stringGenerate an xid: a 20-character, time-ordered, base32hex ID.
Uses rs/xid’s layout. The machine, process and counter fields are random.
| Parameter | Type | Description |
|---|---|---|
timestamp... | string | RFC3339 time to embed instead of now. Optional. |
xid()→ "d3ve5ao8b4kn29gsq0jg"