Node.js Integration Guide

Node.js Feature Flags

Feature flags for Express, Fastify, NestJS, and any Node.js backend

Quick Start

1. Get your API key

Sign up at flagbit.anethoth.com to get your SDK key. The free tier includes 1,000 evaluations/day.

2. Install (optional)

npm install node-fetch

3. Evaluate flags

const fetch = require('node-fetch');

const FLAGBIT_SDK_KEY = process.env.FLAGBIT_SDK_KEY;

async function getFlag(flagKey, context = {}) {
  try {
    const res = await fetch(
      'https://flagbit.anethoth.com/api/v1/evaluate', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'X-SDK-Key': FLAGBIT_SDK_KEY
      },
      body: JSON.stringify({ flag_key: flagKey, context })
    });
    const data = await res.json();
    return data.value;
  } catch {
    return false; // Safe default
  }
}

// Express middleware
function featureGate(flagKey) {
  return async (req, res, next) => {
    const enabled = await getFlag(flagKey, {
      user_id: req.user?.id,
      plan: req.user?.plan
    });
    req.flags = { ...req.flags, [flagKey]: enabled };
    next();
  };
}

// Usage in Express routes
const express = require('express');
const app = express();

app.get('/api/search',
  featureGate('elastic-search'),
  async (req, res) => {
    if (req.flags['elastic-search']) {
      return res.json(await elasticSearch(req.query.q));
    }
    return res.json(await sqlSearch(req.query.q));
  }
);

// NestJS guard
@Injectable()
class FeatureFlagGuard implements CanActivate {
  async canActivate(context) {
    const flagKey = Reflect.getMetadata('feature_flag', context.getHandler());
    return await getFlag(flagKey);
  }
}

Use Cases for Node.js

Express Middleware

Create reusable middleware that gates routes behind feature flags — clean, composable, and testable.

API Migration

Route requests to new API handlers based on flags — migrate users gradually without breaking changes.

Database Migration

Read from both old and new database schemas simultaneously, compare results, then cut over.

Rate Limit Tiers

Dynamically adjust rate limits for different user segments without redeploying.

How FlagBit Works

1

Create a Flag

Define flags in your FlagBit dashboard with targeting rules and rollout percentages.

2

Evaluate in Code

Call the evaluate endpoint from your Node.js app with user context for targeted rollouts.

3

Toggle Instantly

Enable, disable, or adjust rollouts in real-time. No redeployment needed.

FAQ

Yes — implement a custom guard that evaluates flags via FlagBit API. Decorate controllers with @SetMetadata('feature_flag', 'flag-key').

Cache flag values in-memory with a TTL (e.g., 60 seconds). Only call FlagBit API when the cache expires.

Yes — FlagBit is a stateless HTTP API. Works perfectly with AWS Lambda, Vercel Functions, Cloudflare Workers.

Call the evaluate endpoint for each flag. For critical paths, evaluate in parallel with Promise.all().

Start using feature flags in Node.js

Free tier includes 1 project, 10 flags, and 1,000 evaluations/day. No credit card required.

Get Your Free API Key