Skip to main content
Version: Next

Node.js

NPM Version NPM Downloads

This provider supports both in-process and remote evaluation modes, offering flexibility for different deployment scenarios.

Install dependencies

The first things we will do is install the Open Feature SDK and the GO Feature Flag provider.

yarn add @openfeature/server-sdk @openfeature/go-feature-flag-provider

Initialize your Open Feature client

To evaluate the flags you need to have an Open Feature configured in your app. This code block shows you how you can create a client that you can use in your application.

const { OpenFeature } = require("@openfeature/server-sdk");
const { GoFeatureFlagProvider, EvaluationType } = require("@openfeature/go-feature-flag-provider");

// Initialize the provider
const goFeatureFlagProvider = new GoFeatureFlagProvider({
endpoint: 'http://localhost:1031/', // DNS of your instance of relay proxy
evaluationType: EvaluationType.Remote,
});

// Register the provider
OpenFeature.setProvider(goFeatureFlagProvider);

// Get a client
const featureFlagClient = OpenFeature.getClient('my-app');

In-Process Evaluation

For high-performance scenarios where you want to evaluate flags locally:

const { GoFeatureFlagProvider, EvaluationType } = require("@openfeature/go-feature-flag-provider");

const goFeatureFlagProvider = new GoFeatureFlagProvider({
endpoint: 'http://localhost:1031/',
evaluationType: EvaluationType.InProcess,
flagChangePollingIntervalMs: 30000, // Poll every 30 seconds
});

Configuration options

OptionTypeDefaultDescription
endpointstringRequiredThe endpoint of the GO Feature Flag relay-proxy
evaluationTypeEvaluationTypeInProcessEvaluation mode: InProcess or Remote
timeoutnumber10000HTTP request timeout in milliseconds
flagChangePollingIntervalMsnumber120000Polling interval for configuration changes
dataFlushIntervalnumber120000Data collection flush interval
maxPendingEventsnumber10000Maximum pending events before flushing
disableDataCollectionbooleanfalseDisable data collection entirely
apiKeystringundefinedAPI key for authentication
exporterMetadataExporterMetadataundefinedCustom metadata for events
fetchImplementationFetchAPIundefinedCustom fetch implementation

Evaluation types

InProcess Evaluation

  • Performance: Fastest evaluation with local caching
  • Network: Minimal network calls, only for configuration updates and tracking
  • Use Case: High-performance applications, real-time evaluation

Remote Evaluation

  • Performance: Network-dependent evaluation
  • Network: Each evaluation requires a network call, works well with side-cars or in the edge
  • Use Case: Centralized control

Evaluate your flag

This code block explains how you can create an EvaluationContext and use it to evaluate your flag.

note

In this example, we are evaluating a boolean flag, but other types are also available.

Refer to the Open Feature documentation to know more about it.

// Context of your flag evaluation.
// With GO Feature Flag you MUST have a targetingKey that is a unique identifier of the user.
const userContext = {
targetingKey: '1d1b9238-2591-4a47-94cf-d2bc080892f1', // user unique identifier (mandatory)
firstname: 'john',
lastname: 'doe',
email: 'john.doe@gofeatureflag.org',
admin: true, // this field is used in the targeting rule of the flag "flag-only-for-admin"
// ...
};

const adminFlag = await featureFlagClient.getBooleanValue('flag-only-for-admin', false, userContext);
if (adminFlag) {
// flag "flag-only-for-admin" is true for the user
} else {
// flag "flag-only-for-admin" is false for the user
}

Advanced usage

Custom context and targeting

const context = {
targetingKey: 'user-123',
email: 'john.doe@example.com',
firstname: 'John',
lastname: 'Doe',
anonymous: false,
professional: true,
rate: 3.14,
age: 30,
company_info: {
name: 'my_company',
size: 120,
},
labels: ['pro', 'beta'],
};

const flagValue = await featureFlagClient.getBooleanValue('my-feature-flag', false, context);

Data collection and analytics

The provider automatically collects evaluation data. You can customize this behavior:

const goFeatureFlagProvider = new GoFeatureFlagProvider({
endpoint: 'http://localhost:1031/',
evaluationType: EvaluationType.Remote,
disableDataCollection: false, // Enable data collection
dataFlushInterval: 20000, // Flush every 20 seconds
maxPendingEvents: 5000, // Max 5000 pending events
});

Custom exporter metadata

Add custom metadata to your evaluation events:

import { ExporterMetadata } from '@openfeature/go-feature-flag-provider';

const metadata = new ExporterMetadata()
.add('environment', 'production')
.add('version', '1.0.0')
.add('region', 'us-east-1');

const goFeatureFlagProvider = new GoFeatureFlagProvider({
endpoint: 'http://localhost:1031/',
evaluationType: EvaluationType.Remote,
exporterMetadata: metadata,
});

Flag types supported

The provider supports all OpenFeature flag types:

Boolean flags

const isEnabled = await featureFlagClient.getBooleanValue('feature-flag', false, context);
const details = await featureFlagClient.getBooleanDetails('feature-flag', false, context);

String flags

const message = await featureFlagClient.getStringValue('welcome-message', 'Hello!', context);
const details = await featureFlagClient.getStringDetails('welcome-message', 'Hello!', context);

Number flags

const percentage = await featureFlagClient.getNumberValue('discount-percentage', 0, context);
const details = await featureFlagClient.getNumberDetails('discount-percentage', 0, context);

Object flags

const config = await featureFlagClient.getObjectValue('user-config', {}, context);
const details = await featureFlagClient.getObjectDetails('user-config', {}, context);

Tracking events

The provider supports custom event tracking:

// Track a custom event
featureFlagClient.track('user_action', context, {
action: 'button_click',
page: 'homepage',
timestamp: Date.now(),
});

NextJS integration

warning

If you're using NextJS with in-process evaluation mode, you might encounter issues loading the WASM binary.

In the server side, you might have an error like this (only if you are using the in-process evaluation mode):

WasmNotLoadedException: Failed to load WASM binary: WASM file not found

Because of the specificity of NextJS, you need to add the following code to your next.config.ts file:

const nextConfig: NextConfig = {
// ...
serverExternalPackages: ["@openfeature/go-feature-flag-provider"],
// ...
};

This will tell NextJS to use the @openfeature/go-feature-flag-provider package as an external package and not to bundle it with the application, which is the best way to avoid the WASM binary not found error.

Features status

StatusFeatureDescription
Remote EvaluationThe provider is calling the remote server to evaluate the feature flags.
Tracking Flag EvaluationThe provider is tracking all the evaluations of your feature flags and you can export them using an exporter.
Configuration Change UpdatesThe provider is able to update the configuration based on the configuration, it means that the provider is able to react to any feature flag change on your configuration.
Provider Events ReactionsYou can add an event handler to the provider to react to the provider events.
ImplementedIn-progressNot implemented yet

Contribute to the provider

You can find the source of the provider in the open-feature/js-sdk-contrib repository.