@limitry/client
v0.0.3
Published
Limitry client-side SDK for browser, React Native, and Expo
Downloads
57
Maintainers
Readme
@limitry/client
Client-side SDK for Limitry - works in browser, React Native, and Expo.
Installation
npm install @limitry/clientUsage
import { LimitryClient } from '@limitry/client';
// Create a client with a token from your backend
const client = new LimitryClient({
clientToken: 'your-client-token',
});
// Check limits before an operation
const result = await client.checkLimits();
if (result.allowed) {
// Proceed with the operation
await client.recordEvent({
eventType: 'api.call',
values: { tokens: 150 }
});
} else {
// Handle limit exceeded
console.log('Limit exceeded');
}
// Get usage summary
const summary = await client.getEventsSummary('day');
console.log(`Total events: ${summary.usage.totalEvents}`);Getting a Client Token
Client tokens are created by your backend using the server-side SDK:
// On your backend
import { Limitry } from '@limitry/sdk';
const limitry = new Limitry({ apiKey: process.env.LIMITRY_API_KEY });
const { token } = await limitry.clientTokens.create({
context: { customerId: 'cust_123' },
ttlSeconds: 3600, // 1 hour
});
// Return this token to your frontendAPI
LimitryClient
Constructor Options
clientToken(required): The client token from your backendbaseUrl(optional): Custom API URL (defaults tohttps://api.limitry.com)
Methods
checkLimits(options?): Check limits. Options can includedimensionsfor filtering.recordEvent(options): Record an event witheventType,values,dimensions,properties.getEventsSummary(period?): Get events summary. Period can be'hour','day','week', or'month'.getContext(): Get the context associated with the client token
License
Apache-2.0
