Skill 77 · Instrument Feature Flags
Subchapter 77.16
references/nodejs.mdMarkdown5 KBView on GitHub
AI agents: this is one page from PostHog’s docs. Full index of Markdown docs for LLMs: https://posthog.com/llms.txt (opens in a new tab)
1
Required
Install the PostHog Node.js library using your package manager:
npm install posthog-nodeyarn add posthog-nodepnpm add posthog-nodebun add posthog-node2
Required
Initialize the PostHog client with your project token:
Node.js
import { PostHog } from 'posthog-node'
const client = new PostHog(
'<ph_project_token>',
{
host: 'https://us.i.posthog.com'
}
)3
Recommended
Once installed, you can manually send events to test your integration:
Node.js
client.capture({
distinctId: 'distinct_id_of_the_user',
event: 'event_name',
properties: {
property1: 'value',
property2: 'value',
},
})4
Required
Check if a feature flag is enabled:
const isFeatureFlagEnabled = await client.isFeatureEnabled('flag-key', 'distinct_id_of_your_user')
if (isFeatureFlagEnabled) {
// Your code if the flag is enabled
// Optional: fetch the payload
const matchedFlagPayload =
5
Optional
For multivariate flags, check which variant the user has been assigned:
const enabledVariant = await client.getFeatureFlag('flag-key', 'distinct_id_of_your_user')
if (enabledVariant === 'variant-key') { // replace 'variant-key' with the key of your variant
// Do something differently for this user
6
Required
If you want to use your feature flag to breakdown or filter events in your insights, you’ll need to include feature flag information in those events. This ensures that the feature flag value is attributed correctly to the event.
Note: This step is only required for events captured using our server-side SDKs or API.
Set sendFeatureFlags to true in your capture call:
Node.js
client.capture({
7
Optional
Sometimes, you may want to evaluate feature flags using properties that haven’t been ingested yet, or were set incorrectly earlier. You can provide properties to evaluate the flag with:
await client.getFeatureFlag(
'flag-key',
'distinct_id_of_the_user',
{
personProperties: {
'property_name': 'value'
8
Optional
Experiments run on top of our feature flags. Once you’ve implemented the flag in your code, you run an experiment by creating a new experiment in the PostHog dashboard.
9
Recommended
Now that you’re evaluating flags, continue with the resources below to learn what else Feature Flags enables within the PostHog platform.
| Resource | Description |
|---|---|
| Creating a feature flag (opens in a new tab) | How to create a feature flag in PostHog |
| Adding feature flag code (opens in a new tab) | How to check flags in your code for all platforms |
| Framework-specific guides |
Ask PostHog AI
HelpfulCould be better
Include the $feature/feature_flag_name property in your event properties:
Node.js
client.capture({
distinctId: 'distinct_id_of_your_user',
event: 'event_name',
properties: {
'$feature/feature-flag-key': 'variant-key' // replace feature-flag-key with your flag key. Replace 'variant-key' with the key of your variant
},
})| Setup guides for React Native, Next.js, Flutter, and other frameworks |
| How to do a phased rollout (opens in a new tab) | Gradually roll out features to minimize risk |
| More tutorials (opens in a new tab) | Other real-world examples and use cases |