Skip to content

Quickstart

Terminal window
bun add @xtandard/entitlements # or npm i / pnpm add
// catalog.ts — your pricing model, importable anywhere (zero dependencies)
import { defineCatalog, feature } from "@xtandard/entitlements/catalog";
export const catalog = defineCatalog({
features: {
seats: feature.capacity({ name: "Seats", unit: "seat" }),
api_calls: feature.metered({ name: "API calls", unit: "call", reset: "month" }),
export_pdf: feature.boolean({ name: "PDF export" }),
},
plans: {
free: { name: "Free", default: true, features: { seats: 3, api_calls: 1_000 } },
pro: {
name: "Pro",
trial: { days: 14 },
price: { amount: 2900, currency: "usd", interval: "month" },
providers: { stripe: ["price_pro_monthly"] },
features: { seats: 10, api_calls: { limit: 100_000, overage: "allow" }, export_pdf: true },
},
},
});
server.ts
import { createEntitlements } from "@xtandard/entitlements";
import { createRedisStorage } from "@xtandard/entitlements/storage/redis";
import { createRedisCounterStore } from "@xtandard/entitlements/counters/redis";
import { catalog } from "./catalog";
export const ent = createEntitlements({
catalog,
storage: createRedisStorage({ url: process.env.REDIS_URL }),
counters: createRedisCounterStore({ url: process.env.REDIS_URL }),
});
// In a handler:
const decision = await ent.consume(org.id, "api_calls");
if (!decision.allowed) {
return Response.json(decision, { status: decision.denial === "payment_required" ? 402 : 403 });
}

Mount the admin UI, the customer portal and billing webhooks in your app with one line per framework (see Admin UI and customer portal), or run the standalone server.

Five feature kinds — boolean, metered (resets per day / week / month / billing period), credits (a balance other features draw from), capacity (seats, projects) and value (max upload size, support tier) — on plans with groups, defaults, trials and versions (grandfathered subscribers keep the limits they bought), plus stackable add-ons. Feature and plan keys are inferred, so ent.check(org, "exprot_pdf") is a type error and value features return typed values. pricingTable(catalog) renders the same catalog for your pricing page. → Catalog guide

await ent.check(org, "export_pdf"); // { allowed, reason: "included" } — zero I/O when cached
await ent.consume(org, "api_calls", 5, { idempotencyKey: req.id }); // atomic; refusals never count
await ent.consume(org, "seats"); // capacity: one more seat if it fits
await ent.release(org, "seats"); // a seat freed
// Work whose cost is known afterwards (LLM tokens):
const r = await ent.reserve(org, "gpt_tokens", 8_000, { ttl: "5m" });
await ent.settle(r.reservation!.id, usage.totalTokens);
// Operators:
await ent.grant(org, {
feature: "api_calls",
amount: 1_000,
expiresAt: "2026-12-31",
reason: "outage credit",
});
await ent.override(org, { feature: "export_pdf", enabled: true, reason: "pilot" });
await ent.startTrial(org, "pro");

Decisions carry reason, denial (payment_required → 402, forbidden → 403), unlockedBy (cheapest plans first), limit / used / remaining / resetsAt. Usage is drawn from the period allowance, then one-off grants by priority, then overage; onThreshold fires once per period at 80 % and 100 %. The hot path reads a memory-first snapshot with last-known-good fallback, so gates keep working when storage blips. → Metering guide