Skip to main content

checkEntitlement

Check if a customer has access to a specific feature. Use this in API routes to enforce feature gates server-side.
const result = await billing.checkEntitlement("user_123", "api_calls");

if (!result.has_access) {
  return Response.json({ error: "Upgrade required" }, { status: 403 });
}

// For usage-based features, check the limit
if (result.usage !== null && result.limit !== null) {
  if (result.usage >= result.limit) {
    return Response.json({ error: "Usage limit exceeded" }, { status: 429 });
  }
}

Parameters

NameTypeDescription
customerIdstringCustomer external user ID
featureKeystringFeature key to check

Response

interface EntitlementResponse {
  feature_key: string
  has_access: boolean
  reason?: string          // Reason if access denied
  limit: number | null     // null for boolean features
  usage: number | null     // null for boolean features
  metadata: {
    remaining?: number
    resets_at?: string
    type?: string
  } | null
}

Example: API middleware

// middleware/entitlement.ts
import { BillingOS } from "@billingos/node";

const billing = new BillingOS({ secretKey: process.env.BILLINGOS_SECRET_KEY! });

export async function checkFeatureAccess(userId: string, feature: string) {
  const entitlement = await billing.checkEntitlement(userId, feature);

  if (!entitlement.has_access) {
    throw new Error(`Feature "${feature}" requires a plan upgrade`);
  }

  return entitlement;
}
// In your API route
const entitlement = await checkFeatureAccess(userId, "api_calls");
// Proceed with the operation...
Always enforce entitlements server-side for security-critical features. Client-side FeatureGate components improve UX but can be bypassed by determined users.