Affected: REST API
Overview
In LaunchDarkly, a resource generally refers to an entity within the platform that you can configure or manage. This includes feature flags, environments, projects, users, and more.
We currently don't have the option to bulk create multiple resources, such as flags, through the UI. However, you can accomplish something similar by using the Create Feature Flag REST API endpoint and creating a script to do this programmatically.
Solution
To automate the creation of multiple flags, use the Create feature flag API endpoint:
Write a script that loops through your desired flag configurations.
For each flag, make a
POSTrequest to/api/v2/flags/{projectKey}.Include your API access token in the
Authorizationheader.
To ensure smooth operation, please respect our rate-limiting policy when making these requests.
Example using Node.js:
const fetch = require('node-fetch'); // Run "npm install node-fetch"
const LD_API_TOKEN = 'api-123abc'; // Replace with your token
const PROJECT_KEY = 'your-project-key'; // Replace with your project key
const flags = [
{ key: 'feature-login-v2', name: 'Feature: Login v2' },
{ key: 'enable-dark-mode', name: 'Enable: Dark Mode' },
{ key: 'beta-access-banner', name: 'Beta Access Banner' },
];
async function createFlag(flag) {
const url = `https://app.launchdarkly.com/api/v2/flags/${PROJECT_KEY}`;
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': LD_API_TOKEN,
'Content-Type': 'application/json',
},
body: JSON.stringify({
key: flag.key,
name: flag.name,
variations: [{ value: true }, { value: false }]
}),
});
if (response.ok) {
console.log(`Created flag: ${flag.key}`);
} else {
const errorData = await response.json();
console.error(`Failed to create flag: ${flag.key}`, errorData);
}
}
async function main() {
for (const flag of flags) {
await createFlag(flag);
}
}
main();Resources