Skip to content

Commit

Permalink
Merge pull request #316 from Genez-io/update-environment-variables-docs
Browse files Browse the repository at this point in the history
Update documentation on environment variables
  • Loading branch information
andreia-oca authored Feb 24, 2025
2 parents 30f582c + da8b9b5 commit 2166e4b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 68 deletions.
2 changes: 1 addition & 1 deletion docs/frameworks/typesafe-project.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ Deploying your frontend to genezio infrastructure...
No subdomain is specified in the genezio.yaml configuration file. We will provide a random one for you.
App Dashboard URL: https://app.genez.io/project/<projectId>/<projectEnvId>
Frontend URL: https://<subdomain>.dev.app.genez.io
Frontend URL: https://<subdomain>.app.genez.io
```

After the deployment succeeds, you can access the [Dashboard](https://app.genez.io), check the logs of the project, and use the [Test Interface](/docs/features/testing) to call your backend functions manually.
Expand Down
4 changes: 2 additions & 2 deletions docs/genezio-typesafe/genezio-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ This is an example of how a `GnzContext` might look like:
"Accept-Language": "en-US,en;q=0.9",
"Content-Length": "336",
"Content-Type": "application/json",
"Origin": "https://dev.app.genez.io",
"Origin": "https://app.genez.io",
"Priority": "u=1, i",
"Referer": "https://dev.app.genez.io/",
"Referer": "https://app.genez.io/",
"Sec-Ch-Ua": "\"Google Chrome\";v=\"125\", \"Chromium\";v=\"125\", \"Not.A/Brand\";v=\"24\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
Expand Down
140 changes: 81 additions & 59 deletions docs/project-structure/backend-environment-variables.md
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).
11 changes: 5 additions & 6 deletions docs/project-structure/genezio-configuration-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ backend:
environment:
MY_ENV_VAR: my-value
MY_DATABASE_NAME: ${{services.databases.<database-name>.name}}
MY_SECRET: ${{env.SECRET}}
MY_FUNCTION_URL: ${{backend.functions.<function-name>.url}}
```

#### `scripts`: `Object` **Optional**
Expand Down Expand Up @@ -782,7 +782,7 @@ The Docker container configuration. This field can be omitted if the project is
environment:
MY_ENV_VAR: my-value
MY_DATABASE_NAME: ${{services.databases.<database-name>.name}}
MY_SECRET: ${{env.SECRET}}
MY_FUNCTION_URL: ${{backend.functions.<function-name>.url}}
```

- `timeout`: `number` **Optional**
Expand Down Expand Up @@ -906,7 +906,7 @@ You can use the `nextjs`, `nuxt`, `nitro`, `nestjs`, `remix`, or `streamlit` fie
environment:
MY_ENV_VAR: my-value
MY_DATABASE_NAME: ${{services.databases.<database-name>.name}}
MY_SECRET: ${{env.SECRET}}
MY_FUNCTION_URL: ${{backend.functions.<function-name>.url}}
```

- `packageManager`: `npm` | `pnpm` | `yarn` | `pip` **Optional**
Expand Down Expand Up @@ -1050,7 +1050,6 @@ These variables are replaced with their values when resources are created or whe

Genezio supports the following formats:

- `${{env.ENV_KEY}}` - this will be loaded from a `.env` file or from the global process environment variables.
- `{{resource.path.field}}` - this format can be used to reference fields from the `genezio.yaml` itself - e.g. `{{backend.functions.<function-name>.name}}`.

Expressions can be used in the following YAML fields:
Expand All @@ -1063,7 +1062,7 @@ Expressions can be used in the following YAML fields:
- `services.authentication.settings.resetPassword.redirectUrl`
- `services.authentication.settings.emailVerification.redirectUrl`

You can concatenate expressions with strings - e.g. `prefix-${{env.ENV_KEY}}-suffix`.
You can concatenate expressions with strings - e.g. `prefix-${{backend.functions.<function-name>.name}}-suffix`.

For frontend environment variables, you can use expressions like (not limited to):

Expand Down Expand Up @@ -1098,7 +1097,7 @@ backend:
environment:
NODE_ENV: production
MY_DATABASE_URI: ${{services.databases.<database-name>.uri}}
MY_SECRET: ${{env.SECRET}}
FUNCTION_URL: ${{backend.functions.<function-name>.url}}
#...
```

Expand Down

0 comments on commit 2166e4b

Please sign in to comment.