Levered Docs
SDK

SDK

JavaScript/TypeScript SDK for serving optimized variants to your users.

The Levered SDK is a lightweight JavaScript/TypeScript library that fetches optimized variants from the Levered API and delivers them to your users. It handles caching, retries, and timeouts so you can focus on rendering.

Installation

npm install @levered_dev/sdk

React is an optional peer dependency. If you are using React, make sure you have React 18 or later installed.

What the SDK does

  • Fetches variants from the Levered API for a given optimization and user.
  • Caches responses in memory to avoid redundant network requests.
  • Retries on failure with exponential backoff.
  • Fires exposure callbacks so you can log every variant assignment to your 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 your onExposure callback with an ExposureEvent object. It is your responsibility to send that event to your data warehouse (BigQuery, Snowflake, Segment, or wherever you store analytics data). Levered reads exposure and reward data from your warehouse at training time -- the SDK never writes to Levered's servers beyond the initial variant request.

Integration paths

There are two ways to use the SDK:

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 handlingBuilt into the useVariant hook -- you pass a fallback and the hook always returns a renderable valueManual -- getVariant returns null on error and you handle it yourself
State managementManaged by the hook (loading state, re-fetch on prop change)You manage state yourself

Use React if you are building a React or Next.js application. The provider and hook handle loading states, caching, and re-fetching automatically.

Use Vanilla if you are working in a non-React framework, running server-side code, or need full control over when and how variants are fetched.

Next steps