How to toggle on a feature flag based on a custom context in LaunchDarkly?

310 Views Asked by At

I am trying to figure out how to toggle a feature flag based on a custom context value. However, the LaunchDarkly docs aren't helpful.

I am using https://github.com/launchdarkly/node-server-sdk

An example would be:

I have feature flag that has an option to use cityContext or countryContext and I want to return true if cityContext was used as a rule within the feature flag.

I want to understand if the above is possible. As I want to perform custom logic depending on which context is used.

I have tried the following:

const cityContext = {
   key: 'uuid',
   custom: {
        city: 'London'
   }
}

const cityValue = await client.variation('any-value', cityContext, false);


const countryContext = {
   key: 'uuid',
   custom: {
        country: 'United Kingdom'
   }
}

const countryValue = await client.variation('any-value', countryContext, false);
1

There are 1 best solutions below

0
On

You need to define a feature flag in LaunchDarkly and add a rule that assigns a user some variation based on a context:

  • Control or Treatment
  • or simply a boolean value - based on your needs enter image description here

You can add different rules for instance:

  1. Rule to serve Treatment 1 to city=London
  2. Rule to serve Treatment 2 to country=UK
  3. Default rule to serve Control