Report feature usage

Wire up your application to report the usage of a feature that your customer is entitled to consume.

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
  5. Entitlement checks enabled
  6. Pricing Table (optional)

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. Report usage of a feature

Once you have done an entitlement check for the user, you can give access to the feature, and once the user has consumed the feature you can report the feature usage to Tier.

Report usage of a feature

await tier.report('org:customer-id-from-your-db',
	'feature:feature-id',
	 1 // This is the unit of feature consumption,
	{
		at: new Date('23 March 2023'),
		clobber: true // If you want the usage amount to override any previously reported usage of the feature for the current phase
	}
);

3. Use Answer response of can() for reporting usage (optional)

If you used can() for the entitlement check, then you can use the response of this check to report usage

Report usage using response of `can()`

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
  access.report() // Report feature usage here
} else {
  // Handle restriction and even upselling here.
}

This quickstart will give you an idea of how to register your customer in Tier. You can also refer to our SDK references to understand more about can() and report().