Skip to main content
This SDK is being phased out. For new integrations, we recommend using the JavaScript Server SDK with local resolve, which provides better performance and reliability. Only use this SDK when client-side context modifications are required that the local resolve SDK cannot support.
The Confidence JavaScript SDK provides feature flag evaluation for web browser applications. Flags resolve once according to the evaluation context, and values read from a local cache for fast access.

Features

  • Local caching: Flag values cached locally for instant access
  • OpenFeature compatible: Standard feature flag API through OpenFeature provider
  • Automatic apply events: Tracks flag usage
  • Managed context: Optional visitor ID and page context
  • Event tracking: Built-in analytics event tracking

Installation

npm install @spotify-confidence/sdk

Quickstart

import { Confidence } from '@spotify-confidence/sdk';

// Initialize Confidence
const confidence = Confidence.create({
  clientSecret: 'your-client-secret',
});

// Set the evaluation context
confidence.setContext({
  targeting_key: 'user-123',
  country: 'US',
});

// Subscribe to flag updates and evaluate
await confidence.activate();

// Evaluate a flag
const value = confidence.getFlag('my-feature-flag', false);
console.log('Flag value:', value);

With OpenFeature

import { OpenFeature } from '@openfeature/web-sdk';
import { createConfidenceWebProvider } from '@spotify-confidence/openfeature-web-provider';

// Create and register the provider
const provider = createConfidenceWebProvider({
  clientSecret: 'your-client-secret',
});

await OpenFeature.setProviderAndWait(provider);
const client = OpenFeature.getClient();

// Evaluate a flag
const value = client.getBooleanValue('my-feature-flag', false);

React Integration

The standalone React SDK is being phased out. For new React integrations, we recommend using the JavaScript Server SDK with local resolve and its React hooks, which provides better performance and reliability.
The Confidence React SDK provides React hooks and components for feature flag evaluation. Built on top of the JavaScript SDK, it offers a React-native developer experience.

Installation

npm install @spotify-confidence/sdk @spotify-confidence/react

Usage with Confidence SDK

import { Confidence } from '@spotify-confidence/sdk';
import { ConfidenceProvider, useFlag } from '@spotify-confidence/react';

// Initialize Confidence
const confidence = Confidence.create({
  clientSecret: 'your-client-secret',
});

// Set initial context
confidence.setContext({
  targeting_key: 'user-123',
  country: 'US',
});

// Wrap your app with the provider
function App() {
  return (
    <ConfidenceProvider confidence={confidence}>
      <MyComponent />
    </ConfidenceProvider>
  );
}

// Use flags in components
function MyComponent() {
  const showNewFeature = useFlag('my-feature-flag', false);

  return (
    <div>
      {showNewFeature ? <NewFeature /> : <OldFeature />}
    </div>
  );
}

Usage with OpenFeature

import { OpenFeatureProvider, useFlag } from '@openfeature/react-sdk';
import { createConfidenceWebProvider } from '@spotify-confidence/openfeature-web-provider';

const provider = createConfidenceWebProvider({
  clientSecret: 'your-client-secret',
});

function App() {
  return (
    <OpenFeatureProvider provider={provider}>
      <MyComponent />
    </OpenFeatureProvider>
  );
}

function MyComponent() {
  const { value: showNewFeature } = useFlag('my-feature-flag', false);

  return (
    <div>
      {showNewFeature ? <NewFeature /> : <OldFeature />}
    </div>
  );
}

Resources