# Feature Flags

{{< glossary_definition "feature_flag" >}}


## Accessing flags

The Airship SDK will refresh feature flags when the app is brought to the foreground. If a feature flag is accessed before the foreground refresh completes, or after the foreground refresh has failed, feature flags will be refreshed during flag access. Feature flags will only be updated once per session and will persist for the duration of each session.

Once [defined in the dashboard](https://www.airship.com/docs/guides/experimentation/feature-flags/#create-feature-flags), a feature flag can be accessed by its name in the SDK after `TakeOff`. `Flag` is asynchronous, so run it as a coroutine.

```csharp
StartCoroutine(Airship.Shared.featureFlagManager.Flag("YOUR_FLAG_NAME", (FeatureFlag flag) => {
    if (flag.isEligible) {
        // Do something with the flag
    } else {
        // Disable feature or use default behavior
    }
}));
```


Alongside `isEligible`, a `FeatureFlag` carries `exists`, which reports whether a definition for the flag was found, and `variables`, the flag's variables. `variables` is a JSON string rather than a structured object, so parse it with the JSON library of your choice. It is `null` when the flag defines no variables.

## Tracking interaction

To generate the [feature flag interaction event](https://www.airship.com/docs/developer/rest-api/connect/schemas/events/#feature-flag-interaction), you must manually call `TrackInteraction` with the feature flag. Analytics must be enabled. See [Data Collection: Privacy Manager](https://www.airship.com/docs/developer/sdk-integration/unity/data-collection/privacy-manager/).

```csharp
Airship.Shared.featureFlagManager.TrackInteraction(flag);
```


## Error handling

If a feature flag allows evaluation with stale data, the SDK evaluates the flag if a definition for the flag is found. Otherwise, feature flag evaluation depends on updated local state. If the SDK cannot evaluate a flag because data cannot be fetched, the SDK returns an error to the optional `onError` callback. The app can either treat the error as the flag being ineligible or retry at a later time.

```csharp
StartCoroutine(Airship.Shared.featureFlagManager.Flag(
    "YOUR_FLAG_NAME",
    (FeatureFlag flag) => {
        // Do something with the flag
    },
    (System.Exception error) => {
        // Do something with the error
    }
));
```

