Follow our Launch Week between July 1 - 5
Logo
Getting Started

Get Started

Let's see how you can give customers access to 10 million monthly tokens in 3 steps.

This guide assumes that the token usage is already measured using a meter. The meter needs to be either a SUM or COUNT based meter to work with the entitlements system.

This example uses the Node.js OpenMeter SDK to create a feature, assign access to a customer, and check if the customer has access.

1. Create your first feature

Features are representing a feature of the system being metered. First create a feature you want to control customer access for. Example features: AI Tokens, API Requests, etc.

import { OpenMeter, type Event } from '@openmeter/sdk';
 
// Create feature GPT Tokens
const feature = await openmeter.features.create({
  key: 'ai_tokens',
  name: 'AI Tokens',
  meterSlug: 'tokens_total',
});

2. Assign access to a customer

Create an entitlement to define access and allowances of a feature for a customer.

// Give access to monthly 10 million tokens to customer-1
const entitlement = await openmeter.subjects.createEntitlement('customer-1', {
  type: 'metered',
  featureKey: 'ai_tokens',
  issueAfterReset: 10000000 // 10 Million tokens
  usagePeriod: {
    interval: 'MONTH',
  },
  isSoftLimit: false
})

By creating an entitlement you are giving access to a subject (one of the metered system's customers) to use the perviously defined feature.

Multiple entitlement types

You can also create static or boolean entitlements.

3. Check Access

Check if your customer has access to consume that feature.

// Get the entitlement's current value
const value = await openmeter.subjects.getEntitlementValue(
  'customer-1',
  'gpt_tokens',
);
const { hasAccess, balance, usage, overage } = value;
 
if (!hasAccess) {
  return reply.status(402).send('purchase additional tokens');
}
Grant additional usage

Use the Grant API to grant additional usage to a customer.

Last edited on July 2, 2024