1 import { TypeList } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Create Custom Feature Flag`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how to create a custom feature flag in Medusa.
10
11 ## Why Create a Custom Feature Flag?
12
13 As explained in the [Feature Flags](../page.mdx) chapter, feature flags allow the Medusa team to ship new features that are still under development and testing, but not ready for production or wide use yet.
14
15 You can also create custom feature flags for your Medusa project or plugin to control the availability of experimental or in-development features. Feature flags allow you to test features in staging, and only enable them in production when they are ready.
16
17 ---
18
19 ## How to Create a Custom Feature Flag
20
21 ### 1. Define the Feature Flag
22
23 To create a custom feature flag, you need to define it in a new file under the `src/feature-flags` directory of your Medusa project or plugin. The file must export a configuration object.
24
25 For example, to define a feature flag that enables a blog feature, create the file `src/feature-flags/blog.ts` with the following content:
26
27 export const featureFlagHighlights = [
28 ["4", "key", "The unique identifier for the feature flag."],
29 ["5", "default_val", "Whether the feature flag is enabled by default."],
30 ["6", "env_key", "The environment variable key for the feature flag."],
31 ["7", "description", "A brief description of what the feature flag does."]
32 ]
33
34 ```ts title="src/feature-flags/blog.ts" highlights={featureFlagHighlights}
35 import { FlagSettings } from "@medusajs/framework/feature-flags"
36
37 const BlogFeatureFlag: FlagSettings = {
38 key: "blog_feature",
39 default_val: false,
40 env_key: "FF_BLOG_FEATURE",
41 description: "Enable blog features",
42 }
43
44 export default BlogFeatureFlag
45 ```
46
47 The feature flag configuration is an object having the following properties:
48
49 <TypeList
50 types={[
51 {
52 type: "`string`",
53 name: "key",
54 description: "The unique identifier for the feature flag. This key is used to enable or disable the feature flag and check its status.",
55 optional: false
56 },
57 {
58 type: "`boolean`",
59 name: "default_val",
60 description: "Whether the feature flag is enabled by default.",
61 optional: false
62 },
63 {
64 type: "`string`",
65 name: "env_key",
66 description: "The environment variable key for the feature flag. This key is used to enable or disable the feature flag using environment variables.",
67 optional: false
68 },
69 {
70 type: "`string`",
71 name: "description",
72 description: "A brief description of what the feature flag does.",
73 optional: false
74 }
75 ]}
76 />
77
78 ### 2. Hide Features Behind the Flag
79
80 Next, you can build the features you want to hide behind the feature flag.
81
82 To build backend customizations around feature flags, you can either:
83
84 - [Conditionally run code blocks](../page.mdx#conditionally-run-code-blocks) based on the feature flag status;
85 - [Conditionally load files](../page.mdx#conditionally-load-files) if the feature flag is not enabled.
86
87 For client customizations, such as admin widgets, you can use the [Feature Flags API route](../page.mdx#feature-flags-api-route).
88
89 ### 3. Toggle Feature Flag
90
91 To enable or disable your custom feature flag, you can either add it to your `medusa-config.ts` file:
92
93 ```ts title="medusa-config.ts"
94 import BlogFeatureFlag from "./src/feature-flags/blog"
95
96 module.exports = defineConfig({
97 // ...
98 featureFlags: {
99 [BlogFeatureFlag.key]: true,
100 },
101 })
102 ```
103
104 Or set the environment variable for the feature flag:
105
106 ```shell
107 FF_BLOG_FEATURE=true
108 ```
109
110 Afterwards, make sure to [run migrations](../../../fundamentals/data-models/write-migration/page.mdx#run-the-migration) if your feature flag requires database changes.
111
112 If you're disabling a feature flag, make sure to [roll back any migrations](../../../fundamentals/data-models/write-migration/page.mdx#rollback-the-migration) that depend on it first.
113
114 ---
115
116 ## Write Tests for Features Behind Flags
117
118 If you're writing integration tests for features hidden behind feature flags, you can enable the feature flag's environment variable in your integration test using the `env` option.
119
120 For example:
121
122 ```ts title="integration-tests/http/test.spec.ts"
123 import { medusaIntegrationTestRunner } from "@medusajs/test-utils"
124
125 medusaIntegrationTestRunner({
126 env: {
127 FF_BLOG_FEATURE: true,
128 },
129 testSuite: ({ api, getContainer }) => {
130 // TODO write tests...
131 },
132 })
133 ```
134
135 Then, the Medusa application will load your customizations hidden behind the feature flag as part of the integration tests.