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
- You are on an Enterprise subscription plan
- You have the Mixpanel Node.js SDK installed (minimum version
v0.20.0) - You have your Project Token from your Mixpanel Project Settings
Installation
npm install @mixpanel/openfeature-server-provider @openfeature/server-sdkUsage
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 Method | JavaScript Type |
|---|---|
getBooleanValue / getBooleanDetails | boolean |
getStringValue / getStringDetails | string |
getNumberValue / getNumberDetails | number |
getObjectValue / getObjectDetails | object (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.
targetingKeyhas 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_idin 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 Code | Condition |
|---|---|
PROVIDER_NOT_READY | The provider has not been initialized |
FLAG_NOT_FOUND | The requested flag does not exist |
TYPE_MISMATCH | The 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?