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.
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 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"]
)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 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
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
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
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();
}