LeveredDocs
SDK

SDK

JavaScript/TypeScript SDK that requests variants from the Levered API and reports exposures to the app.

The Levered SDK is a JavaScript/TypeScript library. It requests a variant from the Levered API for a user and returns it to the app, which renders it. The SDK handles caching, retries and timeouts.

Installation

npm install @levered_dev/sdk

React is an optional peer dependency. The React integration requires React 18 or later.

What the SDK does

  • The SDK fetches a variant from the Levered API for an optimization and a user.
  • The SDK caches responses in memory and answers repeat requests from the cache.
  • The SDK retries a request after a network error or a timeout, with exponential backoff.
  • The SDK calls the onExposure callback, in which the app logs the variant assignment to the warehouse.

What the SDK does not do

The SDK does not store or forward event data. When a user is exposed to a variant, the SDK calls the onExposure callback with an ExposureEvent object. The app has to send this event on:

  • With your own warehouse, send the event to the place where the analytics data is stored, for example BigQuery, Snowflake or Segment.
  • With the Managed Warehouse, send the event from your server to the ingestion API of Levered.

The variant request is the only request that the SDK sends to the Levered servers.

Integration paths

The SDK has two integrations:

ReactVanilla JavaScript
Import@levered_dev/sdk/react@levered_dev/sdk
API styleComponents and hooksClass with async methods
Best forReact and Next.js appsNode.js, Vue, Svelte, server-side rendering, or any non-React environment
Fallback handlingThe useVariant hook takes a fallback and returns it while loading and on error.getVariant returns null on error. The app has to provide the fallback.
State managementThe hook manages the loading state and re-fetches on prop change.The app manages the state.

Use the React integration in a React or Next.js application. The provider and the hook handle loading states, caching and re-fetching.

Use the vanilla integration in a framework other than React, in server-side code, or if the app has to control when and how variants are fetched.

Next steps