Check for feature access

Learn how to implement entitlement checks, for hiding or displaying features to your customer based on their current subscription plan.

Before getting started

To get the most out of this quick-start guide, you’ll need:

  1. Stripe secret key
  2. Select your pricing model & sync with Stripe
  3. Basic auth implementation with auth guards
  4. Subscribed customers

SDK Object definition

AnswerThis object will be returned by the can() method to help the user report any usage of a given feature in the product.

1. Initialize Tier SDK

Initialize the Tier SDK, ideally in a singleton design pattern, with both TIER_BASE_URL and TIER_API_KEY environment variables set with the prescribed values.

// To make use of Tier Cloud Alpha you need to
// points base URL to the Tier API server
TIER_BASE_URL = "https://api.tier.run"
// and set Tier API Key as your Stripe secret key
TIER_API_KEY = "sk_test_51..."

Initialize Tier

import tier from "tier"

2. Check access to a feature for your customer

You can easily provide/restrict access to a given feature for a customer using Tier’s entitlement checks.

If your customer is in the base plan it can ensure that you only show features that are assigned under the base plan, and you can even ask them to upgrade to a paid plan when they try to access a restricted feature.

Check entitlement

const access = await tier.can(
  "org:customer-id-from-your-db",
  "feature:feature-from-base-plan"
)
if (access.ok) {
  // Give access to the feature here
} else {
  // Handle restriction and even upselling here.
}

This quickstart will give you an idea of how to give access to features for your customers using entitlements in Tier. You can also refer to our SDK references to understand more about can(), lookupLimits(), and lookupLimit().