Tggl Client SDK

3.2.2 · active · verified Tue Apr 21

The `tggl-client` package provides a TypeScript SDK for integrating Tggl feature flags into both client-side (browsers, React Native) and server-side (Node.js) applications. Currently at version 3.2.2, the library maintains an active release cadence with frequent updates and bug fixes, as evidenced by recent v3.x releases. Version 3 represents a complete rewrite focused on improved reliability, fault tolerance, and maintainability, introducing a simpler API, better error handling, and automatic HTTP retries via `ky`. A key differentiator is its enforcement of feature flag best practices, specifically by removing the `isActive` method and making the default value a required argument for the `get` method since v2.0.0, guiding developers to always handle the absence of a flag gracefully. It also offers built-in storage options for Postgres, Redis, and Static storage for server-side usage, requiring corresponding peer dependencies like `pg` or `redis` for these features. The SDK supports Node.js environments version 20.0.0 and above.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the initialization and usage of both TgglClient (for client-side) and TgglLocalClient (for server-side) to evaluate feature flags with different contexts and built-in storage options.

import { TgglClient, TgglLocalClient, StaticStorage } from 'tggl-client';

async function runFeatureFlagExample() {
  // It's highly recommended to use environment variables for API keys in production
  const clientApiKey = process.env.TGGL_CLIENT_API_KEY ?? 'TGGL_CLIENT_KEY_XXX';
  const serverApiKey = process.env.TGGL_SERVER_API_KEY ?? 'TGGL_SERVER_KEY_YYY';

  // --- Client-side usage example (e.g., browser, React Native) ---
  console.log("\n--- Initializing TgglClient (Client-side) ---");
  const client = new TgglClient({
    apiKey: clientApiKey,
    initialContext: { userId: 'browser-user-123', plan: 'free' },
  });

  try {
    await client.waitReady(); // Wait for initial flags to be fetched
    if (client.get('enable-dark-mode', false)) {
      console.log('Client: Dark mode is ENABLED for this user!');
    } else {
      console.log('Client: Dark mode is DISABLED or not found, using default.');
    }

    const welcomeMessage = client.get('welcome-banner-text', 'Hello there!');
    console.log(`Client: Welcome message: "${welcomeMessage}"`);

  } catch (error) {
    console.error("Error with TgglClient initialization or flag evaluation:", error);
  }

  // --- Server-side usage example (e.g., Node.js backend) ---
  console.log("\n--- Initializing TgglLocalClient (Server-side) ---");
  // For production, consider PostgresStorage or RedisStorage with proper configuration
  const storage = new StaticStorage(); 
  const localClient = new TgglLocalClient({
    apiKey: serverApiKey,
    storage, // Pass the storage instance
    refreshInterval: 60000, // Refresh flags from Tggl API every minute
  });

  try {
    await localClient.waitReady(); // Wait for initial flags to be fetched

    const serverContext = { userId: 'server-user-456', environment: 'production' };
    if (localClient.get(serverContext, 'new-api-endpoint', false)) {
      console.log('Server: Using new API endpoint for this context!');
    } else {
      console.log('Server: Using old API endpoint.');
    }

    const featureVariant = localClient.get({ userId: 'another-user-789' }, 'feature-x-variant', 'control');
    console.log(`Server: 'feature-x-variant' for 'another-user-789' is: ${featureVariant}`);

  } catch (error) {
    console.error("Error with TgglLocalClient initialization or flag evaluation:", error);
  } finally {
    // Important: Stop background processes for local client when done (e.g., on server shutdown)
    localClient.stopReporting?.(); 
    localClient.stopRefreshing?.();
  }
}

runFeatureFlagExample();

view raw JSON →