Feature Flag payloads

Last updated:

Payloads

A feature flag payload is an additional piece of information sent along to your app when a flag is matched for a user. The returned value can be any valid JSON type (object, array, number, string, boolean, or null).

For boolean feature flags, you can specify a payload to be returned when the flag is true. For multivariate flags, you can define a payload for each variant of the flag.

Use cases

Payloads give you the ability to configure functionality related to your flag inside PostHog. This means more flexibility and fewer updates to your code. For example, you can:

  • set the color, size, and text of a button
  • modify the title and subtitle of a page
  • trigger different functions with different arguments
  • access multiple different infrastructure options

Without payloads, all of these options would need to be coded into your app, rather than accessing the payload values.

Using payloads in your code

To illustrate how to use feature flag payloads, we can configure a button in JavaScript by first checking for the flag, then setting the config with the payload value.

JavaScript
// payload for 'checkout-button-color' defined as:
// {
// color: 'black',
// size: 50
// }
if (posthog.getFeatureFlag('checkout-button-color') === 'black') {
const buttonConfig = posthog.getFeatureFlagPayload('keyword')
// do something
}

The feature flag payload retrieval function (getFeatureFlagPayload()) is a separate function that can be called to obtain the payload associated with the flag.

Questions?

Was this page useful?

Next article

Early Access Feature Management

Early access feature management enables your users the ability to opt-in to betas and other early features. This is esepcially useful if you don't want to run a beta application process, or if you ship frequent updates and want users to control their own experience. Users can also decide to opt-out of betas they do not want to be part of. PostHog tracks opt-ins and opt-outs, so you can see how popular a beta is with your users. How it works Early access features can be configured in the early…

Read next article