Official JavaScript/TypeScript SDK for Flagdeck, a modern feature flag and feature management system.
Install the Flagdeck SDK using npm or yarn:
# Using npm
npm install @flagdeck/js
# Using yarn
yarn add @flagdeck/js
import { Flagdeck } from '@flagdeck/js';
// Initialize the client
const flagdeck = new Flagdeck({
apiKey: 'your-api-key'
});
// Check if a feature flag is enabled
async function checkFeature() {
const isEnabled = await flagdeck.isEnabled('new-feature', {
userId: 'user-123',
attributes: {
country: 'US',
plan: 'premium'
}
});
if (isEnabled) {
// Feature is enabled for this user
enableNewFeature();
} else {
// Feature is disabled
useDefaultFeature();
}
}
The SDK automatically detects your environment (browser or Node.js) and uses the appropriate implementation. However, you can also explicitly import the version you need:
For browser applications (including React, Vue, Angular, etc.):
// Import browser-specific implementation
import { Flagdeck } from '@flagdeck/js/browser';
const flagdeck = new Flagdeck({
apiKey: 'your-api-key',
// Browser-specific options
enableOfflineMode: true // Uses localStorage for offline storage
});
For server applications (Node.js):
// Import Node.js-specific implementation
import { Flagdeck } from '@flagdeck/js/node';
const flagdeck = new Flagdeck({
apiKey: 'your-api-key',
// Node.js-specific options
enableOfflineMode: true // Uses filesystem for offline storage
});
The Flagdeck SDK provides several methods to work with feature flags:
The most common use case is checking if a feature flag is enabled:
// Simple check with no context
const isEnabled = await flagdeck.isEnabled('new-feature');
// Check with user context
const isEnabledForUser = await flagdeck.isEnabled('new-feature', {
userId: 'user-123'
});
// Check with detailed context
const isEnabledWithContext = await flagdeck.isEnabled('new-feature', {
userId: 'user-456',
attributes: {
country: 'CA',
userType: 'admin'
}
});
For flags with non-boolean values, use the getValue
method:
// Get a string value
const variant = await flagdeck.getValue('experiment-group', context, 'control');
// Get a numeric value
const limit = await flagdeck.getValue('api-rate-limit', context, 100);
// Get an object
const config = await flagdeck.getValue('service-config', context, {
timeout: 5000,
retries: 3
});
For better performance, evaluate multiple flags in a single request:
const values = await flagdeck.getValues([
'feature-a',
'feature-b',
'rate-limit'
], context);
console.log(values['feature-a']); // boolean
console.log(values['rate-limit']); // number
The context object lets you provide user and environment information for targeting rules:
const context = {
// Unique user identifier (strongly recommended)
userId: 'user-123',
// Optional session identifier
sessionId: 'session-abc',
// Custom attributes for targeting rules
attributes: {
email: '[email protected]',
country: 'US',
plan: 'premium',
deviceType: 'mobile'
}
};
The SDK accepts various configuration options when initializing:
const flagdeck = new Flagdeck({
// Required: Your API key from the Flagdeck dashboard
apiKey: 'your-api-key',
// Optional: Enable caching (default: true)
enableCache: true,
// Optional: Cache timeout in milliseconds (default: 30000)
cacheTimeout: 60000,
// Optional: Enable debug logging (default: false)
debug: true,
// Optional: Enable offline mode (default: false)
enableOfflineMode: false,
// Optional: Enable analytics (default: true)
enableAnalytics: true,
// Optional: Request timeout in milliseconds (default: 5000)
timeout: 5000,
// Optional: Retry configuration
retryConfig: {
attempts: 3,
delay: 1000,
maxDelay: 5000
},
// Optional: Global error handler
onError: (error, flagKey) => {
console.error(`Error evaluating flag ${flagKey}:`, error);
},
// Optional: Default value when evaluation fails
defaultFlagValue: false
});
When using the SDK in a browser environment:
localStorage
for storing flag values when offline mode is enabled// Browser-specific offline storage example
const flagdeck = new Flagdeck({
apiKey: 'your-api-key',
enableOfflineMode: true
});
// Save flag for offline use (uses localStorage in browsers)
await flagdeck.saveForOffline('feature-a', true);
When using the SDK in a Node.js environment:
// Node.js-specific offline storage example
const flagdeck = new Flagdeck({
apiKey: 'your-api-key',
enableOfflineMode: true
});
// Save flag for offline use (uses filesystem in Node.js)
await flagdeck.saveForOffline('feature-a', true);
Start using Flagdeck today
Simple feature flag management for modern development teams.
Product
© 2025 Flagdeck. All rights reserved.