1.0.0-beta.185 • Published 9 months ago

@openmeter/sdk v1.0.0-beta.185

Weekly downloads
-
License
Apache 2.0
Repository
github
Last release
9 months ago

OpenMeter Node SDK

Install

npm install --save @openmeter/sdk

Example

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

API

Events

ingest

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)

batch ingest

await openmeter.events.ingest([event1, event2, event3])

list

Retrieve latest raw events. Useful for debugging.

const events = await openmeter.events.list()

Meters

list

List meters.

const meters = await openmeter.meters.list()

get

Get one meter by slug.

const meter = await openmeter.meters.get('m1')

query

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,
})

meter subjects

List meter subjects.

const subjects = await openmeter.meters.subjects('my-meter-slug')

Portal

createToken

Create subject-specific tokens. Useful to build consumer dashboards.

const token = await openmeter.portal.createToken({ subject: 'customer-1' })

invalidateTokens

Invalidate portal tokens for all or specific subjects.

await openmeter.portal.invalidateTokens()

Subject

Subject mappings. Like display name and metadata.

upsert

Upsert subjects.

const subjects = await openmeter.subjects.upsert([
  {
    key: 'customer-1',
    displayName: 'ACME',
  },
])

list

List subjects.

const subjects = await openmeter.subjects.list()

get

Get subject by key.

const subjects = await openmeter.subjects.get('customer-1')

delete

Delete subject by key. It doesn't delete corresponding usage.

await openmeter.subjects.delete('customer-1')

createEntitlement

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,
})

listEntitlements

List subject entitlements.

const entitlement = await openmeter.subjects.listEntitlements('customer-1')

getEntitlement

Get a subject entitlement by ID by Feature ID or by Feature Key.

const entitlement = await openmeter.subjects.getEntitlement(
  'customer-1',
  'ai_tokens'
)

deleteEntitlement

Delete a subject entitlement by ID by Feature ID or by Feature Key.

await openmeter.subjects.deleteEntitlement('customer-1', 'ai_tokens')

getEntitlementValue

Get entitlement value by ID by Feature ID or by Feature Key.

const value = await openmeter.subjects.getEntitlementValue(
  'customer-1',
  'ai_tokens'
)

getEntitlementHistory

Get entitlement history by ID by Feature ID or by Feature Key

const entitlement = await openmeter.subjects.getEntitlementHistory(
  'customer-1',
  'ai_tokens'
)

resetEntitlementUsage

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,
  }
)

createEntitlementGrant

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

listEntitlementGrants

List entitlement grants

