Paywall
Gate UI features and API routes by plan using requirePlan and the PlanGate component.
Overview
Nebutra gates access with plan-based feature entitlements
(packages/commerce/billing/src/entitlements/), not a standalone plan-comparison helper:
| Mechanism | Where to use |
|---|---|
requireFeature(feature, quantity?) | Server-side: API gateway middleware (Hono) |
checkEntitlement / requireEntitlement | Server-side: anywhere else in backend code (Server Components, Server Actions, jobs) |
isPlanFeature(plan, feature) | A pure, static lookup against PLAN_FEATURES — no DB round-trip |
All three ultimately resolve against the tenant's plan (FREE < PRO < ENTERPRISE) and its
PLAN_FEATURES entry, cross-checked with usage limits via requireEntitlementUsage where a
feature also has a metered quota (FEATURE_METER_MAP in the gateway middleware maps ai.chat /
ai.embeddings / ai.images / ai.reasoning to the ai_tokens meter, api.access to
api_calls).
API route gating
Protect gateway routes with requireFeature as middleware:
import { requireFeature } from "@nebutra/billing"; // re-exported via the gateway middleware
app.post("/api/v1/ai/generate", requireFeature("ai.images", 1), async (c) => {
// ... handle the request
});An insufficient plan or exhausted usage quota returns a structured error (ENTITLEMENT_DENIED or
USAGE_LIMIT_EXCEEDED) that the gateway's error handler converts to a 403/429 JSON response.
Server-side checks elsewhere
Outside the gateway's Hono middleware chain, call checkEntitlement / requireEntitlement
directly:
import { requireEntitlement } from "@nebutra/billing";
import { getCurrentTenant } from "@nebutra/tenant";
import { redirect } from "next/navigation";
export default async function AnalyticsPage() {
const tenant = getCurrentTenant();
const result = await checkEntitlement(tenant.tenantId, "ai.chat");
if (!result.allowed) {
redirect("/billing/upgrade");
}
return <AnalyticsDashboard />;
}Client-side UI gating
@nebutra/billing does not ship a <PlanGate> React component. Gate UI by fetching the
entitlement result server-side (a Server Component, or a route that calls checkEntitlement) and
passing a plain boolean prop down — the same rule as any other server-derived authorization state:
never trust a client-only check for something that must actually be denied.
const { allowed } = await checkEntitlement(tenant.tenantId, "ai.chat");
return allowed ? <AdvancedAnalytics /> : <UpgradeBanner feature="Advanced Analytics" />;Upgrade redirect target
The /billing/upgrade route creates a subscription checkout session and redirects the user. It is pre-built and requires no configuration beyond the environment variables. Plan subscriptions still run on Stripe today (legacy, per ADR 2026-09-26) — that's separate from Creem, which handles one-time card purchases (cards worldwide, merchant of record) alongside WeChat Pay/Alipay for mainland China. See Payments Overview for the full provider pair.
If you want to customize the upgrade page (e.g., to show a plan comparison table before redirecting), edit:
apps/web/src/app/billing/upgrade/page.tsxShowing upgrade prompts vs hard blocks
Use the following heuristics to choose between a soft prompt and a hard block:
| Scenario | Recommendation |
|---|---|
| Feature is discoverable but gated (e.g., export button) | Soft prompt — render an upgrade CTA when checkEntitlement denies |
| Route only makes sense on the paid plan (e.g., SSO settings) | Hard redirect via requireEntitlement in the Server Component |
| Sensitive data that must never reach FREE users | Hard block on both the Server Component and the gateway route (requireFeature) |
| Feature preview / teaser | Render a disabled/locked version, still checked server-side |
Never rely on client-side plan checks alone. Always enforce access on the server using requireEntitlement (elsewhere) or requireFeature (gateway routes). A motivated user can bypass any client-side guard.
Related
How is this guide?
Last updated on