Quick Start Guide
Get started with DarkFeature in minutes. This guide will walk you through setting up your first feature flag.
Prerequisites
- A DarkFeature account (sign up at darkfeature.com )
- Basic knowledge of your programming language
- A web browser for testing
Step 1: Create Your First Feature Flag
- Sign in to your DarkFeature dashboard
- Navigate to the Features section
- Click “Create Feature”
- Enter a name for your feature (e.g., “new-navigation”)
- Set the initial state to “Off”
- Click “Create Feature”
Step 2: Configure Your Environment
DarkFeature supports multiple environments for different stages of development:
- Development - For local development and testing
- Staging - For pre-production validation
- Production - For live applications
Each environment has its own configuration and API keys.
Step 3: Get Your API Key
Each environment has its own API key for security:
- Go to your project settings
- Navigate to the API Keys section
- Copy the Client-side API Key for your Development environment
Your API key will look like this:
1234567890abcdef1234567890abcdef
Note: API keys are 32-character nanoids without any prefixes or suffixes.
Step 4: Install the Code Example
Choose your preferred language and install the DarkFeature code example:
JavaScript/TypeScript (Client-side)
npm install @darkfeature/sdk-javascript
# or
yarn add @darkfeature/sdk-javascript
# or
pnpm add @darkfeature/sdk-javascript
React
npm install @darkfeature/sdk-react
# or
yarn add @darkfeature/sdk-react
# or
pnpm add @darkfeature/sdk-react
Node.js (Server-side)
npm install @darkfeature/sdk-javascript
# or
yarn add @darkfeature/sdk-javascript
# or
pnpm add @darkfeature/sdk-javascript
Step 5: Initialize the Code Example
Add the code example to your application:
JavaScript/TypeScript (Client-side)
import { DarkFeatureClient } from '@darkfeature/sdk-javascript'
const client = new DarkFeatureClient({
apiKey: '1234567890abcdef1234567890abcdef',
context: {
userId: '123',
email: '[email protected]',
},
})
// Check if a feature is enabled
const isEnabled = await client.getFeature('new-navigation', { fallback: false })
console.log('New navigation enabled:', isEnabled)
React
import { DarkFeatureProvider, useFeature } from '@darkfeature/sdk-react'
function App() {
return (
<DarkFeatureProvider
config={{
apiKey: '1234567890abcdef1234567890abcdef',
context: { userId: '123' },
}}>
<MyComponent />
</DarkFeatureProvider>
)
}
function MyComponent() {
const { feature: isEnabled } = useFeature('new-navigation', {
fallback: false,
})
return <div>{isEnabled ? <NewNavigation /> : <OldNavigation />}</div>
}
Node.js (Server-side)
import { DarkFeatureClient } from '@darkfeature/sdk-javascript'
const client = new DarkFeatureClient({
apiKey: '1234567890abcdef1234567890abcdef',
context: {
userId: '123',
email: '[email protected]',
},
})
// Check if a feature is enabled
const isEnabled = await client.getFeature('new-navigation', { fallback: false })
console.log('New navigation enabled:', isEnabled)
Step 6: Test Your Feature Flag
Enable the Feature
- Go back to your DarkFeature dashboard
- Find your “new-navigation” feature
- Toggle the feature to “On”
- Save the changes
Verify the Change
If you’re using the JavaScript code example, you can test in your browser:
// Open browser console and run:
const isEnabled = await client.getFeature('new-navigation', { fallback: false })
console.log('Feature enabled:', isEnabled)
You should see the feature status change from false
to true
.
Step 7: Add User Targeting
Now let’s target specific users:
- Go to your feature settings
- Click “Add Rule”
- Select “User Attribute”
- Choose “email” as the attribute
- Set the condition to “contains” and value “admin”
- Save the rule
This will enable the feature only for users with “admin” in their email address.
Step 8: Test with Different Users
// Test with admin user
client.setContext({ email: '[email protected]' })
const adminEnabled = await client.getFeature('new-navigation', {
fallback: false,
})
console.log('Admin enabled:', adminEnabled) // true
// Test with regular user
client.setContext({ email: '[email protected]' })
const userEnabled = await client.getFeature('new-navigation', {
fallback: false,
})
console.log('User enabled:', userEnabled) // false
Step 9: Environment Variables (Recommended)
For production applications, store your API key in environment variables:
Client-side (JavaScript/React)
# .env
REACT_APP_DARKFEATURE_API_KEY=1234567890abcdef1234567890abcdef
VUE_APP_DARKFEATURE_API_KEY=1234567890abcdef1234567890abcdef
const client = new DarkFeatureClient({
apiKey:
process.env.REACT_APP_DARKFEATURE_API_KEY ||
process.env.VUE_APP_DARKFEATURE_API_KEY,
context: { userId: '123' },
})
Server-side (Node.js)
# .env
DARKFEATURE_API_KEY=1234567890abcdef1234567890abcdef
const client = new DarkFeatureClient({
apiKey: process.env.DARKFEATURE_API_KEY,
context: { userId: '123' },
})
Step 10: Advanced Usage
Multiple Features
// Get multiple features at once
const features = await client.getFeatures({
features: {
'new-navigation': false,
'dark-mode': false,
'premium-features': false,
},
context: { userId: '123' },
})
console.log('Features:', features)
// { 'new-navigation': true, 'dark-mode': false, 'premium-features': true }
Context Override
// Override context for a specific request
const isEnabled = await client.getFeature('premium-features', {
fallback: false,
context: { plan: 'premium' },
})
Error Handling
try {
const isEnabled = await client.getFeature('new-navigation', {
fallback: false,
})
// Use feature
} catch (error) {
console.error('Feature flag error:', error)
// Fallback to default behavior
showDefaultNavigation()
}
Next Steps
Explore Code Examples
- JavaScript Code Example - Detailed JavaScript integration
- React Code Example - React-specific features and hooks
- Node.js Code Example - Server-side integration
Advanced Features
- Percentage Rollouts - Gradually roll out features
- Targeting Rules - Advanced user targeting
- Analytics - Track feature usage and performance
Troubleshooting
Feature Flag Not Working
- Verify your API key is correct (32 characters, no prefixes)
- Check that the feature flag name matches exactly
- Ensure the code example is properly initialized
- Verify the feature is enabled in the dashboard
Code Example Not Initializing
Problem: Code example initialization fails or times out
Solutions:
- Check your internet connection
- Verify the API key format (32 characters)
- Ensure you’re using the correct environment
- Check browser console for error messages
Feature Changes Not Reflecting
Problem: Feature flag changes don’t appear immediately
Solutions:
- Wait a few seconds for propagation
- Check if caching is enabled
- Verify the feature is saved in the dashboard
- Try refreshing the page or clearing cache
API Key Issues
Problem: “Invalid API key” or “Unauthorized” errors
Solutions:
- Ensure the API key is exactly 32 characters
- Remove any prefixes or suffixes
- Copy the key from the dashboard without extra spaces
- Verify you’re using the correct environment’s key
Need Help?
- Documentation: Browse our comprehensive guides
- Community: Join our community forum
- Support: Contact our support team
- Email: [email protected]