Skip to content

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

impure
cuid2(length...) → string

Generate a CUID2: a lowercase, letter-first, hash-based ID, 24 characters by default.

ParameterTypeDescription
length...numberFrom 2 to 32. Optional; defaults to 24.
cuid2()

→ "tz4a98xxat96iws9zmbrgj3a"

ksuid

impure
ksuid(timestamp...) → string

Generate a KSUID: a 27-character, time-ordered, base62 ID (Segment’s format).

ParameterTypeDescription
timestamp...stringRFC3339 time to embed instead of now. Optional.
ksuid()

→ "34iLqKcLmVGVZTQ3PjmCsPXxzmg"

nanoid

impure
nanoid(length...) → string

Generate a Nano ID: a URL-safe random string, 21 characters by default.

For another alphabet, use randstring(length, charset).

ParameterTypeDescription
length...numberOptional; defaults to 21.
nanoid()

→ "V1StGXR8_Z5jdHi6B-myT"

objectid

impure
objectid(timestamp...) → string

Generate a MongoDB ObjectId as a 24-character hex string.

The first 4 bytes are the Unix time in seconds; the rest are random.

ParameterTypeDescription
timestamp...stringRFC3339 time to embed instead of now. Optional.
objectid()

→ "68f9a1c27d3e4b0a91c2e5f4"

snowflake

impure
snowflake(node, epoch...) → string

Generate 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.

ParameterTypeDescription
node, epoch...numberNode ID (0–1023, default 0), then the epoch in Unix milliseconds. Optional.
snowflake()

→ "1981467329371627520"

snowflake(3, 1420070400000)

typeid

impure
typeid(prefix, timestamp...) → string

Generate a TypeID: a type prefix and a base32 v7 UUID, like user_01k8e5wygg4q8f3t6n2vj9zb7c.

ParameterTypeDescription
prefixstringLowercase letters and underscores, up to 63; empty for no prefix.
timestamp...stringRFC3339 time to embed instead of now. Optional.
typeid("user")

→ "user_01k8e5wygg4q8f3t6n2vj9zb7c"

ulid

impure
ulid(timestamp...) → string

Generate a ULID: a 26-character, time-ordered, Crockford base32 ID.

ParameterTypeDescription
timestamp...stringRFC3339 time to embed instead of now. Optional.
ulid()

→ "01K8E5WYGG4Q8F3T6N2VJ9ZB7C"

uuid

impure
uuid() → string

Generate 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

impure
uuidv7(timestamp...) → string

Generate 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.

ParameterTypeDescription
timestamp...stringRFC3339 time to embed instead of now. Optional.
uuidv7()

→ "019a1c5e-7a10-7c3e-9b1f-3d2a6c0e4f81"

uuidv7("2026-01-01T00:00:00Z")

xid

impure
xid(timestamp...) → string

Generate an xid: a 20-character, time-ordered, base32hex ID.

Uses rs/xid’s layout. The machine, process and counter fields are random.

ParameterTypeDescription
timestamp...stringRFC3339 time to embed instead of now. Optional.
xid()

→ "d3ve5ao8b4kn29gsq0jg"