Skip to main content

Server-side feature gating

Step 1: Get the Statsig server secret key#

To get the Statsig server secret key,

  • Log into the Statsig console at https://console.statsig.com

  • Click on the Project Settings gear icon next to your account settings at the top right corner of the page as shown below

    image

  • Click on the API Keys tab

  • Copy the active Server Secret Key

Step 2: Install the SDK#

You can install the statsig SDK via npm or yarn:
npm install statsig-node //using npm
yarn add statsig-node //using yarn

Step 3: Initialize the SDK#

To initialize the SDK, copy the following in your server side application code:

const statsig = require('statsig-node');
await statsig.initialize(
'SERVER_SECRET_KEY',
{ environment: { tier: 'staging' } }, // optional, pass options here if needed
});

Step 4: Check the feature gate#

To implement a feature gate, you must include a feature gate check in your application code.

A feature gate check returns a Boolean value. A feature gate is closed/off and a gate check returns false by default. After you have created a rule to target a set of users, the feature gate check returns true when you perform a check for an eligible user. You can use this return value to expose the eligible user to the feature as shown below.

const user = {
userID: 'LOGGED_IN_USER_ID',
email: 'LOGGED_IN_USER_EMAIL',
...
};
const showNewDesign = await statsig.checkGate(user, 'FEATURE_GATE_NAME');
if (showNewDesign) {
// show new design here
} else {
// show old design here
}
### Step 5 (Optional): Log an event

You can optionally log an event to capture any metrics that show the impact of your feature.

statsig.logEvent(user, 'EVENT_NAME', 'EVENT_VALUE', {'price': '9.99', 'item_name': 'diet_coke_48_pack'});