const entitlement = await openmeter.subjects.listEntitlementGrants('customer-1', 'ai_tokens)

Features

Features are the building blocks of your entitlements, part of your product offering.

create

Upsert subjects.

const feature = await openmeter.features.create({
  key: 'ai_tokens',
  name: 'AI Tokens',
  // optional
  meterSlug: 'tokens_total',
})

list

List features.

const features = await openmeter.features.list()

get

Get feature by key.

const feature = await openmeter.features.get('ai_tokens')

delete

Delete feature by key.

await openmeter.features.delete('ai_tokens')

Grants

Entitlement grants allow to issue of additional one-time or recurring allowances on a subject's entitlement.

list

List grants.

const grants = await openmeter.grants.list()
1.0.0-beta.185

9 months ago

1.0.0-beta.183

10 months ago

1.0.0-beta.184

10 months ago

1.0.0-beta.182

10 months ago

1.0.0-beta.181

10 months ago

1.0.0-beta.180

10 months ago

1.0.0-beta.179

10 months ago

1.0.0-beta.178

10 months ago

1.0.0-beta.177

10 months ago

1.0.0-beta.176

10 months ago

1.0.0-beta.174

10 months ago

1.0.0-beta.175

10 months ago

1.0.0-beta.173

10 months ago

1.0.0-beta.172

10 months ago

1.0.0-beta.170

10 months ago

1.0.0-beta.171

10 months ago

1.0.0-beta.169

10 months ago

1.0.0-beta.167

10 months ago

1.0.0-beta.168

10 months ago

1.0.0-beta.166

10 months ago

1.0.0-beta.165

10 months ago

1.0.0-beta.163

10 months ago

1.0.0-beta.164

10 months ago

1.0.0-beta.136

11 months ago

1.0.0-beta.137

11 months ago

1.0.0-beta.134

11 months ago

1.0.0-beta.135

11 months ago

1.0.0-beta.132

11 months ago

1.0.0-beta.133

11 months ago

1.0.0-beta.130

11 months ago

1.0.0-beta.131

11 months ago

1.0.0-beta.138

11 months ago

1.0.0-beta.139

11 months ago

1.0.0-beta.125

11 months ago

1.0.0-beta.126

11 months ago

1.0.0-beta.123

12 months ago

1.0.0-beta.124

12 months ago

1.0.0-beta.121

12 months ago

1.0.0-beta.122

12 months ago

1.0.0-beta.120

12 months ago

1.0.0-beta.129

11 months ago

1.0.0-beta.127

11 months ago

1.0.0-beta.128

11 months ago

1.0.0-beta.114

12 months ago

1.0.0-beta.115

12 months ago

1.0.0-beta.112

1 year ago

1.0.0-beta.113

1 year ago

1.0.0-beta.110

1 year ago

1.0.0-beta.111

1 year ago

1.0.0-beta.118

12 months ago

1.0.0-beta.119

12 months ago

1.0.0-beta.116

12 months ago

1.0.0-beta.117

12 months ago

1.0.0-beta.103

1 year ago

1.0.0-beta.104

1 year ago

1.0.0-beta.101

1 year ago

1.0.0-beta.102

1 year ago

1.0.0-beta.100

1 year ago

1.0.0-beta.109

1 year ago

1.0.0-beta.107

1 year ago

1.0.0-beta.108

1 year ago

1.0.0-beta.105

1 year ago

1.0.0-beta.106

1 year ago

1.0.0-beta.161

10 months ago

1.0.0-beta.162

10 months ago

1.0.0-beta.160

11 months ago

1.0.0-beta.99

1 year ago

1.0.0-beta.97

1 year ago

1.0.0-beta.98

1 year ago

1.0.0-beta.95

1 year ago

1.0.0-beta.96

1 year ago

1.0.0-beta.93

1 year ago

1.0.0-beta.94

1 year ago

1.0.0-beta.150

11 months ago

1.0.0-beta.151

11 months ago

1.0.0-beta.158

11 months ago

1.0.0-beta.91

1 year ago

1.0.0-beta.159

11 months ago

1.0.0-beta.92

1 year ago

1.0.0-beta.156

11 months ago

1.0.0-beta.157

11 months ago

1.0.0-beta.90

1 year ago

1.0.0-beta.154

11 months ago

1.0.0-beta.155

11 months ago

1.0.0-beta.152

11 months ago

1.0.0-beta.153

11 months ago

1.0.0-beta.88

1 year ago

1.0.0-beta.89

1 year ago

1.0.0-beta.86

1 year ago

1.0.0-beta.87

1 year ago

1.0.0-beta.84

1 year ago

1.0.0-beta.85

1 year ago

1.0.0-beta.82

1 year ago

1.0.0-beta.83

1 year ago

1.0.0-beta.140

11 months ago

1.0.0-beta.147

11 months ago

1.0.0-beta.148

11 months ago

1.0.0-beta.81

1 year ago

1.0.0-beta.145

11 months ago

1.0.0-beta.146

11 months ago

1.0.0-beta.143

11 months ago

1.0.0-beta.144

11 months ago

1.0.0-beta.141

11 months ago

1.0.0-beta.142

11 months ago

1.0.0-beta.149

11 months ago

1.0.0-beta.80

1 year ago

1.0.0-beta.79

1 year ago

1.0.0-beta.78

1 year ago

1.0.0-beta.77

1 year ago

1.0.0-beta.75

1 year ago

1.0.0-beta.76

1 year ago

1.0.0-beta.74

1 year ago

1.0.0-beta.73

1 year ago

1.0.0-beta.67

1 year ago

1.0.0-beta.68

1 year ago

1.0.0-beta.69

1 year ago

1.0.0-beta.72

1 year ago

1.0.0-beta.66

1 year ago

1.0.0-beta.65

1 year ago

1.0.0-beta.64

1 year ago

1.0.0-beta.63

1 year ago

1.0.0-beta.62

1 year ago

1.0.0-beta.60

1 year ago

1.0.0-beta.59

1 year ago

1.0.0-beta.58

1 year ago

1.0.0-beta.55

1 year ago

1.0.0-beta.56

1 year ago

1.0.0-beta.57

1 year ago

1.0.0-beta.54

1 year ago

1.0.0-beta.53

1 year ago

1.0.0-beta.51

1 year ago

1.0.0-beta.52

1 year ago

1.0.0-beta.50

1 year ago

1.0.0-beta.49

1 year ago

1.0.0-beta.48

1 year ago

1.0.0-beta.47

1 year ago

1.0.0-beta.46

1 year ago

1.0.0-beta.45

1 year ago

1.0.0-beta.44

1 year ago

1.0.0-beta.43

1 year ago

1.0.0-beta.42

1 year ago

1.0.0-beta.40

1 year ago

1.0.0-beta.41

1 year ago

1.0.0-beta.39

1 year ago

1.0.0-beta.37

1 year ago

1.0.0-beta.38

1 year ago

1.0.0-beta.36

1 year ago

1.0.0-beta.35

2 years ago

1.0.0-beta.33

2 years ago

1.0.0-beta.34

2 years ago

1.0.0-beta.22

2 years ago

1.0.0-beta.23

2 years ago

1.0.0-beta.21

2 years ago

1.0.0-alpha.4

2 years ago

1.0.0-alpha.3

2 years ago

1.0.0-alpha.2

2 years ago

1.0.0-alpha.1

2 years ago

1.0.0-beta.28

2 years ago

1.0.0-beta.29

2 years ago

1.0.0-beta.26

2 years ago

1.0.0-beta.24

2 years ago

1.0.0-beta.25

2 years ago

1.0.0-beta.2

2 years ago

1.0.0-beta.3

2 years ago

1.0.0-beta.4

2 years ago

1.0.0-beta.5

2 years ago

1.0.0-beta.1

2 years ago

1.0.0-beta.31

2 years ago

1.0.0-beta.32

2 years ago

1.0.0-beta.6

2 years ago

1.0.0-beta.30

2 years ago

1.0.0-beta.8

2 years ago

1.0.0-beta.9

2 years ago

1.0.0-beta.13

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago