@kelviq/js-sdk is a lightweight, plain TypeScript library designed for interacting with Kelviq entitlement and pricing services. 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.
- Fetch product offering and pricing data for display on pricing pages.
- Format prices with locale-aware currency formatting.
- Automatically populate pricing into DOM elements via data attributes.
- Manually refresh entitlement and pricing 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.productId to fetch pricing data only (without entitlements):
customerId and productId together:
Options
At least one of
customerId or productId must be provided.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.
Pricing Methods
fetchPricing(forceRefresh?: boolean): Promise<RawPricingApiResponse>
Fetch product offering and pricing data. Requires productId to be set. Results are cached; use forceRefresh to bypass the cache.
getPricing(): RawPricingApiResponse | null
Get cached pricing data. Returns null if not yet fetched.
getPlan(identifier: string): RawPricingPlan | null
Find an enabled plan by its identifier from cached pricing data.
isPricingLoading(): boolean
Check if pricing data is currently being fetched.
getLastPricingError(): Error | null
Get the last error from a pricing fetch.
clearPricingCache(): void
Clear cached pricing data and reset pricing state.
renderPricing() — DOM Binding
Automatically populate pricing into HTML elements using data-kq-price attributes. The SDK scans the DOM for elements with these attributes and fills in formatted prices.
data-kq-price— the plan identifier (e.g.,"starter","pro")data-kq-period— the charge period (e.g.,"MONTHLY","ANNUALLY")- Free plans display
"Free"instead of a price - Disabled or missing plans result in empty text
kqFormatPrice Utility
A standalone utility for formatting prices with locale-aware currency formatting.
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.
Pricing Types
The pricing API returns a rich data structure with plans, charges, features, and billing periods.RawPricingApiResponse, RawPricingPlan, RawPricingPrice, RawPricingCharge, RawPricingFeature, RawPricingBillingPeriod.
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:Using the Sandbox
If you want to use the sandbox, you need to set theenvironment option to sandbox in the init function.