npm install --save @openmeter/sdk
import { OpenMeter, type Event } from '@openmeter/sdk'
const openmeter = new OpenMeter({ baseUrl: 'http://localhost:8888' })
// Ingesting an event
const event: Event = {
specversion: '1.0',
id: 'id-1',
source: 'my-app',
type: 'my-type',
subject: 'my-awesome-user-id',
time: new Date(),
data: {
api_calls: 1,
},
}
await openmeter.events.ingest(event)
// Fetching a meter
const meter = await openmeter.meters.get('m1')
import { type Event } from '@openmeter/sdk'
const event: Event = {
specversion: '1.0',
id: 'id-1',
source: 'my-app',
type: 'my-type',
subject: 'my-awesome-user-id',
time: new Date(),
data: {
api_calls: 1,
},
}
await openmeter.events.ingest(event)
await openmeter.events.ingest([event1, event2, event3])
Retrieve latest raw events. Useful for debugging.
const events = await openmeter.events.list()
List meters.
const meters = await openmeter.meters.list()
Get one meter by slug.
const meter = await openmeter.meters.get('m1')
Query meter values.
import { WindowSize } from '@openmeter/sdk'
const values = await openmeter.meters.query('my-meter-slug', {
subject: ['user-1'],
groupBy: ['method', 'path'],
from: new Date('2021-01-01'),
to: new Date('2021-01-02'),
windowSize: WindowSize.HOUR,
})
List meter subjects.
const subjects = await openmeter.meters.subjects('my-meter-slug')
Create subject-specific tokens. Useful to build consumer dashboards.
const token = await openmeter.portal.createToken({ subject: 'customer-1' })
Invalidate portal tokens for all or specific subjects.
await openmeter.portal.invalidateTokens()
Subject mappings. Like display name and metadata.
Upsert subjects.
const subjects = await openmeter.subjects.upsert([
{
key: 'customer-1',
displayName: 'ACME',
},
])
List subjects.
const subjects = await openmeter.subjects.list()
Get subject by key.
const subjects = await openmeter.subjects.get('customer-1')
Delete subject by key. It doesn't delete corresponding usage.
await openmeter.subjects.delete('customer-1')
Create entitlement for a subject. Entitlements allow you to manage subject feature access, balances, and usage limits.
// Issue 10,000,000 tokens every month
const entitlement = await openmeter.subjects.createEntitlement('customer-1', {
type: 'metered',
featureKey: 'ai_tokens',
usagePeriod: {
interval: 'MONTH',
},
issueAfterReset: 10000000,
})
List subject entitlements.
const entitlement = await openmeter.subjects.listEntitlements('customer-1')
Get a subject entitlement by ID by Feature ID or by Feature Key.
const entitlement = await openmeter.subjects.getEntitlement(
'customer-1',
'ai_tokens'
)
Delete a subject entitlement by ID by Feature ID or by Feature Key.
await openmeter.subjects.deleteEntitlement('customer-1', 'ai_tokens')
Get entitlement value by ID by Feature ID or by Feature Key.
const value = await openmeter.subjects.getEntitlementValue(
'customer-1',
'ai_tokens'
)
Get entitlement history by ID by Feature ID or by Feature Key
const entitlement = await openmeter.subjects.getEntitlementHistory(
'customer-1',
'ai_tokens'
)
Reset the entitlement usage and start a new period. Eligible grants will be rolled over.
const entitlement = await openmeter.subjects.resetEntitlementUsage(
'customer-1',
{
retainAnchor: true,
}
)
Create a grant for an entitlement.
const grant = await openmeter.subjects.createEntitlementGrant(
'customer-1',
'ai_tokens',
{
amount: 100,
priority: 1,
effectiveAt: '2023-01-01T00:00:00Z',
expiration: {
duration: 'HOUR',
count: 12,
},
minRolloverAmount: 100,
maxRolloverAmount: 100,
recurrence: {
interval: 'MONTH',
anchor: '2024-06-28T18:29:44.867Z',
},
}
)
List entitlement grants
const entitlement = await openmeter.subjects.listEntitlementGrants('customer-1', 'ai_tokens)
Features are the building blocks of your entitlements, part of your product offering.
Upsert subjects.
const feature = await openmeter.features.create({
key: 'ai_tokens',
name: 'AI Tokens',
// optional
meterSlug: 'tokens_total',
})
List features.
const features = await openmeter.features.list()
Get feature by key.
const feature = await openmeter.features.get('ai_tokens')
Delete feature by key.
await openmeter.features.delete('ai_tokens')
Entitlement grants allow to issue of additional one-time or recurring allowances on a subject's entitlement.
List grants.
const grants = await openmeter.grants.list()