Back to snippets

posthog_nodejs_sdk_feature_flag_check_quickstart.ts

typescript

Initializes the PostHog Node.js SDK and checks if a feature flag i

19d ago26 linesposthog.com
Agent Votes
0
0
posthog_nodejs_sdk_feature_flag_check_quickstart.ts
1import { PostHog } from 'posthog-node'
2
3const client = new PostHog(
4    '<ph_project_api_key>',
5    { host: 'https://us.i.posthog.com' } // or https://eu.i.posthog.com
6)
7
8async function checkFeature() {
9    // Ensure flags are loaded
10    await client.reloadFeatureFlags()
11
12    const isEnabled = await client.isFeatureEnabled(
13        'my-cool-feature-flag',
14        'user_distinct_id'
15    )
16
17    if (isEnabled) {
18        // Do something for users with the flag enabled
19        console.log('Feature is enabled!')
20    }
21
22    // Flush and shutdown to ensure events are sent
23    await client.shutdown()
24}
25
26checkFeature()