Unleash Proxy Client

3.7.8 · active · verified Tue Apr 21

The `unleash-proxy-client` is a robust JavaScript client library tailored for integrating Unleash feature flags directly into browser-based applications. It is specifically designed to work with an Unleash Proxy or Unleash Edge instance, which acts as an intermediary to securely serve feature toggles to the frontend without exposing sensitive API keys. The current stable version is 3.7.8. The library maintains an active development pace, frequently releasing patch updates for bug fixes and minor enhancements, alongside occasional alpha/beta releases for upcoming major versions. Its core strengths include efficient context management for user and application attributes, an event-driven architecture for reacting to flag changes, and automated metrics reporting to the Unleash instance. It provides comprehensive TypeScript type definitions, significantly improving developer experience and code reliability in modern web development. This client is ideal for SPAs and server-side rendered apps where client-side feature toggle evaluation is desired.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the UnleashClient, set context, listen for 'ready' and 'error' events, and check the status of a feature flag. It also shows how to react to flag updates.

import { UnleashClient, EVENTS } from 'unleash-proxy-client';

const unleash = new UnleashClient({
  url: 'https://proxy.unleash.cloud/proxy',
  clientKey: process.env.UNLEASH_CLIENT_KEY ?? 'YOUR_CLIENT_KEY',
  appName: 'my-frontend-app',
  instanceId: 'your-instance-id',
  refreshInterval: 15000, // Refresh flags every 15 seconds
});

unleash.on(EVENTS.READY, () => {
  console.log('Unleash Client is ready!');
  const isNewFeatureEnabled = unleash.isEnabled('new-feature');
  console.log(`'new-feature' is ${isNewFeatureEnabled ? 'enabled' : 'disabled'}`);

  // You can also listen for flag updates
  unleash.on(EVENTS.UPDATE, () => {
    const updatedFeatureState = unleash.isEnabled('new-feature');
    console.log(`'new-feature' state updated to ${updatedFeatureState ? 'enabled' : 'disabled'}`);
  });
});

unleash.on(EVENTS.ERROR, (error) => {
  console.error('Unleash Client Error:', error);
});

// Set initial context or update it later
unleash.setContext({
  userId: 'user123',
  environment: 'production',
});

unleash.start();

console.log('Unleash Client started initialization...');

view raw JSON →