Flagix Docs
Sdk

JavaScript SDK

Complete reference for the Flagix JavaScript SDK

The Flagix JavaScript SDK provides a powerful way to integrate feature flags into any JavaScript application.

Core API

Flagix.initialize()

Initializes the Flagix SDK with your configuration.

async initialize(options: FlagixClientOptions): Promise<void>

Parameters

  • options - Configuration object for the SDK

Options

interface FlagixClientOptions {
  apiKey: string;                    // Required: Your API key
  initialContext?: EvaluationContext; // Optional: Initial context
  logs?: {
    level?: "none" | "error" | "warn" | "info"; // Optional: default "none"
  };
}

Example

import { Flagix } from "@flagix/js-sdk";

await Flagix.initialize({
  apiKey: "your-api-key",
  initialContext: {
    userId: "user_123",
    email: "user@example.com",
    plan: "premium"
  },
});

Flagix.evaluate()

Evaluates a feature flag based on the current context.

evaluate<T extends VariationValue>(
  flagKey: string,
  contextOverrides?: EvaluationContext
): T | null

Parameters

  • flagKey - The key of the flag to evaluate
  • contextOverrides - Optional context to override the global context

Returns

  • The evaluated value of the flag, or null if the flag doesn't exist

Examples

// Basic evaluation
const isNewFeatureEnabled = Flagix.evaluate("new-feature");

// With context overrides
const isAdminFeature = Flagix.evaluate("admin-panel", {
  userId: "admin_user",
  role: "admin"
});

Flagix.track()

Records a custom event for analytics and conversion tracking.

track(
  eventName: string,
  properties?: Record<string, unknown>,
  contextOverrides?: EvaluationContext
): void

Parameters

  • eventName - The name of the event to track
  • properties - Optional additional properties
  • contextOverrides - Optional context overrides

Examples

// Simple event tracking
Flagix.track("button_click");

// Event with properties
Flagix.track("purchase_completed", {
  amount: 99.99,
  currency: "USD",
  productId: "prod_123"
});

// Event with context overrides
Flagix.track("admin_action", 
{
  action: "delete_user"
}, 
{
  userId: "admin_456",
  role: "super_admin"
});

Flagix.setContext()

Updates the global evaluation context and triggers re-evaluation of all flags.

setContext(newContext: EvaluationContext): void

Parameters

  • newContext - New context attributes to merge with existing context

Reactivity

Calling setContext() instantly re-evaluates all cached flags with the new context. Any active listeners subscribed via onFlagUpdate() are triggered, enabling reactive updates in your application.

Examples

// Update user context after login
Flagix.setContext({
  userId: "user_789",
  email: "newuser@example.com",
  isAuthenticated: true,
  plan: "premium"
});
// → All flags re-evaluate with new context
// → All onFlagUpdate listeners are triggered

// Add additional attributes
Flagix.setContext({
  lastLogin: new Date().toISOString(),
  sessionId: "session_abc123"
});
// → Context merges with existing values
// → Reactive updates triggered again

Flagix.identify()

Replaces the entire global evaluation context, clearing all previous context values.

identify(newContext: EvaluationContext): void

Parameters

  • newContext - New context object that will completely replace the existing context

Use Cases

Unlike setContext() which merges with existing context, identify() is useful when you need to:

  • Clear user context on logout: Remove all user-specific attributes
  • Switch between different users: Replace one user's context entirely with another's
  • Reset evaluation context: Start fresh with a completely new context

Reactivity

Calling identify() instantly replaces the global context and triggers re-evaluation of all cached flags. Any active listeners subscribed via onFlagUpdate() are triggered.

Examples

// On user logout - completely clear context
Flagix.identify({});
// → All previous context is removed
// → All flags re-evaluate with empty context
// → All onFlagUpdate listeners are triggered

// Switch to a different user
Flagix.identify({
  userId: "new_user_456",
  email: "newuser@example.com",
  plan: "basic"
});
// → Previous context is completely replaced
// → No old user data persists
// → Flags re-evaluate with new user context

// Distinguish between setContext and identify
// Use setContext for incremental updates
Flagix.setContext({ lastActivity: new Date() });

// Use identify for complete context replacement
Flagix.identify({ userId: "user_new", role: "admin" });

Flagix.onFlagUpdate()

Subscribes to real-time flag updates triggered by setContext() or server-sent events.

onFlagUpdate(listener: (flagKey: string) => void): void

Parameters

  • listener - Callback function called when any flag is updated

Creating Reactive UI

In vanilla JavaScript applications, use onFlagUpdate() to create reactive behavior when user identity or attributes change:

// Subscribe to flag updates
Flagix.onFlagUpdate((flagKey) => {
  console.log(`Flag ${flagKey} was updated`);
  
  // Re-evaluate the flag
  const newValue = Flagix.evaluate(flagKey);
  console.log(`New value:`, newValue);
  
  // Update DOM elements
  updateUIForFlag(flagKey, newValue);
});

// Helper to update UI
function updateUIForFlag(flagKey, value) {
  if (flagKey === "premium-features") {
    const element = document.getElementById("premium-section");
    if (value) {
      element.classList.remove("hidden");
    } else {
      element.classList.add("hidden");
    }
  }
}

// Later: When user context changes, UI automatically updates
Flagix.setContext({ isPremium: true });
// → onFlagUpdate fires for all flags
// → DOM updates instantly without page refresh

This pattern enables dynamic feature control in single-page applications without requiring a full page reload.

Flagix.offFlagUpdate()

Unsubscribes from flag updates.

offFlagUpdate(listener: (flagKey: string) => void): void

Example

const updateListener = (flagKey) => {
  console.log(`Flag ${flagKey} updated`);
};

Flagix.onFlagUpdate(updateListener);

// Later, remove the listener
Flagix.offFlagUpdate(updateListener);

Flagix.isInitialized()

Checks if the SDK is properly initialized.

isInitialized(): boolean

Example

if (!Flagix.isInitialized()) {
  console.log("Flagix not initialized yet");
  await Flagix.initialize(config);
}

Flagix.close()

Cleans up resources and closes connections.

close(): void

Example

// In a React component cleanup
useEffect(() => {
  return () => {
    Flagix.close();
  };
}, []);

// In Node.js process shutdown
process.on('SIGTERM', () => {
  Flagix.close();
  process.exit(0);
});

Type Definitions

EvaluationContext

type EvaluationContext ={
  [x: string]: any;
}

VariationValue

type VariationValue = string | number | boolean;

Check out our React SDK.

On this page