|
1
|
import { Table } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `${pageNumber} Environment Variables in Admin Customizations`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this chapter, you'll learn how to use environment variables in your admin customizations. |
|
10
|
|
|
11
|
<Note> |
|
12
|
|
|
13
|
To learn how environment variables are generally loaded in Medusa based on your application's environment, check out [this chapter](../../environment-variables/page.mdx). |
|
14
|
|
|
15
|
</Note> |
|
16
|
|
|
17
|
## How to Set Environment Variables |
|
18
|
|
|
19
|
<Note> |
|
20
|
|
|
21
|
This only applies to customizations in a Medusa project. For plugins, refer to the [Environment Variables in Plugins](#environment-variables-in-plugins) section. |
|
22
|
|
|
23
|
</Note> |
|
24
|
|
|
25
|
The Medusa Admin is built on top of [Vite](https://vite.dev/). To set an environment variable that you want to use in a widget or UI route, prefix the environment variable with `VITE_`. |
|
26
|
|
|
27
|
For example: |
|
28
|
|
|
29
|
```plain |
|
30
|
VITE_MY_API_KEY=sk_123 |
|
31
|
``` |
|
32
|
|
|
33
|
--- |
|
34
|
|
|
35
|
## How to Use Environment Variables |
|
36
|
|
|
37
|
To access or use an environment variable starting with `VITE_`, use the `import.meta.env` object. |
|
38
|
|
|
39
|
For example: |
|
40
|
|
|
41
|
```tsx highlights={[["8"]]} |
|
42
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
43
|
import { Container, Heading } from "@medusajs/ui" |
|
44
|
|
|
45
|
const ProductWidget = () => { |
|
46
|
return ( |
|
47
|
<Container className="divide-y p-0"> |
|
48
|
<div className="flex items-center justify-between px-6 py-4"> |
|
49
|
<Heading level="h2">API Key: {import.meta.env.VITE_MY_API_KEY}</Heading> |
|
50
|
</div> |
|
51
|
</Container> |
|
52
|
) |
|
53
|
} |
|
54
|
|
|
55
|
export const config = defineWidgetConfig({ |
|
56
|
zone: "product.details", |
|
57
|
}) |
|
58
|
|
|
59
|
export default ProductWidget |
|
60
|
``` |
|
61
|
|
|
62
|
In this example, you display the API key in a widget using `import.meta.env.VITE_MY_API_KEY`. |
|
63
|
|
|
64
|
### Type Error on import.meta.env |
|
65
|
|
|
66
|
If you receive a type error on `import.meta.env`, create the file `src/admin/vite-env.d.ts` with the following content: |
|
67
|
|
|
68
|
```ts title="src/admin/vite-env.d.ts" |
|
69
|
/// <reference types="vite/client" /> |
|
70
|
|
|
71
|
declare const __BASE__: string |
|
72
|
declare const __BACKEND_URL__: string |
|
73
|
declare const __STOREFRONT_URL__: string |
|
74
|
``` |
|
75
|
|
|
76
|
This file tells TypeScript to recognize the `import.meta.env` object and enhances the types of your custom environment variables. |
|
77
|
|
|
78
|
Note that the `__BASE__`, `__BACKEND_URL__`, and `__STOREFRONT_URL__` variables are global variables available in your admin customizations. Learn more in the [Tips for Admin Customizations](../tips/page.mdx#global-variables-in-admin-customizations) chapter. |
|
79
|
|
|
80
|
--- |
|
81
|
|
|
82
|
## Check Node Environment in Admin Customizations |
|
83
|
|
|
84
|
To check the current environment, Vite exposes two variables: |
|
85
|
|
|
86
|
- `import.meta.env.DEV`: Returns `true` if the current environment is development. |
|
87
|
- `import.meta.env.PROD`: Returns `true` if the current environment is production. |
|
88
|
|
|
89
|
Learn more about other Vite environment variables in the [Vite documentation](https://vite.dev/guide/env-and-mode). |
|
90
|
|
|
91
|
--- |
|
92
|
|
|
93
|
## Predefined Environment Variables |
|
94
|
|
|
95
|
You can further customize the Medusa Admin behavior using the following predefined environment variables. You can set the environment variables in your `.env` file or in your deployment platform. |
|
96
|
|
|
97
|
<Note> |
|
98
|
|
|
99
|
The following pre-defined environment variables are available starting [Medusa v2.12.0](https://github.com/medusajs/medusa/releases/tag/v2.12.0). |
|
100
|
|
|
101
|
</Note> |
|
102
|
|
|
103
|
<Table> |
|
104
|
<Table.Header> |
|
105
|
<Table.Row> |
|
106
|
<Table.HeaderCell> |
|
107
|
Environment Variable |
|
108
|
</Table.HeaderCell> |
|
109
|
<Table.HeaderCell> |
|
110
|
Description |
|
111
|
</Table.HeaderCell> |
|
112
|
<Table.HeaderCell> |
|
113
|
Default |
|
114
|
</Table.HeaderCell> |
|
115
|
</Table.Row> |
|
116
|
</Table.Header> |
|
117
|
<Table.Body> |
|
118
|
<Table.Row> |
|
119
|
<Table.Cell> |
|
120
|
|
|
121
|
`ADMIN_AUTH_TYPE` |
|
122
|
|
|
123
|
</Table.Cell> |
|
124
|
<Table.Cell> |
|
125
|
|
|
126
|
A string indicating the authentication method that the JS SDK instance uses in the Medusa Admin. Possible values are `session` and `jwt`. Learn more in the [JS SDK Authentication guide](!resources!/js-sdk/auth/overview). |
|
127
|
|
|
128
|
</Table.Cell> |
|
129
|
<Table.Cell> |
|
130
|
|
|
131
|
`session` |
|
132
|
|
|
133
|
</Table.Cell> |
|
134
|
</Table.Row> |
|
135
|
<Table.Row> |
|
136
|
<Table.Cell> |
|
137
|
|
|
138
|
`ADMIN_JWT_TOKEN_STORAGE_KEY` |
|
139
|
|
|
140
|
</Table.Cell> |
|
141
|
<Table.Cell> |
|
142
|
|
|
143
|
A string indicating the key used to store the authentication JWT token in the browser's local storage. Only applicable if `ADMIN_AUTH_TYPE` is set to `jwt`. |
|
144
|
|
|
145
|
</Table.Cell> |
|
146
|
<Table.Cell> |
|
147
|
|
|
148
|
`medusa_auth_token` |
|
149
|
|
|
150
|
</Table.Cell> |
|
151
|
</Table.Row> |
|
152
|
</Table.Body> |
|
153
|
</Table> |
|
154
|
|
|
155
|
--- |
|
156
|
|
|
157
|
## Environment Variables in Production |
|
158
|
|
|
159
|
When you build the Medusa application, including the Medusa Admin, with the `build` command, the environment variables are inlined into the build. This means that you can't change the environment variables without rebuilding the application. |
|
160
|
|
|
161
|
For example, the `VITE_MY_API_KEY` environment variable in the example above will be replaced with the actual value during the build process. |
|
162
|
|
|
163
|
--- |
|
164
|
|
|
165
|
## Environment Variables in Plugins |
|
166
|
|
|
167
|
<Note> |
|
168
|
|
|
169
|
Environment variable support in plugins is available starting [Medusa v2.11.0](https://github.com/medusajs/medusa/releases/tag/v2.11.0). Refer to the [Medusa versions prior to v2.11.0](#for-medusa-versions-prior-to-v2110) section for more details if you're using an earlier version. |
|
170
|
|
|
171
|
</Note> |
|
172
|
|
|
173
|
For plugins, you can use environment variables without a prefix. Then, Medusa applications that use the plugin can set the environment variable with the `PLUGIN_` prefix. |
|
174
|
|
|
175
|
For example, you can create a widget in your plugin that uses the `MY_API_KEY` environment variable: |
|
176
|
|
|
177
|
```tsx highlights={[["8"]]} |
|
178
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
179
|
import { Container, Heading } from "@medusajs/ui" |
|
180
|
|
|
181
|
const ProductWidget = () => { |
|
182
|
return ( |
|
183
|
<Container className="divide-y p-0"> |
|
184
|
<div className="flex items-center justify-between px-6 py-4"> |
|
185
|
<Heading level="h2">API Key: {import.meta.env.MY_API_KEY}</Heading> |
|
186
|
</div> |
|
187
|
</Container> |
|
188
|
) |
|
189
|
} |
|
190
|
|
|
191
|
export const config = defineWidgetConfig({ |
|
192
|
zone: "product.details", |
|
193
|
}) |
|
194
|
|
|
195
|
export default ProductWidget |
|
196
|
``` |
|
197
|
|
|
198
|
Then, in the Medusa application that uses the plugin, set the environment variable with the `PLUGIN_` prefix: |
|
199
|
|
|
200
|
```bash |
|
201
|
PLUGIN_MY_API_KEY=sk_123 |
|
202
|
``` |
|
203
|
|
|
204
|
The `MY_API_KEY` environment variable in the plugin will be replaced with the value of `PLUGIN_MY_API_KEY` during the build process of the Medusa application. |
|
205
|
|
|
206
|
### Global Variables in Plugins |
|
207
|
|
|
208
|
Plugins also have the following global variables available: |
|
209
|
|
|
210
|
- `__BACKEND_URL__`: The URL of the Medusa backend, as set in the [Medusa configurations](../../../configurations/medusa-config/page.mdx#backendurl). |
|
211
|
- `__BASE__`: The base path of the Medusa Admin. (For example, `/app`). |
|
212
|
- `__STOREFRONT_URL__`: The URL of the Medusa Storefront, as set in the [Medusa configurations](../../../configurations/medusa-config/page.mdx#storefronturl). |
|
213
|
|
|
214
|
You can use those variables in your Medusa Admin customizations of a plugin. For example: |
|
215
|
|
|
216
|
```tsx highlights={[["8"]]} |
|
217
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
218
|
import { Container, Heading } from "@medusajs/ui" |
|
219
|
|
|
220
|
const ProductWidget = () => { |
|
221
|
return ( |
|
222
|
<Container className="divide-y p-0"> |
|
223
|
<div className="flex items-center justify-between px-6 py-4"> |
|
224
|
<Heading level="h2">Backend URL: {__BACKEND_URL__}</Heading> |
|
225
|
</div> |
|
226
|
</Container> |
|
227
|
) |
|
228
|
} |
|
229
|
|
|
230
|
export const config = defineWidgetConfig({ |
|
231
|
zone: "product.details", |
|
232
|
}) |
|
233
|
|
|
234
|
export default ProductWidget |
|
235
|
``` |
|
236
|
|
|
237
|
To fix possible type errors, create the file `src/admin/vite-env.d.ts` and add the global variables: |
|
238
|
|
|
239
|
```ts title="src/admin/vite-env.d.ts" |
|
240
|
/// <reference types="vite/client" /> |
|
241
|
|
|
242
|
declare const __BACKEND_URL__: string |
|
243
|
declare const __BASE__: string |
|
244
|
declare const __STOREFRONT_URL__: string |
|
245
|
``` |
|
246
|
|
|
247
|
### For Medusa versions prior to v2.11.0 |
|
248
|
|
|
249
|
<Details summaryContent="Instructions for Medusa versions prior to v2.11.0"> |
|
250
|
|
|
251
|
As explained in the [Environment Variables in Production section](#environment-variables-in-production), environment variables are inlined into the build. This presents a limitation for plugins, where you can't use environment variables. |
|
252
|
|
|
253
|
Instead, you can use the [Plugin Global Variables](#global-variables-in-plugins) described above to access the backend URL, base path, and storefront URL. |
|
254
|
|
|
255
|
</Details> |