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.
// 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.