Skip to main content

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 using setCustomerId function as shown below.
To find out when the client is ready, you can use 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

The getActiveSubscriptionsList method returns a list of slim subscription data, for extended data for a specific subscription use getSubscription method.
In addition, there’s an option to pass a list of resource Ids

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 the estimateSubscription method. This can help the customer to understand the costs before paying.
Return fields
You can also estimate the cost of updating an existing subscription using the updateSubscription method, which also returns a breakdown of the proration amounts:
Return fields
  1. Passing of promo codes requires an integration with a billing solution, such as: Stripe.
  2. Promo codes should be generated in the integrated billing solution.
  3. When providing the optional promotionCode parameter, the promo code and associated coupon will validated against the defined restrictions to ensure that they’re applicable for the subscription. If validation fails, a relevant error code would be returned; otherwise, the subscription will include the discounted price.

Handling a successful checkout

When initiating checkout using Stigg, the user is expected to be redirected and land on the successUrl 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 a CustomerEntitlementsResultV2 object with an entitlements array (EntitlementV2[]).
To unsubscribe, pass the same function reference to 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.
In offline mode, the JS SDK respects the global fallback strategy, and entitlement evaluations are limited to the values defined as fallback entitlements.

Credit auto-recharge

Migrating to v4.0.0

Full SDK reference

JavaScript SDK reference

SDK changelog

JavaScript SDK changelog