-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #316 from Genez-io/update-environment-variables-docs
Update documentation on environment variables
- Loading branch information
Showing
4 changed files
with
89 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 81 additions & 59 deletions
140
docs/project-structure/backend-environment-variables.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,139 @@ | ||
--- | ||
sidebar_position: 4 | ||
description: Learn to manage backend environment variables in Genezio, including setting, importing, and using them effectively in your projects | ||
description: Learn how to set, manage, and use backend environment variables in Genezio. Explore the Genezio CLI, genezio.yaml file, and dashboard methods for configuring secure API keys, database URLs, and app settings | ||
--- | ||
|
||
import Tabs from '@theme/Tabs'; | ||
|
||
import TabItem from '@theme/TabItem'; | ||
|
||
import useBaseUrl from '@docusaurus/useBaseUrl'; | ||
|
||
# Backend Environment Variables | ||
|
||
<head> | ||
<title>Backend Environment Variables | Genezio Documentation</title> | ||
<title>How to Set Backend Environment Variables in Genezio | Genezio Documentation</title> | ||
</head> | ||
This page describes how to set environment variables for your backend classes. | ||
|
||
These variables can be either secrets such as database keys or 3rd party API keys, or variables to use globally across your backend classes. | ||
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. | ||
|
||
### Set environment variables using the genezio dashboard | ||
## How to set environment variables | ||
|
||
To set environment variables in the backend classes, head to the [Dashboard](https://dev.app.genez.io/dashboard) page of the project. | ||
You can pass environment variables to your functions in the following ways: | ||
* Individually set environment variables using the [Genezio dashboard](https://app.genez.io/dashboard) | ||
* In bulk using the [Genezio CLI](https://genezio.com/docs/cli/) by providing a `.env` file | ||
* Making use of the `genezio.yaml` file to set environment variables | ||
|
||
Click on the `Environment Variables` button on the sidebar: | ||
### Set environment variables using the Genezio dashboard | ||
|
||
Add the environment variables like a `<key, value>` pair. After adding all the environment variables hit the `Save` button: | ||
To set environment variables, head to the [Dashboard](https://app.genez.io/dashboard) and select the project you want to add environment variables to. | ||
|
||
Note: You can also import environment variables from a file using `Import from .env` button. | ||
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. | ||
|
||
### Set environment variables using genezio CLI | ||
Note: The environment variables are immediately injecting into the execution environment of your functions without any need to redeploy. | ||
|
||
You can load your environment variables when deploying `genezio` in the CLI by appending the following flag: | ||
### 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: | ||
|
||
```bash | ||
# E.G. genezio deploy --env backend/.env | ||
genezio deploy --env <your-env-file-path> | ||
``` | ||
|
||
<!-- :::info --> | ||
|
||
:::info | ||
Depending on your project's structure - a fullstack single repository or dedicated repositories for backend and frontend - be careful to provide the correct path to the file: | ||
|
||
`--env .env` or `--env ./server/.env`. | ||
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. | ||
::: | ||
|
||
<!-- ::: --> | ||
|
||
### Use environment variables locally | ||
### Set environment variables using the `genezio.yaml` file | ||
|
||
When testing locally with genezio local, the environment variable are loaded by default without any prior flags. | ||
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}}` | ||
|
||
For fullstack single repositories, the default path and name of the environment variables file will be: | ||
You can add the environment variables in the `genezio.yaml` file as shown below: | ||
|
||
``` | ||
<root-directory>/<workspace.backend>/.env | ||
```yaml | ||
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}} | ||
``` | ||
For dedicated server repositories, the default path and name of the environment variables file will be: | ||
## How to use environment variables when testing locally | ||
``` | ||
<root-directory>/.env | ||
``` | ||
By default, the environment variables are automatically loaded from the `.env` file when testing locally with `genezio local`. | ||
|
||
If the file used to store environment variables has a different name or is located to a different path, you must provide the new name/location using the `.env` flag: | ||
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`: | ||
|
||
```yaml | ||
name: my-project | ||
region: us-east-1 | ||
yamlVersion: 2 | ||
backend: | ||
path: ./server # genezio will search for the .env file in the ./server directory | ||
``` | ||
genezio local --env ./custom-path/.my-env | ||
|
||
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: | ||
|
||
```bash | ||
# E.G. genezio local --env backend/.env | ||
genezio local --env <your-env-file-path> | ||
``` | ||
|
||
Note: There is no need for you to explicitly use the `dotenv` library in your code. | ||
:::info | ||
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 the environment variables in your project | ||
## How to use environment variables in your code | ||
|
||
The environment variables used on the deployed environment are exported. | ||
<Tabs> | ||
<TabItem className="tab-item" value="ts" label="TypeScript"> | ||
To access an environment variable use `process.env.MY_VARIABLE` | ||
<TabItem className="tab-item" value="ts" label="TypeScript"> | ||
To access an environment variable use `process.env.MY_VARIABLE` | ||
|
||
```typescript title="main.ts" | ||
const myVariable = process.env.MY_VARIABLE; | ||
console.log("Print environment variable", myVariable); | ||
``` | ||
|
||
</TabItem> | ||
```typescript title="main.ts" | ||
const myVariable = process.env.MY_VARIABLE; | ||
console.log("Print environment variable", myVariable); | ||
``` | ||
</TabItem> | ||
<TabItem className="tab-item" value="js" label="JavaScript"> | ||
To access an environment variable use `process.env.MY_VARIABLE` | ||
|
||
```javascript title="main.js" | ||
const myVariable = process.env.MY_VARIABLE; | ||
console.log("Print environment variable", myVariable); | ||
``` | ||
</TabItem> | ||
<TabItem className="tab-item" value="python" label="Python"> | ||
To access an environment variable use `os.environ['MY_VARIABLE']` | ||
|
||
```python title="main.py" | ||
import os | ||
my_variable = os.environ['MY_VARIABLE'] | ||
print("Print environment variable", my_variable) | ||
``` | ||
</TabItem> | ||
<TabItem className="tab-item" value="dart" label="Dart"> | ||
To access an environment variable use `Platform.environment['MY_VARIABLE']` | ||
</Tabs> | ||
|
||
```dart title="main.dart" showLineNumbers | ||
import 'dart:io'; | ||
## Integration environment variables | ||
|
||
void main() { | ||
my_variable = Platform.environment['MY_VARIABLE']; | ||
print(my_variable); | ||
} | ||
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](https://app.genez.io/dashboard). | ||
|
||
</TabItem> | ||
</Tabs> | ||
## Linking databases | ||
|
||
<!-- :::info --> | ||
Linking databases will automatically add the database URL to your environment variables. | ||
|
||
:::info | ||
Note: There is no need to import specific libraries for loading environment variables (such as dotenv).`genezio` will implicitly load the `.env` file while testing locally. | ||
::: | ||
For example, linking a MongoDB database named `my-mongo` will automatically add the `MY_MONGO_DATABASE_URL` environment variable to your project. | ||
|
||
You can always check the environment variables added to your project in the `Environment Variables` tab or in the `Databases/Connect` tab in the [Genezio dashboard](https://app.genez.io/dashboard). | ||
|
||
## Support | ||
|
||
<!-- ::: --> | ||
If you have any questions or need help, please reach out to us on [Discord](https://discord.com/invite/uc9H5YKjXv). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters