LaunchDarkly REST API Client
This package provides a TypeScript/JavaScript client for interacting with the LaunchDarkly REST API, automatically generated from their OpenAPI specification. It is built on `axios` and supports both Node.js and browser environments, with compatibility for ES5/ES6 and CommonJS/ES6 module systems. The current stable version is `22.0.0`, with frequent releases mirroring updates to the LaunchDarkly API. This client is specifically designed for custom integrations, data export, and automating feature flag workflows, such as programmatic flag management. It is crucial to note that this client library should *not* be used for integrating feature flags directly into web or mobile applications; dedicated SDKs are available for that purpose. It serves as a programmatic interface for administrative tasks rather than a runtime feature flag evaluation tool.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'data')
cause An API request failed, and the error object thrown by axios does not have the expected structure for direct access to `error.response.data`, or `error.response` is `undefined`.fixWhen handling API errors, check if `error.response` and `error.response.data` exist before accessing them, especially when expecting specific error messages from the server. For example: `error.response?.data || error.message`. -
Error: Request failed with status code 401
cause The provided API access token is missing, invalid, or does not have sufficient permissions for the requested operation.fixVerify that your `LAUNCHDARKLY_API_KEY` is correctly set and is a valid access token. Check the token's permissions in the LaunchDarkly UI to ensure it has the necessary scopes for the API endpoints you are calling. -
TypeError: __importDefault is not a function
cause Attempting to use ES module imports (`import ... from 'pkg'`) in a CommonJS environment without proper transpilation (e.g., via Babel or TypeScript targeting `commonjs` but configured incorrectly).fixEnsure your `tsconfig.json` `module` option is set correctly for your target environment (e.g., `"commonjs"` for Node.js projects, `"esnext"` or `"es2020"` for browser environments with bundlers). If using CommonJS, use `const { ... } = require('pkg');` syntax.
Warnings
- gotcha This client library is exclusively for programmatic interaction with the LaunchDarkly REST API (e.g., custom integrations, automation). It must NOT be used to integrate feature flags into client-side (web/mobile) or server-side applications for flag evaluation. For in-application feature flagging, use the official LaunchDarkly SDKs.
- breaking The client library updates frequently to align with the latest version of the LaunchDarkly REST API. Major version bumps (`vX.0.0`) typically indicate significant API changes, which may include new endpoints, modified request/response bodies, or removal of deprecated features, potentially breaking existing integrations.
- gotcha API access tokens are sensitive credentials. When using this client, ensure API keys are stored securely (e.g., environment variables, secret management services) and never hardcoded or exposed in client-side code.
- gotcha The API client is generated using OpenAPI tools, which means the structure of request bodies and response objects strictly adheres to the OpenAPI specification. Mismatches in property names, types, or required fields will lead to validation errors or unexpected behavior.
Install
-
npm install launchdarkly-api-typescript -
yarn add launchdarkly-api-typescript -
pnpm add launchdarkly-api-typescript
Imports
- Configuration
const Configuration = require('launchdarkly-api-typescript').Configuration;import { Configuration } from 'launchdarkly-api-typescript'; - AIConfigsApi
import AIConfigsApi from 'launchdarkly-api-typescript';
import { AIConfigsApi } from 'launchdarkly-api-typescript'; - FlagsApi
import { FlagsApi } from 'launchdarkly-api-typescript/dist/api';import { FlagsApi } from 'launchdarkly-api-typescript';
Quickstart
import { Configuration, FlagsApi } from 'launchdarkly-api-typescript';
const LAUNCHDARKLY_API_KEY = process.env.LAUNCHDARKLY_API_KEY ?? '';
const PROJECT_KEY = 'my-project'; // Replace with your project key
if (!LAUNCHDARKLY_API_KEY) {
console.error('LAUNCHDARKLY_API_KEY environment variable is not set.');
process.exit(1);
}
async function listFlagsForProject() {
const configuration = new Configuration({
accessToken: LAUNCHDARKLY_API_KEY,
basePath: 'https://app.launchdarkly.com'
});
const flagsApi = new FlagsApi(configuration);
try {
console.log(`Fetching feature flags for project: ${PROJECT_KEY}...`);
const response = await flagsApi.getFeatureFlags({ projectKey: PROJECT_KEY });
console.log(`Found ${response.items.length} feature flags.`);
if (response.items.length > 0) {
console.log('First flag key:', response.items[0].key);
console.log('First flag name:', response.items[0].name);
}
} catch (error) {
console.error('Error fetching feature flags:', error.response?.data || error.message);
}
}
listFlagsForProject();