Skip to content

Commit d348204

Browse files
authored
docs: document admin environment variables (medusajs#11313)
1 parent 98236c8 commit d348204

File tree

12 files changed

+127
-22
lines changed

12 files changed

+127
-22
lines changed

www/apps/book/app/learn/customization/customize-admin/route/page.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ If you didn't follow the [previous chapter](../widget/page.mdx), create the file
156156
import Medusa from "@medusajs/js-sdk"
157157

158158
export const sdk = new Medusa({
159-
baseUrl: "http://localhost:9000",
160-
debug: process.env.NODE_ENV === "development",
159+
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
160+
debug: import.meta.env.DEV,
161161
auth: {
162162
type: "session",
163163
},
@@ -170,6 +170,8 @@ You initialize the SDK passing it the following options:
170170
- `debug`: Whether to enable logging debug messages. This should only be enabled in development.
171171
- `auth.type`: The authentication method used in the client application, which is `session` in the Medusa Admin dashboard.
172172

173+
Notice that you use `import.meta.env` to access environment variables in your customizations because the Medusa Admin is built on top of Vite. Learn more in [this chapter](../../../fundamentals/admin/environment-variables/page.mdx).
174+
173175
You can now use the SDK to send requests to the Medusa server.
174176

175177
<Note>

www/apps/book/app/learn/customization/customize-admin/widget/page.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ So, you'll start by configuring the JS SDK. Create the file `src/admin/lib/sdk.t
2929
import Medusa from "@medusajs/js-sdk"
3030

3131
export const sdk = new Medusa({
32-
baseUrl: "http://localhost:9000",
33-
debug: process.env.NODE_ENV === "development",
32+
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
33+
debug: import.meta.env.DEV,
3434
auth: {
3535
type: "session",
3636
},
@@ -43,6 +43,8 @@ You initialize the SDK passing it the following options:
4343
- `debug`: Whether to enable logging debug messages. This should only be enabled in development.
4444
- `auth.type`: The authentication method used in the client application, which is `session` in the Medusa Admin dashboard.
4545

46+
Notice that you use `import.meta.env` to access environment variables in your customizations because the Medusa Admin is built on top of Vite. Learn more in [this chapter](../../../fundamentals/admin/environment-variables/page.mdx).
47+
4648
You can now use the SDK to send requests to the Medusa server.
4749

4850
<Note>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
export const metadata = {
2+
title: `${pageNumber} Environment Variables in Admin Customizations`,
3+
}
4+
5+
# {metadata.title}
6+
7+
In this chapter, you'll learn how to use environment variables in your admin customizations.
8+
9+
## How to Set Environment Variables
10+
11+
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_`.
12+
13+
For example:
14+
15+
```plain
16+
VITE_MY_API_KEY=sk_123
17+
```
18+
19+
---
20+
21+
## How to Use Environment Variables
22+
23+
To access or use an environment variable starting with `VITE_`, use the `import.meta.env` object.
24+
25+
For example:
26+
27+
```tsx highlights={[["8"]]}
28+
import { defineWidgetConfig } from "@medusajs/admin-sdk"
29+
import { Container, Heading } from "@medusajs/ui"
30+
31+
const ProductWidget = () => {
32+
return (
33+
<Container className="divide-y p-0">
34+
<div className="flex items-center justify-between px-6 py-4">
35+
<Heading level="h2">API Key: {import.meta.env.VITE_MY_API_KEY}</Heading>
36+
</div>
37+
</Container>
38+
)
39+
}
40+
41+
export const config = defineWidgetConfig({
42+
zone: "product.details.before",
43+
})
44+
45+
export default ProductWidget
46+
```
47+
48+
In this example, you display the API key in a widget using `import.meta.env.VITE_MY_API_KEY`.
49+
50+
### Type Error on import.meta.env
51+
52+
If you receive a type error on `import.meta.env`, create the file `src/admin/vite-env.d.ts` with the following content:
53+
54+
```ts title="src/admin/vite-env.d.ts"
55+
/// <reference types="vite/client" />
56+
```
57+
58+
This file tells TypeScript to recognize the `import.meta.env` object and enhances the types of your custom environment variables.
59+
60+
---
61+
62+
## Check Node Environment in Admin Customizations
63+
64+
To check the current environment, Vite exposes two variables:
65+
66+
- `import.meta.env.DEV`: Returns `true` if the current environment is development.
67+
- `import.meta.env.PROD`: Returns `true` if the current environment is production.
68+
69+
Learn more about other Vite environment variables in the [Vite documentation](https://vite.dev/guide/env-and-mode).
70+

www/apps/book/app/learn/fundamentals/admin/tips/page.mdx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,16 @@ First, create the file `src/admin/lib/config.ts` to setup the SDK for use in you
2424
import Medusa from "@medusajs/js-sdk"
2525

2626
export const sdk = new Medusa({
27-
baseUrl: "http://localhost:9000",
28-
debug: process.env.NODE_ENV === "development",
27+
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
28+
debug: import.meta.env.DEV,
2929
auth: {
3030
type: "session",
3131
},
3232
})
3333
```
3434

35+
Notice that you use `import.meta.env` to access environment variables in your customizations, as explained in [this chapter](../environment-variables/page.mdx).
36+
3537
<Note>
3638

3739
Learn more about the JS SDK's configurations [this documentation](!resources!/js-sdk#js-sdk-configurations).

www/apps/book/app/learn/fundamentals/environment-variables/page.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,11 @@ In the `medusa-config.ts` file of your Medusa application, you'll find a `loadEn
9494
This function is responsible for loading the correct `.env` file based on the value of `process.env.NODE_ENV`.
9595

9696
To ensure that the correct `.env` file is loaded as shown in the table above, only specify `development`, `production`, `staging` or `test` as the value of `process.env.NODE_ENV` or as the parameter of `loadEnv`.
97+
98+
---
99+
100+
## Environment Variables for Admin Customizations
101+
102+
Since the Medusa Admin is built on top of [Vite](https://vite.dev/), you prefix the environment variables you want to use in a widget or UI route with `VITE_`. Then, you can access or use them with the `import.meta.env` object.
103+
104+
Learn more in [this documentation](../admin/environment-variables/page.mdx).

www/apps/book/generated/edit-dates.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export const generatedEditDates = {
5050
"app/learn/fundamentals/scheduled-jobs/execution-number/page.mdx": "2024-10-21T13:30:21.371Z",
5151
"app/learn/fundamentals/api-routes/parameters/page.mdx": "2024-11-19T16:37:47.251Z",
5252
"app/learn/fundamentals/api-routes/http-methods/page.mdx": "2024-10-21T13:30:21.367Z",
53-
"app/learn/fundamentals/admin/tips/page.mdx": "2025-01-27T08:45:19.023Z",
53+
"app/learn/fundamentals/admin/tips/page.mdx": "2025-02-05T09:07:52.584Z",
5454
"app/learn/fundamentals/api-routes/cors/page.mdx": "2024-12-09T13:04:04.357Z",
5555
"app/learn/fundamentals/admin/ui-routes/page.mdx": "2024-12-09T16:44:40.198Z",
5656
"app/learn/fundamentals/api-routes/middlewares/page.mdx": "2024-12-09T13:04:03.712Z",
@@ -88,8 +88,8 @@ export const generatedEditDates = {
8888
"app/learn/customization/extend-features/extend-create-product/page.mdx": "2025-01-06T11:18:58.250Z",
8989
"app/learn/customization/custom-features/page.mdx": "2024-12-09T10:46:28.593Z",
9090
"app/learn/customization/customize-admin/page.mdx": "2024-12-09T11:02:38.801Z",
91-
"app/learn/customization/customize-admin/route/page.mdx": "2025-02-04T07:35:04.523Z",
92-
"app/learn/customization/customize-admin/widget/page.mdx": "2025-01-27T08:45:19.022Z",
91+
"app/learn/customization/customize-admin/route/page.mdx": "2025-02-05T09:09:11.472Z",
92+
"app/learn/customization/customize-admin/widget/page.mdx": "2025-02-05T09:10:18.163Z",
9393
"app/learn/customization/extend-features/define-link/page.mdx": "2024-12-09T11:02:39.346Z",
9494
"app/learn/customization/extend-features/page.mdx": "2024-12-09T11:02:39.244Z",
9595
"app/learn/customization/extend-features/query-linked-records/page.mdx": "2024-12-09T11:02:39.519Z",
@@ -102,7 +102,7 @@ export const generatedEditDates = {
102102
"app/learn/introduction/architecture/page.mdx": "2025-01-16T10:25:10.780Z",
103103
"app/learn/fundamentals/data-models/infer-type/page.mdx": "2024-12-09T15:54:08.713Z",
104104
"app/learn/fundamentals/custom-cli-scripts/seed-data/page.mdx": "2024-12-09T14:38:06.385Z",
105-
"app/learn/fundamentals/environment-variables/page.mdx": "2024-12-09T11:00:57.428Z",
105+
"app/learn/fundamentals/environment-variables/page.mdx": "2025-02-05T09:15:49.196Z",
106106
"app/learn/build/page.mdx": "2024-12-09T11:05:17.383Z",
107107
"app/learn/deployment/general/page.mdx": "2024-11-25T14:33:50.439Z",
108108
"app/learn/fundamentals/workflows/multiple-step-usage/page.mdx": "2024-11-25T16:19:32.169Z",
@@ -115,5 +115,6 @@ export const generatedEditDates = {
115115
"app/learn/fundamentals/plugins/page.mdx": "2025-01-22T10:14:10.433Z",
116116
"app/learn/customization/reuse-customizations/page.mdx": "2025-01-22T10:01:57.665Z",
117117
"app/learn/update/page.mdx": "2025-01-27T08:45:19.030Z",
118-
"app/learn/fundamentals/module-links/query-context/page.mdx": "2025-02-03T17:04:24.479Z"
118+
"app/learn/fundamentals/module-links/query-context/page.mdx": "2025-02-03T17:04:24.479Z",
119+
"app/learn/fundamentals/admin/environment-variables/page.mdx": "2025-02-05T09:05:33.334Z"
119120
}

www/apps/book/generated/sidebar.mjs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -789,14 +789,23 @@ export const generatedSidebar = [
789789
"children": [],
790790
"chapterTitle": "3.9.2. Admin UI Routes"
791791
},
792+
{
793+
"loaded": true,
794+
"isPathHref": true,
795+
"type": "link",
796+
"path": "/learn/fundamentals/admin/environment-variables",
797+
"title": "Environment Variables",
798+
"children": [],
799+
"chapterTitle": "3.9.3. Environment Variables"
800+
},
792801
{
793802
"loaded": true,
794803
"isPathHref": true,
795804
"type": "link",
796805
"path": "/learn/fundamentals/admin/constraints",
797806
"title": "Constraints",
798807
"children": [],
799-
"chapterTitle": "3.9.3. Constraints"
808+
"chapterTitle": "3.9.4. Constraints"
800809
},
801810
{
802811
"loaded": true,
@@ -805,7 +814,7 @@ export const generatedSidebar = [
805814
"path": "/learn/fundamentals/admin/tips",
806815
"title": "Tips",
807816
"children": [],
808-
"chapterTitle": "3.9.4. Tips"
817+
"chapterTitle": "3.9.5. Tips"
809818
}
810819
],
811820
"chapterTitle": "3.9. Admin Development"

www/apps/book/sidebar.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,11 @@ export const sidebar = sidebarAttachHrefCommonOptions([
456456
path: "/learn/fundamentals/admin/ui-routes",
457457
title: "Admin UI Routes",
458458
},
459+
{
460+
type: "link",
461+
path: "/learn/fundamentals/admin/environment-variables",
462+
title: "Environment Variables",
463+
},
459464
{
460465
type: "link",
461466
path: "/learn/fundamentals/admin/constraints",

www/apps/resources/app/integrations/guides/sanity/page.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,8 +1454,8 @@ To configure the JS SDK, create the file `src/admin/lib/sdk.ts` with the followi
14541454
import Medusa from "@medusajs/js-sdk"
14551455

14561456
export const sdk = new Medusa({
1457-
baseUrl: "http://localhost:9000",
1458-
debug: process.env.NODE_ENV === "development",
1457+
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
1458+
debug: import.meta.env.DEV,
14591459
auth: {
14601460
type: "session",
14611461
},

www/apps/resources/app/js-sdk/page.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ For admin customizations, create this file at `src/admin/lib/config.ts`.
4848
import Medusa from "@medusajs/js-sdk"
4949

5050
export const sdk = new Medusa({
51-
baseUrl: "http://localhost:9000",
52-
debug: process.env.NODE_ENV === "development",
51+
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
52+
debug: import.meta.env.DEV,
5353
auth: {
5454
type: "session",
5555
},
@@ -78,6 +78,12 @@ export const sdk = new Medusa({
7878
</CodeTab>
7979
</CodeTabs>
8080

81+
<Note title="Tip">
82+
83+
In the Medusa Admin you use `import.meta.env` to access environment variables becaues the Medusa Admin is built on top of [Vite](https://vitejs.dev/). Learn more in [this documentation](!docs!/learn/fundamentals/admin/environment-variables).
84+
85+
</Note>
86+
8187
### JS SDK Configurations
8288

8389
The `Medusa` initializer accepts as a parameter an object with the following properties:

0 commit comments

Comments
 (0)