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.
With Local Evaluation
const { OpenFeature } = require('@openfeature/server-sdk');
const { MixpanelProvider } = require('@mixpanel/openfeature-server-provider');
// Create provider with local evaluation (automatically starts polling)
const provider = MixpanelProvider.createLocal('YOUR_PROJECT_TOKEN', {
enable_polling: true,
polling_interval_in_seconds: 60
});
await OpenFeature.setProviderAndWait(provider);
OpenFeature.setContext({ distinct_id: 'user-123', plan: 'premium' });
const client = OpenFeature.getClient();
const showNewUI = await client.getBooleanValue('new-ui', false);With Remote Evaluation
const provider = MixpanelProvider.createRemote('YOUR_PROJECT_TOKEN', {
api_host: 'api.mixpanel.com'
});
await OpenFeature.setProviderAndWait(provider);
OpenFeature.setContext({ distinct_id: 'user-123' });
const client = OpenFeature.getClient();
const variant = await client.getStringValue('checkout-flow', 'control');With an Existing Flags Provider
If you already have a configured Mixpanel instance, you can wrap its flags provider directly:
const Mixpanel = require('mixpanel');
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();
const provider = new MixpanelProvider(mixpanel.local_flags);
await OpenFeature.setProviderAndWait(provider);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
The provider merges the base context (set via OpenFeature.setContext() during initialization) with any per-evaluation context. Per-evaluation context values override the base context for overlapping keys.
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 the base context after initialization, call
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()delegates to the underlying flags provider’sshutdown()method if available.- The reason code for successful evaluations is
STATIC.
Was this page useful?