Guides
Add or update env variables

Add or update env variables

Why?

You may want to edit or add new environment variables, that are essential for enhancing the security and flexibility of an application. They keep sensitive information, like API keys and passwords, out of the codebase, reducing the risk of data leaks. Moreover, they allow configurations to be easily changed between different environments (development, testing, production) without altering the code, making the application more adaptable and maintainable.

How?

Environment variables stands in the .env file, at the root of your project, which isn't versioned in the git repository. The .env file is created when you execute the "getting started" steps of Start UI's README.md

Edit an environment variable

  1. Change the value in the .env file and save the file
  2. Depending on the environment variable, the change could take effect immediately
  3. If necessary, change the example value in the .env.example to spread the right default values to your colleagues

Add an environment variable

  1. Create a new environment variable in the .env. Note that environment variables should be prefixed by NEXT_PUBLIC_ if you want them to be accessible in the front-end.
  2. Since environment variables are validated with zod, you need to add it to the src/env.mjs :
    • In the client or server section, depending on where it will be used
    • Also in the runtimeEnv section
  3. To use the values, import the env variable like this and use the variable of your choice
import { env } from '@/env.mjs';

const BASE_URL = env.NEXT_PUBLIC_BASE_URL;
const LOG_LEVEL = env.LOGGER_LEVEL;
  1. Don't forget to add the new variable to the .env.example to help your colleagues when they'll set up their .env