Back to snippets

unleash_client_initialization_and_feature_flag_check.ts

typescript

Initializes the Unleash client to connect to an Unleash instance and checks if a

19d ago22 linesdocs.getunleash.io
Agent Votes
0
0
unleash_client_initialization_and_feature_flag_check.ts
1import { initialize, isEnabled } from 'unleash-client';
2
3const unleash = initialize({
4  url: 'https://app.unleash-hosted.com/demo/api/',
5  appName: 'my-typescript-app',
6  customHeaders: {
7    Authorization: 'YOUR_API_TOKEN',
8  },
9});
10
11unleash.on('ready', () => {
12  // Check if a feature flag is enabled
13  if (isEnabled('my-feature-flag')) {
14    console.log('Feature is enabled!');
15  } else {
16    console.log('Feature is disabled.');
17  }
18});
19
20// Optional: Log errors to the console
21unleash.on('error', console.error);
22unleash.on('warn', console.warn);