Skip to main content

Guides

Feature Gating

Control access to individual features using entitlements attached to licenses and policies.

How Entitlements Work

Entitlements are named features (like "export" or "api_access") that you create and attach to policies or individual licenses. During validation, you can require specific entitlements — if they are missing, the API returns ENTITLEMENTS_MISSING.

1. Create Entitlements

Define the features you want to gate. Each entitlement has a human-readable name and a machine-readable code.

create_entitlements.py
from licentric import Licentric

client = Licentric(api_key="lk_live_your_key_here")

# Create entitlements for your product
client.entitlements.create(
    name="Export",
    code="export",
    description="Export data to CSV, JSON, and PDF"
)

client.entitlements.create(
    name="Premium Analytics",
    code="premium_analytics",
    description="Advanced charts, reports, and dashboards"
)

client.entitlements.create(
    name="API Access",
    code="api_access",
    description="Programmatic access via REST API"
)

2. Attach to Policies or Licenses

Attach entitlements at the policy level (all licenses under that policy inherit them) or at the individual license level for fine-grained control.

attach.py
# Attach entitlements to a policy (all licenses inherit them)
client.policies.attach_entitlements(
    policy_id="pol_abc123",
    entitlement_codes=["export", "premium_analytics"]
)

# Or attach to a specific license (overrides policy)
client.licenses.attach_entitlements(
    license_id="lic_xyz789",
    entitlement_codes=["export", "premium_analytics", "api_access"]
)
Policy-level vs license-level
Use policy-level entitlements for subscription tiers (e.g., all "Growth" licenses get analytics). Use license-level entitlements for one-off feature grants or custom enterprise agreements.

3. Validate with Entitlement Check

Pass the required entitlement codes to the validate call. The API checks the license is valid and that all required entitlements are present.

Python

validate.py
# Validate and check for specific entitlements
result = client.validate(
    key="DSK-XXXX-XXXX-XXXX-XXXX",
    fingerprint=client.fingerprint(),
    entitlements=["export"]  # Require "export" entitlement
)

if result.valid:
    # License is valid AND has the "export" entitlement
    run_export()
elif result.code == "ENTITLEMENTS_MISSING":
    print("Upgrade to access the export feature.")
else:
    print(f"Validation failed: {result.code}")

TypeScript

validate.ts
const result = await client.validate({
  key: "DSK-XXXX-XXXX-XXXX-XXXX",
  fingerprint: client.fingerprint(),
  entitlements: ["export"], // Require "export" entitlement
});

if (result.valid) {
  runExport();
} else if (result.code === "ENTITLEMENTS_MISSING") {
  showUpgradePrompt("Upgrade to access the export feature.");
}

4. Implement Feature Gates

Build a reusable feature gate function that wraps validation with an entitlement check. Use it throughout your application to conditionally enable features.

Python

feature_gate.py
def feature_gate(license_key: str, required_feature: str):
    """Gate a feature behind an entitlement check."""
    result = client.validate(
        key=license_key,
        fingerprint=client.fingerprint()
    )

    if not result.valid:
        return False

    # Check entitlements from the validation response
    return required_feature in result.entitlements

# Usage in your application
if feature_gate(user.license_key, "premium_analytics"):
    show_analytics_dashboard()
else:
    show_upgrade_banner("Unlock analytics with a premium license")

TypeScript

feature-gate.ts
async function featureGate(
  licenseKey: string,
  requiredFeature: string
): Promise<boolean> {
  const result = await client.validate({
    key: licenseKey,
    fingerprint: client.fingerprint(),
  });

  if (!result.valid) {
    return false;
  }

  return result.entitlements.includes(requiredFeature);
}

// Usage in your application
if (await featureGate(user.licenseKey, "premium_analytics")) {
  showAnalyticsDashboard();
} else {
  showUpgradeBanner();
}
Entitlements in offline mode
Offline license files include the full entitlements array in their payload. Feature gates work identically in offline mode — the SDK reads entitlements from the signed certificate.