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/sdkReact 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:
| React | Vanilla JavaScript | |
|---|---|---|
| Import | @levered_dev/sdk/react | @levered_dev/sdk |
| API style | Components and hooks | Class with async methods |
| Best for | React and Next.js apps | Node.js, Vue, Svelte, server-side rendering, or any non-React environment |
| Fallback handling | Built into the useVariant hook -- you pass a fallback and the hook always returns a renderable value | Manual -- getVariant returns null on error and you handle it yourself |
| State management | Managed 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
- React integration -- Provider, hooks, and admin menu
- Vanilla JavaScript -- Client class and server-side usage