@kelviq/js-sdk is a lightweight, plain TypeScript library designed for interacting with the kelviq entitlement service. It allows you to:
- Fetch all feature entitlements for a specific organization and customer.
- Cache these entitlements client-side for efficient and repeated access.
- Check if a user has access to a particular feature.
- Retrieve specific entitlement details, including boolean flags, numeric configurations, and metered usage data.
- Manually refresh entitlement data from the server.
If you are using React, you can use the React SDK instead.
Installation
Install the SDK using your preferred package manager or include it directly via CDN.Basic Usage
Get started with the SDK in just a few lines of code.API Reference
Complete documentation of all SDK methods and configuration options.Initialization
Create and configure your SDK instance with the required parameters.Options
| Option | Type | Description | |
|---|---|---|---|
customerId | string | Unique identifier for the customer | |
accessToken | string | Authentication token for API requests | |
environment | sandbox|production | ’production’ | The environment to use. If you want to use the sandbox, you must provide this. |
initializeAndFetch | boolean | true | Automatically fetch entitlements on initialization |
onError | (error: Error) => void | Error callback function |
Core Methods
hasAccess(featureId: string): boolean
Check if the user has access to a specific feature. Returns false if the feature doesn’t exist or data hasn’t been fetched.
getEntitlement(featureId: string): Entitlement | null
Get the aggregated entitlement for a specific feature. Returns the unified Entitlement object which includes the .items array of raw API objects.
getEntitlements(): Record<string, Entitlement> | null
Get all aggregated entitlements as a map keyed by featureId.
getRawEntitlement(featureId: string): RawEntitlement[] | null
Get the raw, un-aggregated API items for a specific feature. Useful when you need per-item details like resetAt or hardLimit.
getRawEntitlements(): RawEntitlementsApiResponse | null
Get the full raw API response including the customerId wrapper, exactly as received from the server.
ready(): Promise<void>
Wait for the initial fetch (triggered by initializeAndFetch: true) to complete. Resolves immediately if no initial fetch was triggered or if the data is already available.
fetchAllEntitlements(forceRefresh?: boolean): Promise<Record<string, Entitlement>>
Manually trigger a network request to refresh the entitlements. If a fetch is already in progress, the same promise is returned (calls are deduplicated).
isLoading(): boolean
Check if entitlements are currently being fetched.
getLastError(): Error | null
Get the last error that occurred during entitlement fetching.
clearCache(): void
Clear the cached entitlements.
Entitlement Types
The SDK uses a single, unifiedEntitlement interface. When the API returns duplicate featureIds (e.g., from Subscriptions + Top-ups), they are aggregated automatically.
resetAt and hardLimit are available on each item in the items array.
Examples
Real-world usage patterns and common implementation scenarios.Feature Gating
Control access to features based on user entitlements.Usage-Based Features
Implement usage limits and quota management.Configuration-Based Features
Apply dynamic configuration based on user entitlements.Accessing Per-Item Details
When you need item-level details likeresetAt or hardLimit, use the items array or the getRawEntitlement method.
Error Handling
Robust error handling for production applications.Custom API Request Service
Integrate with your existing HTTP client or add custom logic.TypeScript Support
Full TypeScript support with comprehensive type definitions. The SDK is written in TypeScript and provides full type safety:Advanced Configuration
Advanced options for customizing SDK behavior and API integration.API Configuration
Configure API request behavior and retry logic.Custom Error Handling
Implement sophisticated error handling and recovery strategies.Advanced Options
Additional configuration options for specialized use cases. Default values are:| Option | Type | Description |
|---|---|---|
apiUrl | string | Base URL for the API |
entitlementsPath | string | Path for fetching entitlements |
apiConfig.maxRetries | number | Maximum number of retries for API requests |
apiConfig.timeout | number | Timeout for API requests in milliseconds |
apiConfig.backoffBaseDelay | number | Base delay for exponential backoff strategy |
Using the Sandbox
If you want to use the sandbox, you need to set theenvironment option to sandbox in the init function.