Overview
The JavaScript SDK allows you access customer data from Stigg, and leverage it to implement mind-blowing customer experiences.Stigg also offers plug-and-play customizable widgets for React, Vue.js, Webflow, Wordpress, and plain HTML.
Installing the SDK
The first step is to install the Stigg SDK as a dependency in your application using your application’s dependency manager:Retrieving the publishable key
In the Stigg app, go to Integrations > API keys. Copy the Publishable key of the relevant environment.Initializing the SDK
Initializing client instance with the publishable key. You can either pass the customer ID when initializing the client instance or usingsetCustomerId function as shown below.
waitForInitialization which return a promise that resolves when the client is ready to use, or rejects when the initialization failed.
Setting the customer context
Set the customer ID of the user that’s accessing your app, usually after the customer signs in or restores a session:Setting the customer resource context
For Multi-instance products, set the customer resource ID of the user that’s accessing your app, usually after the customer enters into a resource context:Getting all of the entitlements of a specific customer
Getting the entitlement of a customer to a specific feature
Checking the entitlement for a boolean feature
Checking the entitlement for a numeric configuration feature
getNumericEntitlement is for non-metered numeric features (meterType: "None"). Calling it on a metered feature returns a FeatureTypeMismatch fallback (v4.0.0+). Use getMeteredEntitlement for metered features.Checking if a customer has access to a metered feature
Proactively checking if a customer will have access to X units of a metered feature
Getting paywall data
Useful for rendering the public pricing page or customer paywall.Getting customer portal data
Useful for rendering the public pricing page or customer paywall.Getting customer data
Useful for rendering a customer portal.Listing customers active subscriptions
ThegetActiveSubscriptionsList method returns a list of slim subscription data, for extended data for a specific subscription use getSubscription method.
Getting a subscription
Useful for rendering a customer portal. To retrieve extended subscription:Getting all available coupons
Useful for rendering a paywall or promotions.Estimate subscription cost
You can estimate the subscription cost using theestimateSubscription method. This can help the customer to understand the costs before paying.
updateSubscription method, which also returns a breakdown of the proration amounts:
Handling a successful checkout
When initiating checkout using Stigg, the user is expected to be redirected and land on thesuccessUrl destination page in the case of a successful payment.
Due to the async nature of webhooks from Stripe, the subscription might not be yet provisioned in your Stigg account. To make sure that the new subscription has been activated in this scenario, you can utilize the waitForCheckoutCompleted method to have an indication if the checkout is still in progress or not. Once the promise resolves, the subscription has been successfully activated and new entitlements were granted. If there’s a timeout or a network error during that time, the promise is rejected with the error.
For example, you can present a loading animation until the promise returned from the method is resolved.
Clearing the customer context
Clear the customer ID of the user that’s accessing your app, usually after the customer signs out:Clearing the customer resource context
For Multi-instance products, clear the customer ID of the user that’s accessing your app, usually after the customer exit the context of specific resource:Refresh the cache
Stigg’s SDK refreshes its cache of customer entitlements and usage data upon initialization (for example: when a page is refreshed), as well as periodically every 30 seconds. After performing an operation on the backend that can modify the customer entitlements or the usage of a feature (for example: updating subscriptions or reporting usage), it’s useful to immediately refresh the cache. To do so, call the below method:Listening for entitlement updates
Subscribe to real-time updates whenever a customer’s entitlements change. The callback receives aCustomerEntitlementsResultV2 object with an entitlements array (EntitlementV2[]).
removeListener:
Offline mode
During local development or testing, you might want to avoid making network requests to the Stigg API. To do this, you can run the JS SDK in offline mode by enabling the offline option. When enabled, API key validation will always succeed, regardless of the key provided.Credit auto-recharge
Migrating to v4.0.0
| Change | Migration |
|---|---|
entitlementsUpdated event payload changed from Map<string, CachedEntitlement> to CustomerEntitlementsResultV2 | Update listeners to iterate .entitlements (an EntitlementV2[] array) instead of a Map. |
EntitlementsState type removed | Replace imports with CustomerEntitlementsResult from @stigg/js-client-sdk. |
resetSettings on MeteredEntitlement removed (was @deprecated) | Use the top-level resetPeriod and usagePeriodEnd fields directly on the entitlement. |
getNumericEntitlement() on a metered feature now returns a FeatureTypeMismatch fallback | Use getMeteredEntitlement() for metered features. |
isGranted on credit entitlements removed | Use hasAccess — same semantics. |
Logger interface second parameter widened to LoggerExtra (Record<string, any> | Error | Error['stack']) | Update any custom Logger implementation to accept LoggerExtra as the second parameter. |
Entitlement union type now includes EnumEntitlement | Add an EnumEntitlement branch to any exhaustive switch/case on Entitlement. |
CreditEntitlementCurrency named export removed | Use CreditEntitlement['currency'] inline type instead. |
