The FlagBox Node.js SDK provides a simple and powerful way to integrate feature flags into your Node.js applications. Control your application's features in real-time with a clean and intuitive API.

Installation

npm install @flagbox/node

Quick Start

import { FlagBoxClient } from 'flagbox';

// Initialize the client
const flagbox = FlagBoxClient(
    'https://api.flagbox.io',
    'your-api-key'
);

// Get a feature flag value
const value = await flagbox.getFlag('my-feature', false);
console.log('Feature enabled:', value);

Configuration

The FlagBox client can be configured with the following options:

  • apiUrl (required): The base URL for the FlagBox API
  • apiKey (required): Your FlagBox API key
  • globalContext (optional): Default context for all flag evaluations

API Reference

getFlag

Asynchronously get a feature flag value. Returns a boolean value indicating if the feature is enabled.

// Basic usage
const isEnabled = await flagbox.getFlag('my-feature', false);

// With context
const isEnabled = await flagbox.getFlag('my-feature', false, {
    userId: 'user-123',
    plan: 'premium'
});

Error Handling

The SDK handles errors gracefully by returning the default value when:

  • The API request fails
  • The API returns a non-200 status code
  • Network errors occur
  • The feature flag type is not supported

All errors are logged to the console for debugging purposes.

Best Practices

  • Initialize the FlagBox client once and reuse it throughout your application
  • Use meaningful flag keys that describe the feature's purpose
  • Always provide a default value when using getFlag()
  • Use globalContext for context values that are common across all flag evaluations
  • Keep context values under 256 characters to ensure they are included in the request

Framework Integration

The SDK can be easily integrated with popular Node.js web frameworks:

// Express integration
import express from 'express';
import { FlagBoxClient } from 'flagbox';

const app = express();
const flagbox = FlagBoxClient(
    'https://api.flagbox.io',
    'your-api-key'
);

app.get('/feature', async (req, res) => {
    const isEnabled = await flagbox.getFlag('my-feature', false, {
        userId: req.query.userId
    });
    res.json({ enabled: isEnabled });
});

// Next.js integration
import { FlagBoxClient } from 'flagbox';

const flagbox = FlagBoxClient(
    'https://api.flagbox.io',
    'your-api-key'
);

export default async function handler(req, res) {
    const isEnabled = await flagbox.getFlag('my-feature', false, {
        userId: req.query.userId
    });
    res.status(200).json({ enabled: isEnabled });
}