Quick Start
Generate your first prefixed ID, then see how the prefix flows through TypeScript.
Generate an ID
Import id and call it with a prefix. That's the whole API for the common case.
import { id } from "prefid";
const userId = id("user");
// => "user_a8Kd0f2bQ1nR7pZ3xW4mT6y"
const orderId = id("order");
// => "order_9f8e7d6c5b4a3F2e1D0cB9aX"const { id } = require("prefid");
const userId = id("user");
// => "user_a8Kd0f2bQ1nR7pZ3xW4mT6y"
const orderId = id("order");
// => "order_9f8e7d6c5b4a3F2e1D0cB9aX"The prefix is type-safe
The return value isn't just string — it's a template literal type that remembers the prefix. This is what stops you from mixing up ID types.
ts
const userId = id("user");
// ^? type: `user_${string}`
function getUser(id: `user_${string}`) { /* ... */ }
getUser(userId); // ✅ ok
getUser(id("order")); // ❌ compile error — that's an order idConfigure defaults
Use createId to set the size, separator, or alphabet once and reuse the generator across your app.
import { createId } from "prefid";
// Configure once, reuse everywhere.
const id = createId({ size: 16 });
id("user"); // => "user_a1b2c3d4e5f6g7h8"const { createId } = require("prefid");
// Configure once, reuse everywhere.
const id = createId({ size: 16 });
id("user"); // => "user_a1b2c3d4e5f6g7h8"What's next?
id()— the full reference for the default generator.ensureUnique()— guarantee an ID is free in your database.- Uniqueness & Collisions — how safe these IDs really are.