HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityLog In

Create feature flags

This topic describes how to create and implement feature flags.

Feature flags, also known as feature toggles or permission toggles, are a software development technique that lets you turn certain functionality on and off without deploying new code. Flags allow for better control over the full lifecycle of features. You can toggle a feature off to release code quickly without exposing it to users. You can also roll it out to a fraction of your user base to minimize the impact of the launch, allowing you to validate functionality and measure performance prior to rolling out broadly.

In Optimizely, you can manage all your feature flags in one interface. In the following sections, we cover how to create, edit, and archive a feature flag.

Create a flag

To create a flag, navigate to Flags>Create New:

Click to enlarge imageClick to enlarge image

Click to enlarge image

  1. In the Name field, give the flag a unique name. (Valid keys contain alphanumeric characters, hyphens, and underscores, are limited to 64 characters, and can't contain spaces.)

Optimizely automatically assigns a key to the flag based on this name. You will use the flag key to implement the flag in your application code.

  1. Click Create Flag to save your new flag.

    • Optimizely automatically creates a read-only flag_off variation for this flag, which you use as a control in experiments.

Implement the feature flag

Prerequisite: Before you can implement the flag, you must decide how to pass users to Optimizely, so that Optimizely can give each user a consistent experience of either an enabled or disabled flag. For more information see Handle user ids.

After you create the flag in the Optimizely dashboard, you need to integrate it with your application code. Use the Decide method to enable or disable the flag for a user:

// Decide if user sees a feature flag variation
let user = optimizely.createUserContext(userId: "user123", attributes: ["logged_in":true])
let decision = user.decide(key: "flag_1")
let enabled = decision.enabled
// Decide if user sees a feature flag variation
user := optimizely.CreateUserContext("user123", map[string]interface{}{"logged_in": true})
decision := user.Decide("flag_1", nil)
enabled := decision.Enabled
# Decide if user sees a feature flag variation
user = optimizely.create_user_context("user123", {'logged_in': True})
decision = user.decide("flag_1")
enabled = decision.enabled
// Decide if user sees a feature flag variation
$user = $optimizely_client->createUserContext('user123', ['logged_in' => true]);
$decision = $user->decide('flag_1');
$enabled = $decision->getEnabled();
# Decide if user sees a feature flag variation
user = optimizely_client.create_user_context('user123', {'logged_in' => true})
decision = user.decide('flag_1')
enabled = decision.enabled
// Decide if user sees a feature flag variation
var user = optimizely.CreateUserContext("user123", new UserAttributes { { "logged_in", true } });
var decision = user.Decide(key: "flag_1");
var enabled = decision.Enabled;
// Decide if user sees a feature flag variation
OptimizelyUserContext user = optimizely.createUserContext("user123", new HashMap<String, Object>() { { put("logged_in", true); } });
OptimizelyDecision decision = user.decide("flag_1");
Boolean enabled = decision.getEnabled();
// Decide if user sees a feature flag variation
var user = optimizelyClient.createUserContext('user123', { logged_in: true });
var decision = user.decide('flag_1');
var enabled = decision.enabled;
// Decide if user sees a feature flag variation
var decision = useDecision('flag_1', null, { overrideUserAttributes: { logged_in: true }});
var enabled = decision.enabled;

For more detailed examples, see:

  1. Adapt the integration code in your application. Show or hide the flag's functionality for a given user ID, based on the Boolean value your application receives Optimizely.

The goal of the Decide method is to separate the process of developing and releasing code from the decision to turn a flag on. The value this method returns is determined by your flag rules. For example, if the current user is assigned to a control or "off" variation in an experiment, the method returns false.

Next steps

To deliver feature flag configurations, see Create flag variations.


Did this page help you?