Skip to main content

Feature Flagging

In this guide, we'll go over how you can implement feature flags for your application using Warrant.

What are Feature Flags?

Feature flags allow you to modify application behavior at runtime based on some specific context, without changing any code. Feature flags have a variety of application use-cases including gating feature access, A/B testing experimental features, implementing fall-back logic and safely turning off old features. Their versatility makes them 'must-have' product infrastructure.

In the remainder of this guide, we'll go over how you can use Warrant as a user-centric feature flagging system in your application.

Prerequisites

This guide exclusively uses the Warrant API and thus assumes you have a Warrant account with API keys. If you don't, please first follow the Getting Started guide to set up your account.

Creating a 'Feature' Object Type

There's really only 1 object type we need in order to implement feature flags: feature. Each object of this type represents a specific feature within our application. For example, we might have a 'query_builder' feature and a 'reporting' feature.

The feature object type only has an access relation which allows us to grant users access to the specific feature. Putting this together, we get the following simple object type definition for feature:

{
"type": "feature",
"relations": {
"access": {}
}
}

Managing Feature Access

After our feature object type is defined, we have the ability to create warrants to manage who has access to which feature(s) in the application.

We can create these warrants adhoc in the Warrant dashboard or within our business logic using the Warrant APIs. For example, the following code can be used to grant user1 access to the 'query_builder' feature in our app:

client
.createWarrant({
objectType: "feature",
objectId: "query_builder",
relation: "access",
subject: {
objectType: "user",
objectId: "user1"
}
})
.then((newWarrant) => console.log(newWarrant))
.catch((error) => console.log(error));

Checking Feature Access

With our feature flags defined, we can start to enforce access to the features in our app.

For example, in the case of the 'query_builder' feature, we will want to add the following user check in our application code that gives access to the 'query_builder' feature:

client
.isAuthorized({
warrants: [{
objectType: "feature",
objectId: "query_builder",
relation: "access",
subject: {
objectType: "user",
objectId: userToCheck
}
}]
})
.then((isAuthorized) => {
if (isAuthorized) {
// Allow access to 'query_builder' for this user
}
})
.catch((error) => console.log(error));

Summary

In this guide, we showed how you can use Warrant to quickly implement feature flags in your application. These feature flags exist as user-based warrants that can be modified in real-time via the Warrant dashboard and APIs. They allow us to change user access to features or application behavior at runtime without having to modify any code.