NewSortable IDs are here
prefID
Menu

id()

The default generator. Takes a prefix and returns a secure, type-safe prefixed ID.

Signature

ts
function id<P extends string>(prefix: P): `${P}_${string}`

Usage

Call id with a prefix string. The default output has 24 random base62 characters after an underscore separator.

import { id } from "prefid";

id("user");    // => "user_a8Kd0f2bQ1nR7pZ3xW4mT6y"
id("order");   // => "order_9f8e7d6c5b4a3F2e1D0cB9aX"
id("invoice"); // => "invoice_2xR7pZ3xW4mT6yQ1nR7pZ3x"
const { id } = require("prefid");

id("user");    // => "user_a8Kd0f2bQ1nR7pZ3xW4mT6y"
id("order");   // => "order_9f8e7d6c5b4a3F2e1D0cB9aX"
id("invoice"); // => "invoice_2xR7pZ3xW4mT6yQ1nR7pZ3x"

Parameters

  • prefix — a non-empty string that must not contain the separator (_ by default). This is preserved in the return type.

Returns

A string typed as `${prefix}_${string}` — for example id("user") returns the type `user_${string}`.

Errors

id throws a TypeError when the prefix is invalid, so problems surface immediately instead of producing a malformed ID.

ts
id("");      // ❌ TypeError: prefix must be a non-empty string
id("us_er"); // ❌ TypeError: prefix must not contain the separator "_"

Customizing the output

To change the length, separator, or alphabet, create a configured generator with createId().