Statsig Node.js SDK

6.5.1 · active · verified Sun Apr 19

The Statsig Node.js SDK (current stable version 6.5.1) provides robust tools for managing feature gates, dynamic configurations, and A/B testing within multi-user server environments. It enables developers to implement continuous deployment strategies and understand the impact of new features on key performance indicators. The package sees frequent updates, typically releasing bug fixes and minor improvements multiple times a month, as evidenced by recent changelogs. Key differentiators include its comprehensive A/B testing capabilities, detailed analytics, and a focus on server-side performance and data consistency, contrasting with client-side SDKs that handle single-user contexts. It is designed for use cases where server-side evaluation of features is critical for performance and security.

Common errors

Warnings

Install

Imports

Quickstart

Initializes the Statsig SDK, checks a feature gate, fetches a dynamic config, logs an event, and gracefully shuts down. Demonstrates core SDK functionality.

import { Statsig, StatsigUser } from 'statsig-node';

const STATSIG_SECRET_KEY = process.env.STATSIG_SERVER_SECRET_KEY ?? '';

async function runStatsigExample() {
  if (!STATSIG_SECRET_KEY) {
    console.error('STATSIG_SERVER_SECRET_KEY environment variable is not set.');
    return;
  }

  try {
    await Statsig.initialize(STATSIG_SECRET_KEY, {
      localMode: false, // Set to true for local testing without network calls
      // Additional options like 'environment', 'api' can be passed here
    });

    const user: StatsigUser = {
      userID: 'unique-user-id-123',
      email: 'test@example.com',
      country: 'US',
      custom: { membershipTier: 'premium' },
      // Additional custom properties can be added
    };

    if (await Statsig.checkGate(user, 'my_new_feature_gate')) {
      console.log('Feature "my_new_feature_gate" is ENABLED for the user!');
    } else {
      console.log('Feature "my_new_feature_gate" is DISABLED for the user.');
    }

    const config = await Statsig.getConfig(user, 'product_pricing_config');
    console.log('Product pricing level:', config.get('priceLevel', 'standard'));

    Statsig.logEvent(user, 'checkout_completed', 99.99, { currency: 'USD', items: 3 });

  } catch (error) {
    console.error('Statsig initialization or usage failed:', error);
  } finally {
    await Statsig.shutdown(); // Important to call shutdown for graceful exit and event flushing
  }
}

runStatsigExample();

view raw JSON →