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')