7.7. Feature Flags

In this chapter, you'll learn what feature flags are, what the available feature flags in Medusa are, and how to toggle them.

What are Feature Flags?#

Feature flags allow you to ship new features that are still under development and testing, but not ready for production or wide use yet.

Medusa uses feature flags to ship new versions even when some features are not fully ready. This approach allows our team to continuously deliver updates and improvements while stabilizing new features.


Available Feature Flags in Medusa#

For a list of features hidden behind a feature flag in Medusa, check out the feature-flags directory in the @medusajs/medusa package.


Toggle Feature Flags#

There are multiple ways to enable or disable feature flags in Medusa:

  • In the medusa-config.ts file;
  • Or using environment variables.

1. Using the medusa-config.ts file#

The Medusa configurations in medusa-config.ts accept a featureFlags configuration to toggle feature flags. Its value is an object whose key is the feature flag key (defined in the feature flag's file), and the value is a boolean indicating whether the feature is enabled.

For example, to enable the Index Module's feature flag in medusa-config.ts:

medusa-config.ts
1module.exports = defineConfig({2  // ...3  featureFlags: {4    "index_engine": true,5  },6})

Make sure to run migrations after enabling a feature flag, in case it requires database changes.

Before disabling a feature flag, make sure to roll back the migrations that depend on it.

2. Using Environment Variables#

The feature flags can also be enabled or disabled using environment variables. This is useful to control the feature flag based on the environment.

To toggle a feature flag using an environment variable, set an environment variable with its environment variable name (defined in the feature flag's file) and set its value to true or false.

For example, to enable the Index Module's feature flag using an environment variable:

Code
MEDUSA_FF_INDEX_ENGINE=true

Make sure to run migrations after enabling a feature flag, in case it requires database changes.

Before disabling a feature flag, make sure to roll back the migrations that depend on it.


Check Feature Flag Status#

During development, you can check whether a feature flag is enabled. This is useful to add customizations that only apply when a specific feature is enabled.

To build backend customizations around feature flags, you can either:

For client customizations, you can use the Feature Flags API Route.

Conditionally Run Code Blocks#

The FeatureFlag utility allows you to check whether a specific feature flag is enabled in your backend customizations, including scheduled jobs, subscribers, and workflow steps.

For example, to enable access to a route only if a feature flag is enabled, you can use the FeatureFlag utility in a middleware:

src/api/middlewares.ts
1import { defineMiddlewares } from "@medusajs/framework/http"2import { FeatureFlag } from "@medusajs/framework/utils"3
4export default defineMiddlewares({5  routes: [6    {7      matcher: "/custom",8      method: ["GET"],9      middlewares: [10        async (req, res, next) => {11          if (!FeatureFlag.isFeatureEnabled("index_engine")) {12            return res.sendStatus(404)13          }14          next()15        },16      ],17    },18  ],19})

The FeatureFlag.isFeatureEnabled method accepts the feature flag key as a parameter and returns a boolean indicating whether the feature is enabled or not.

In the above example, you return a 404 response if a GET request is sent to the /custom route and the index_engine feature flag is disabled.

Conditionally Load Files#

You can also combine the FeatureFlag utility with the defineFileConfig utility that allows you to fully enable or disable loading a file based on a condition.

For example, instead of adding a middleware, you can use the defineFileConfig utility to conditionally load an API route file only if a feature flag is enabled:

src/api/routes/custom.ts
1import { MedusaRequest, MedusaResponse } from "@medusajs/framework"2import { defineFileConfig, FeatureFlag } from "@medusajs/framework/utils"3
4export async function GET(5  req: MedusaRequest,6  res: MedusaResponse7): Promise<void> {8  res.json({9    message: "Hello World",10  })11}12
13defineFileConfig({14  isDisabled: () => !FeatureFlag.isFeatureEnabled("index_engine"),15})

The defineFileConfig function accepts an object with an isDisabled property. Its value is a function that returns a boolean indicating whether the file should be disabled.

In the above example, the GET API route at /custom will only be available if the index_engine feature flag is enabled.

Middleware vs defineFileConfig: While both approaches can be used to disable API routes, a middleware is useful to disable specific HTTP methods at a route. For example, if a route file has GET and POST route handlers, the defineFileConfig approach will disable both GET and POST methods if the feature flag is disabled. If you need to disable only the POST route, you should use a middleware instead.

Feature Flags API Route#

For client customizations, you can use the List Feature Flags API Route to check the status of feature flags.

For example, you can show an admin widget only if a feature flag is enabled:

src/admin/widgets/product-details.tsx
1import { defineWidgetConfig } from "@medusajs/admin-sdk"2import { sdk } from "../lib/sdk"3import { useQuery } from "@tanstack/react-query"4
5const ProductWidget = () => {6  const { data: featureFlags } = useQuery({7    queryKey: ["featureFlags"],8    queryFn: () => sdk.client.fetch<{ 9      feature_flags: Record<string, boolean>10    }>(`/admin/feature-flags`),11  })12
13  if (!featureFlags?.feature_flags.index_engine) {14    return null15  }16
17  return (18    <div>19      <h2>Product Widget</h2>20      <p>Index engine feature is enabled</p>21    </div>22  )23}24
25export const config = defineWidgetConfig({26  zone: "product.details.after",27})28
29export default ProductWidget

In the above example, the Product Widget will only be displayed if the index_engine feature flag is enabled. It retrieves the feature flag statuses from the /admin/feature-flags API route.

Was this chapter helpful?
Ask Anything
FAQ
What is Medusa?
How can I create a module?
How can I create a data model?
How do I create a workflow?
How can I extend a data model in the Product Module?
Recipes
How do I build a marketplace with Medusa?
How do I build digital products with Medusa?
How do I build subscription-based purchases with Medusa?
What other recipes are available in the Medusa documentation?
Chat is cleared on refresh
Line break