DocsTracking MethodsSDKsNode.jsOpenFeature Provider (Node.js)

OpenFeature Provider (Node.js)

Overview

The Mixpanel OpenFeature provider allows you to use Mixpanel Feature Flags through the standardized OpenFeature API. This provider wraps the Mixpanel Node.js SDK’s feature flags, letting you use the OpenFeature Server SDK with Mixpanel as the backend.

For the core Feature Flags SDK guide, see Feature Flags (Node.js).

Prerequisites

Installation

npm install @mixpanel/openfeature-server-provider @openfeature/server-sdk

Usage

The provider wraps either a LocalFeatureFlagsProvider or RemoteFeatureFlagsProvider from the Mixpanel SDK. Initialize Mixpanel with feature flags first, then register the provider with OpenFeature.

With Local Evaluation

const Mixpanel = require('mixpanel');
const { OpenFeature } = require('@openfeature/server-sdk');
const { MixpanelProvider } = require('@mixpanel/openfeature-server-provider');
 
const mixpanel = Mixpanel.init('YOUR_PROJECT_TOKEN', {
    local_flags_config: {
        enable_polling: true,
        polling_interval_in_seconds: 60
    }
});
 
await mixpanel.local_flags.startPollingForDefinitions();
 
// Register the provider with the user context for evaluation
await OpenFeature.setProviderAndWait(new MixpanelProvider(mixpanel.local_flags));
OpenFeature.setContext({ distinct_id: 'user-123', plan: 'premium' });
 
const client = OpenFeature.getClient();
const showNewUI = await client.getBooleanValue('new-ui', false);

With Remote Evaluation

const mixpanel = Mixpanel.init('YOUR_PROJECT_TOKEN', {
    remote_flags_config: {
        api_host: 'api.mixpanel.com'
    }
});
 
await OpenFeature.setProviderAndWait(new MixpanelProvider(mixpanel.remote_flags));
OpenFeature.setContext({ distinct_id: 'user-123' });
 
const client = OpenFeature.getClient();
const variant = await client.getStringValue('checkout-flow', 'control');

Supported Flag Types

OpenFeature MethodJavaScript Type
getBooleanValue / getBooleanDetailsboolean
getStringValue / getStringDetailsstring
getNumberValue / getNumberDetailsnumber
getObjectValue / getObjectDetailsobject (non-null, non-array)

Context and Identity

Context is set globally when registering the provider, not per-evaluation. Per-evaluation context passed to individual flag evaluations is ignored.

  • targetingKey has no special meaning in this provider. It is treated as a regular context property.
  • Identity should be managed through the Mixpanel SDK (e.g., setting distinct_id in the context).
  • To update context after initialization, set a new global context via OpenFeature.setContext().

Error Handling

The provider returns the default value on all errors, with an error code indicating the cause:

Error CodeCondition
PROVIDER_NOT_READYThe provider has not been initialized
FLAG_NOT_FOUNDThe requested flag does not exist
TYPE_MISMATCHThe flag value does not match the requested type

Use getDetails() methods instead of getValue() to inspect error codes:

const details = await client.getBooleanDetails('my-flag', false);
if (details.errorCode) {
    console.log(`Flag error: ${details.errorCode} - ${details.errorMessage}`);
}

Lifecycle

  • initialize() stores the global context for all subsequent evaluations.
  • onClose() is a no-op. The Mixpanel SDK manages its own lifecycle.
  • The reason code for successful evaluations is STATIC.

Was this page useful?