Backend Environment Variables
This page describes how to set environment variables for your functions/servers on the Genezio platform. Environment variables are used to store information or configuration data that you do not want to hardcode in your code such as API keys, database URLs,etc.
How to set environment variables
You can pass environment variables to your functions in the following ways:
- Individually set environment variables using the Genezio dashboard
- In bulk using the Genezio CLI by providing a
.env
file - Making use of the
genezio.yaml
file to set environment variables
Set environment variables using the Genezio dashboard
To set environment variables, head to the Dashboard and select the project you want to add environment variables to.
Navigate to the Environment Variables
tab and add the environment variables like a <key, value>
pair. After adding all the environment variables hit the Save
button.
Note: The environment variables are immediately injecting into the execution environment of your functions without any need to redeploy.
Set environment variables using the genezio
CLI
You can set environment variables using the genezio CLI by providing a .env
file and running the following command:
# E.G. genezio deploy --env backend/.env
genezio deploy --env <your-env-file-path>
The --env
flag refers only to backend functions/classes or persistent servers. For deploying frontend applications, you can rely on each specific framework's approach to handle .env
files.
Set environment variables using the genezio.yaml
file
The genezio.yaml
file can be used to set environment variables for your functions/servers in the following ways:
- Set literal values for environment variables such as
NODE_ENV: production
- Reference environment variables from other services or functions using the
${{}}
syntax such as FUNCTION_URL:${{backend.functions.<function-name>.url}}
You can add the environment variables in the genezio.yaml
file as shown below:
name: my-project
region: us-east-1
yamlVersion: 2
backend:
# ...
environment:
NODE_ENV: production
MY_DATABASE_URI: ${{services.databases.<database-name>.uri}}
FUNCTION_URL: ${{backend.functions.<function-name>.url}}
How to use environment variables when testing locally
By default, the environment variables are automatically loaded from the .env
file when testing locally with genezio local
.
The default path and name of the environment variables file is the backend path mentioned in the genezio.yaml
file. i.e. backend.path/.env
:
name: my-project
region: us-east-1
yamlVersion: 2
backend:
path: ./server # genezio will search for the .env file in the ./server directory
If you want to use a different path or name for the .env
file, you can specify it using the --env
flag when running the genezio local
command:
# E.G. genezio local --env backend/.env
genezio local --env <your-env-file-path>
Note: You do not need to explicitly use a .env file loader (such as dotenv
or python-dotenv
) in your code.
genezio
has a built-in approach to load the environment variables from the .env
file during the local testing with genezio local
.
How to use environment variables in your code
The environment variables used on the deployed environment are exported.
- TypeScript
- JavaScript
- Python
To access an environment variable use process.env.MY_VARIABLE
const myVariable = process.env.MY_VARIABLE;
console.log("Print environment variable", myVariable);
To access an environment variable use process.env.MY_VARIABLE
const myVariable = process.env.MY_VARIABLE;
console.log("Print environment variable", myVariable);
To access an environment variable use os.environ['MY_VARIABLE']
import os
my_variable = os.environ['MY_VARIABLE']
print("Print environment variable", my_variable)
Integration environment variables
Integrations will automatically add environment variables to your project.
You can always check the environment variables added to your project in the Environment Variables
tab or in the Integrations
tab in the Genezio dashboard.