Flagsmith CLI

0.2.3 · active · verified Wed Apr 22

The `flagsmith-cli` is a command-line interface tool designed to fetch feature flags and environment configurations from the Flagsmith API. Its primary use case is to generate a local JSON file, typically `flagsmith.json`, which can then be baked into an application as default flag state. This enables defensive coding practices, provides offline support, and facilitates CI/CD integration for feature flag management. The current stable version is `0.2.3`. Releases occur on an as-needed basis, focusing on bug fixes, dependency updates, and minor feature additions. The CLI is built on Oclif and ships with TypeScript types, targeting Node.js environments `>=12.0.0`. It serves as a crucial component for applications leveraging Flagsmith in scenarios requiring static flag data.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates installing the CLI, fetching flags into a `flagsmith.json` file, and then importing and initializing the Flagsmith client with these default flags for application use.

// 1. Install the CLI as a dev dependency
// npm install flagsmith-cli --save-dev

// 2. In your CI/CD or package.json "postinstall" script,
//    set the environment key and run the CLI to fetch flags.
//    (Replace 'YOUR_FLAGSMITH_ENVIRONMENT_KEY' with your actual key or use process.env)

// Example using a shell command (e.g., in a CI script or `npm run get-flags`):
// export FLAGSMITH_ENVIRONMENT="${process.env.FLAGSMITH_ENVIRONMENT_KEY ?? 'YOUR_FLAGSMITH_ENVIRONMENT_KEY'}"
// npx flagsmith get --output ./src/flagsmith.json --pretty

// 3. In your application code (e.g., src/main.ts or src/App.tsx):
import flagsmith from 'flagsmith'; // Flagsmith client SDK (install separately: npm install flagsmith)
import state from './flagsmith.json'; // The JSON file generated by flagsmith-cli

async function initializeFlagsmith() {
  await flagsmith.init({
    state, // Initial state fetched by the CLI
    environmentID: state.environmentID, // Extracted from the generated JSON
    // If you need the SDK to fetch live flags later, ensure API URL is correct:
    // apiUrl: 'https://edge.api.flagsmith.com/api/v1/', // Default Flagsmith API
  });

  console.log('Flagsmith initialized with default state.');

  // Example: Check a feature flag
  const myFeatureEnabled = flagsmith.hasFeature('example_feature_name');
  console.log(`Is 'example_feature_name' enabled? ${myFeatureEnabled}`);

  // Get a flag value
  const welcomeMessage = flagsmith.getValue('welcome_message');
  console.log(`Welcome message: ${welcomeMessage}`);
}

initializeFlagsmith();

view raw JSON →