JavaScript (Node)

The @hyphen/sdk is Hyphen's native server-side Node.js SDK for feature flag evaluation and management. It provides a streamlined, purpose-built interface for backend services, APIs, and server-rendered applications without requiring OpenFeature.

Table of Contents

Installation

Install the native SDK:

npm install @hyphen/sdk

Setup and Initialization

Initialize the Hyphen client with your API key and configuration:

import { HyphenClient } from '@hyphen/sdk';

const client = new HyphenClient({
  apiKey: 'your-api-key',
  application: 'your-application-name',
  environment: 'production' // or project environment ID
});

await client.initialize();

Usage

Basic Feature Flag Evaluation

Check if a feature is enabled for a user:

const isEnabled = await client.isEnabled('new-checkout-flow', {
  userId: 'user-123',
  email: '[email protected]'
});

if (isEnabled) {
  // Execute new checkout flow
  processNewCheckout();
} else {
  // Use legacy checkout
  processLegacyCheckout();
}

Getting Feature Flag Details

Retrieve detailed information about a feature flag evaluation:

const flagDetails = await client.getFlagDetails('feature-flag-key', {
  userId: 'user-123',
  customAttributes: {
    subscriptionLevel: 'premium',
    region: 'us-east'
  }
});

console.log(flagDetails.enabled); // true or false
console.log(flagDetails.variant); // variant name if applicable
console.log(flagDetails.metadata); // additional flag metadata

Targeting Context

Provide rich context for advanced targeting rules:

const context = {
  userId: 'user-456',
  email: '[email protected]',
  name: 'Jane Smith',
  ipAddress: '203.0.113.42',
  customAttributes: {
    role: 'admin',
    subscriptionLevel: 'enterprise',
    region: 'us-west'
  }
};

const showAdminPanel = await client.isEnabled('admin-panel-v2', context);

Configuration Options

Option Type Required Description
apiKey string Yes Your Hyphen API key
application string Yes The application id or alternate ID
environment string Yes The environment identifier (project environment ID or alternate)
enableTelemetry boolean No Enable/disable usage telemetry (default: true)
cacheTTL number No Cache time-to-live in seconds (default: 300)

Open Feature

The Hyphen Toggle Provider for Node.js is an OpenFeature provider implementation that enables seamless feature flag evaluation. This section covers setup, usage, and configuration specific to the JavaScript implementation.

Installation

Install the provider and the OpenFeature server SDK:

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

Setup and Initialization

To begin using the Hyphen Provider, follow these steps:

  1. Import the required modules.

  2. Configure the provider with your publicKey , application name and environment.
    You can specify the environment in one of two formats:

    • Alternate ID (e.g., "production", "staging") — the environment in which your application is running.
    • Project Environment ID (e.g., pevr_abc123) — useful for internal references.
  3. Register the provider with OpenFeature.

import { OpenFeature } from '@openfeature/server-sdk';  
import { HyphenProvider, type HyphenProviderOptions } from '@hyphen/openfeature-server-provider';

const publicKey = "your-public-key-here";

const options: HyphenProviderOptions = {
  application: 'your-application-name',
  environment: 'production', // or project environment ID
};

await OpenFeature.setProviderAndWait(new HyphenProvider(publicKey, options));
  1. Configure the context needed for feature targeting evaluations, incorporating user or application context.
const context: HyphenEvaluationContext = {  
  targetingKey: 'user-123',  
  ipAddress: '203.0.113.42',  
  customAttributes: {  
    subscriptionLevel: 'premium',  
    region: 'us-east',  
  },  
  user: {  
    id: 'user-123',  
    email: '[email protected]',  
    name: 'John Doe',  
    customAttributes: {  
      role: 'admin',  
    },  
  },  
};

Usage

Evaluation Context Example

Evaluate a feature flag using the OpenFeature client and context information:

const client = OpenFeature.getClient();
const flagDetailsWithContext = await client.getBooleanDetails('feature-flag-key', false, context);

console.log(flagDetailsWithContext.value); // true or false

Configuration

Options

Option Type Required Description
application string Yes The application id or alternate ID.
environment string Yes The environment identifier for the Hyphen project (project environment ID or alternateId).
horizonUrls string[] No Hyphen Horizon URLs for fetching flags.
enableToggleUsage boolean No Enable/disable telemetry (default: true).
cache object No Configuration for caching feature flag evaluations.

Cache Configuration

The cache option accepts the following properties:

Property Type Default Description
ttlSeconds number 300 Time-to-live in seconds for cached flag evaluations.
generateCacheKeyFn Function - Custom function to generate cache keys from evaluation context.

Example with cache configuration:

const options: HyphenProviderOptions = {
  application: 'your-application-name',
  environment: 'production',
  cache: {
    ttlSeconds: 600, // 10 minutes
    generateCacheKeyFn: (context: HyphenEvaluationContext) => {
      return `${context.targetingKey}-${context.user?.id}`;
    },
  },
};

Context

Provide an EvaluationContext to pass contextual data for feature evaluation.

Field Type Required Description
targetingKey string Yes The key used for caching the evaluation response.
ipAddress string No The IP address of the user making the request.
customAttributes Record No Custom attributes for additional contextual information.
user object No An object containing user-specific information for the evaluation.
user.id string No The unique identifier of the user.
user.email string No The email address of the user.
user.name string No The name of the user.
user.customAttributes Record No Custom attributes specific to the user.

Example

const context: HyphenEvaluationContext = {  
  targetingKey: 'user-456',  
  customAttributes: {  
    subscriptionLevel: 'premium',  
  },  
};

const flagDetails = await client.getBooleanDetails('enable-discounts', false, context);

if (flagDetails.value) {  
  console.log('Discounts enabled for premium users.');  
} else {  
  console.log('No discounts available.');  
}