docs: document admin environment variables (#11313)
This commit is contained in:
1 parent
98236c8262
commit
d348204fe5
12 files changed
+127
-22
No files matched your search
@@ -0,0 +1,70 @@
|
||||
export const metadata = {
|
||||
title: `${pageNumber} Environment Variables in Admin Customizations`,
|
||||
}
|
||||
|
||||
# {metadata.title}
|
||||
|
||||
In this chapter, you'll learn how to use environment variables in your admin customizations.
|
||||
|
||||
## How to Set Environment Variables
|
||||
|
||||
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_`.
|
||||
|
||||
For example:
|
||||
|
||||
```plain
|
||||
VITE_MY_API_KEY=sk_123
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## How to Use Environment Variables
|
||||
|
||||
To access or use an environment variable starting with `VITE_`, use the `import.meta.env` object.
|
||||
|
||||
For example:
|
||||
|
||||
```tsx highlights={[["8"]]}
|
||||
import { defineWidgetConfig } from "@medusajs/admin-sdk"
|
||||
import { Container, Heading } from "@medusajs/ui"
|
||||
|
||||
const ProductWidget = () => {
|
||||
return (
|
||||
<Container className="divide-y p-0">
|
||||
<div className="flex items-center justify-between px-6 py-4">
|
||||
<Heading level="h2">API Key: {import.meta.env.VITE_MY_API_KEY}</Heading>
|
||||
</div>
|
||||
</Container>
|
||||
)
|
||||
}
|
||||
|
||||
export const config = defineWidgetConfig({
|
||||
zone: "product.details.before",
|
||||
})
|
||||
|
||||
export default ProductWidget
|
||||
```
|
||||
|
||||
In this example, you display the API key in a widget using `import.meta.env.VITE_MY_API_KEY`.
|
||||
|
||||
### Type Error on import.meta.env
|
||||
|
||||
If you receive a type error on `import.meta.env`, create the file `src/admin/vite-env.d.ts` with the following content:
|
||||
|
||||
```ts title="src/admin/vite-env.d.ts"
|
||||
/// <reference types="vite/client" />
|
||||
```
|
||||
|
||||
This file tells TypeScript to recognize the `import.meta.env` object and enhances the types of your custom environment variables.
|
||||
|
||||
---
|
||||
|
||||
## Check Node Environment in Admin Customizations
|
||||
|
||||
To check the current environment, Vite exposes two variables:
|
||||
|
||||
- `import.meta.env.DEV`: Returns `true` if the current environment is development.
|
||||
- `import.meta.env.PROD`: Returns `true` if the current environment is production.
|
||||
|
||||
Learn more about other Vite environment variables in the [Vite documentation](https://vite.dev/guide/env-and-mode).
|
||||
|
||||
@@ -24,14 +24,16 @@ First, create the file `src/admin/lib/config.ts` to setup the SDK for use in you
|
||||
import Medusa from "@medusajs/js-sdk"
|
||||
|
||||
export const sdk = new Medusa({
|
||||
baseUrl: "http://localhost:9000",
|
||||
debug: process.env.NODE_ENV === "development",
|
||||
baseUrl: import.meta.env.VITE_BACKEND_URL || "/",
|
||||
debug: import.meta.env.DEV,
|
||||
auth: {
|
||||
type: "session",
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
Notice that you use `import.meta.env` to access environment variables in your customizations, as explained in [this chapter](../environment-variables/page.mdx).
|
||||
|
||||
<Note>
|
||||
|
||||
Learn more about the JS SDK's configurations [this documentation](!resources!/js-sdk#js-sdk-configurations).
|
||||
|
||||
Reference in new issue
Block a user