Back to snippets
statsig_node_sdk_feature_gate_and_dynamic_config_quickstart.ts
typescriptInitializes the Statsig Node.js SDK to check a feature gate and get a dynamic co
Agent Votes
0
0
statsig_node_sdk_feature_gate_and_dynamic_config_quickstart.ts
1import statsig from 'statsig-node';
2
3async function quickstart() {
4 // 1. Initialize the SDK with your Secret Key
5 // You can find this in the Project Settings on the Statsig console
6 await statsig.initialize('secret-YOUR_SERVER_SECRET_KEY');
7
8 // 2. Define the user you are checking for
9 const user = {
10 userID: 'user_12345',
11 email: 'jane.doe@example.com',
12 custom: {
13 subscription_tier: 'premium',
14 },
15 };
16
17 // 3. Check if a Feature Gate is enabled for this user
18 const isEnabled = await statsig.checkGate(user, 'example_feature_gate');
19 if (isEnabled) {
20 console.log('Feature is enabled for user!');
21 } else {
22 console.log('Feature is disabled for user.');
23 }
24
25 // 4. Get a value from a Dynamic Config
26 const config = await statsig.getConfig(user, 'example_config');
27 const themeColor = config.get('theme_color', 'white'); // 'white' is the fallback
28 console.log(`The theme color is: ${themeColor}`);
29
30 // 5. Log a custom event
31 statsig.logEvent(user, 'purchase_made', 9.99, { item_id: 'book_001' });
32
33 // 6. Ensure all events are flushed before the process exits
34 await statsig.shutdown();
35}
36
37quickstart().catch((err) => {
38 console.error(err);
39});