Chargebee provides the feature
resource to help you define various features offered as part of your product line. This tutorial explains how you can check if a user of your system is entitled to a feature and then provide them access to it. This is achieved by retrieving and checking the subscription_entitlement
object. The subscription_entitlement
object specifies the entitlement a specific subscription has towards a specific feature.
If you want more guides to help with other use cases for the Entitlements API, reach out to us at Chargebee Support.
Prerequisites
Start by ensuring that the following objects have been created in Chargebee:
feature
objects to model the features you offer your users.item_entitlements
towards those features.
It is recommended that you cache the subscription_entitlement
resource in your system to speed up the process of checking entitlements.
Implementation
Perform the following steps each time the user logs in and a session is created:
Based on your use case, the user in the diagram below could be a human being or even a device such as in IoT.
The following steps explain the sequence shown in the above diagram:
- Log the user in and check their
subscription.id
.- The user logs in and undergoes authorization and authentication with your application.
- Your application retrieves
subscription.id
of the user.
- Retrieve
subscription_entitlements
.- Your application attempts to retrieve the
subscription_entitlements
object from its cache. - If there is a cache miss, retrieve
subscription_entitlements
from Chargebee and store it in the cache.
curl https://{site}.chargebee.com/api/v2/subscriptions/{subscription_id}/subscription_entitlements \ -G \ -u {site_api_key}:
- You will get a list of all entitlements to the subscription:
{ "list": [ { "subscription_entitlement": { "id": "KyVnHhSBWlm1j2m7", "subscription_id": "JzDnHhSBWlm1j1n4", "feature_id": "salesforce-integration", "feature_name": "Salesforce integration", "value": "available", "name": "", "is_overridden": false } }, { "subscription_entitlement": { "id": "KyVnHhSBWlm1j2m7", "subscription_id": "JzDnHhSBWlm1j1n4", "feature_id": "number_of_users", "feature_name": "Number of users", "value": "10", "name": "", "is_overridden": false } } ] }
- Your application attempts to retrieve the
- Check subscription entitlements and allow/deny access to feature.
- Extract the JSON object corresponding to the
feature_id
from the list. - Verify whether the user’s role and permissions allow them to access or modify this feature.
- Compare the consumed entitlement level of the feature with
subscription_entitlements.value
and allow/deny the user access to the feature:- For
feature.type
switch
, allow access ifsubscription_entitlements.value
istrue
. - For
feature.type
quantity
orrange
, allow access whensubscription_entitlements.value
is eitherunlimited
or more than the consumed entitlements for the feature. - For
feature.type
custom
, allow access based on your specific interpretation ofsubscription_entitlements.value
.
- For
- Extract the JSON object corresponding to the
- It is recommended to define a time-to-live (TTL) attribute against
subscription_entitlement
cache entries so that they may get refreshed periodically. - If you have a more real-time use case, it is better to listen to webhooks and invalidate the associated cache entries.
We're always happy to help you with any questions you might have!
support@chargebee.com