Environment Variables
In this chapter, you'll learn how environment variables are loaded in MedusaGo.
System Environment Variables#
The MedusaGo application loads and uses system environment variables.
For example, if you set the PORT environment variable to 8000, the MedusaGo application runs on that port instead of 9000.
In production, you should always use system environment variables that you set through your hosting provider. A system environment variable always takes precedence over a value in a .env file.
Environment Variables in .env Files#
During development, it's easier to set environment variables in a .env file in your repository. The MedusaGo CLI loads the .env file of the directory you run it from before it runs a command, so it applies to go run . develop, db:migrate, user, and every other command.
Based on your NODE_ENV system environment variable, the CLI also loads an environment-specific file when it loads your configuration:
NODE_ENV defaults to production when you run start. Otherwise, it defaults to development. Set it explicitly to override the default.Environment |
|
|---|---|
|
|
|
|
|
|
|
|
A variable is only set if it isn't already set. So the precedence, from highest to lowest, is the system environment, then .env, then .env.<NODE_ENV>. A value can reference other variables with $NAME or ${NAME}. For example, the installer writes DATABASE_URL=postgres://localhost/$DB_NAME in the .env file.
To make sure the correct file is loaded as shown in the table, only use development, production, staging, or test as the value of NODE_ENV.
.env handling follows the dotenv format: lines such as KEY=value, export KEY=value, quoted values, and # comments.Use Environment Variables in Your Configuration#
Read environment variables in medusa-config.go with os.Getenv. The CLI loads the .env files before it calls your configuration function:
import (
"os"
"github.com/hitkey-io/medusago/packages/core/framework/src/config"
)
// medusaConfig is the equivalent of medusa-config.ts. The CLI loads the .env
// files before calling it.
func medusaConfig() config.InputConfig {
return config.InputConfig{
ProjectConfig: config.ProjectConfig{
DatabaseURL: os.Getenv("DATABASE_URL"),
HTTP: config.HttpConfig{
StoreCors: os.Getenv("STORE_CORS"),
AdminCors: os.Getenv("ADMIN_CORS"),
AuthCors: os.Getenv("AUTH_CORS"),
JwtSecret: os.Getenv("JWT_SECRET"),
CookieSecret: os.Getenv("COOKIE_SECRET"),
},
},
}
}
Some options fall back to an environment variable when you leave them empty, as listed in the next section. You don't need to read those variables yourself.
Environment Variables for Admin Customizations#
Since the Medusa Admin is built on top of Vite, 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.
Learn more in the Admin Environment Variables chapter.
Predefined MedusaGo Environment Variables#
You can further customise the MedusaGo application's behaviour using the following predefined environment variables. You can set them in your .env file or in your deployment platform.
medusa-config.go where possible. For a full list of MedusaGo configurations, refer to the MedusaGo Configurations chapter.Environment Variable | Description | Default |
|---|---|---|
| The host to run the MedusaGo application on. The | All network interfaces |
| The port to run the MedusaGo application on. The |
|
| The environment the application runs in. It selects the |
|
| The URL to connect to the PostgreSQL database. Only used if projectConfig.databaseUrl isn't set in | - |
| The name of the database that |
|
| URLs of storefronts that can access the Store API routes. Only used if projectConfig.http.storeCors isn't set in |
|
| URLs of admin dashboards that can access the Admin API routes. Only used if projectConfig.http.adminCors isn't set in |
|
| URLs of clients that can access the authentication routes. Only used if projectConfig.http.authCors isn't set in |
|
| A random string used to create authentication tokens in the HTTP layer. Only used if projectConfig.http.jwtSecret isn't set in |
|
| A random string used to sign the session cookie. Only used if projectConfig.http.cookieSecret isn't set in |
|
| The URL of the MedusaGo server that the Medusa Admin sends requests to. Only used if admin.backendUrl isn't set in |
|
| The worker mode of the instance: |
|
| Toggles a feature flag, such as |
|
| The levels to log. Learn more in the Logging chapter. |
|
| The file to save logs in. By default, logs aren't saved in any file. Learn more in the Logging chapter. | - |
| When set to a non-empty value, the development logs aren't coloured. | - |
| The key that encrypts the secrets of multi-factor authentication. It's required to enrol or use an authentication factor. The installer generates a random value in the | - |
| A string indicating the authentication method that the JS SDK instance uses in the Medusa Admin. Possible values are |
|
| A string indicating the key used to store the authentication JWT token in the browser's local storage. Only applicable if |
|
| Numeric values whose magnitude is not greater than this are returned as |
|
REDIS_URL in your .env file. MedusaGo doesn't read this variable by itself: your medusa-config.go must pass it to the options that need a Redis URL, as the configurations chapter explains.MEDUSA_DISABLE_TELEMETRY variable.