# Environment variables

Environment variables provide a standardized and crucial method for effectively managing sensitive information within your software projects. These variables serve as a secure repository for storing a wide range of confidential data, including but not limited to API keys, access tokens, database credentials, and configuration settings.

## Getting started

You can access all features by using the `GodotEnvironment` class in any part of your code. This class provides extra functionality while maintaining compatibility with `OS.get_environment()` and `OS.set_environment()` for runtime variable management.

By default, this class looks for **.env** files in the root of your project `res://` To modify this path, you can use:

```python
GodotEnvironment.ENVIRONMENT_FILES_PATH = "res://project"
```

or set the value on the Project settings -> GodotEnv -> Root directory:

<figure><img src="/files/pCnwHiqHTzdcWIQCeK57" alt=""><figcaption><p>Godot2DEssentials settings section</p></figcaption></figure>

## Variable tracker

For internal execution convenience, the plugin tracks active variables without storing their values in array, as storing sensitive content in this array can risk data leaks or unauthorized access. This approach allows you to verify which variables have been read and loaded into memory without exposing their values

```python
GodotEnvironment.ENVIRONMENT_VARIABLE_TRACKER
# ["ADDRESS", "PORT", "SERVER_ID]
```

### Example of use

````python
# .env file
ADDRESS=127.0.0.1
PORT=9492

SERVER_ID=1919

# random_script.gd
GodotEnvironment.load_env_file(".env")

GodotEnvironment.get_var("PORT") # Returns an empty string if does not exists
# or
GodotEnvironment.get_var_or_null("PORT") # Returns null instead
```
**You only need to load your environment variables once**, and there's no need to load them in every `_ready()` function in your nodes. If you have duplicate variables, the value used will be the one from the last occurrence. Therefore, it's important to review your files carefully to avoid unintentional overwriting.
```dotenv
ADDRESS=127.0.0.1
ADDRESS=192.168.1.55 # This is the one that will be assigned as it overwrites the previous one
````

## Loading variables from other files

This class supports reading multiple environment files. For production, it's highly advisable to refrain from including the `.env` file in your source code repository. Instead, consider providing on the repository an `.env.example` file with blank values for the keys used. This strategy enables you to duplicate the file and input the values in your local environment, thereby averting the inadvertent exposure of sensitive information.

```python
# .env.example
ADDRESS=
PORT=
SERVER_NAME=

# random_script.gd
GodotEnvironment.load_env_file(".env.example")
GodotEnvironment.load_env_file(".env.dev")
GodotEnvironment.load_env_file(".env.staging")
# ...
```

## Signals

```python
signal variable_added(key: String)
signal variable_removed(key: String)
signal variable_replaced(key: String)
signal env_file_loaded(filename: String)
```

## API reference

#### get\_var(key: String) -> String

This is an alternative to `OS.get_environment(key)`

```python
GodotEnvironment.get_var("SERVER_PORT")
```

#### get\_var\_or\_null(key: String)

Retrieve the value of an environment variable by its key or null it if it doesn't

```python
GodotEnvironment.get_var_or_null("SERVER_PORT")
```

#### set\_var(key: String, value: String = "") -> void

Set a environment variable on the runtime process, this is an alternative to `OS.set_environment(key, value)`

```python
GodotEnvironment.set_var("API_KEY", "991918291921")
```

#### remove\_var(key: String)-> void

Remove a variable from the runtime process

```python
GodotEnvironment.remove_var("API_KEY")
```

#### create\_environment\_file(filename: String = ".env", overwrite: bool = false) -> void

Create an environment file with the specified filename. If it already exists, it can be overwritten

```python
GodotEnvironment.create_environment_file(".env")
GodotEnvironment.add_var_to_file("env", "PORT", 3000)
GodotEnvironment.add_var_to_file("env", "ENCRYPTION_ALGORITHM", 'SHA256')
```

#### load\_env\_file(filename: String = ".env") -> void

Read an `.env` file and set the environment variables to be accessible in the code

```python
GodotEnvironment.load_env_file(".env.example")
```

#### flush\_environment\_variables(filename: String = ".env", except: Array\[String] = \[]) -> void

Remove environment variables from the current process runtime. You can add the keys that you do not want to be deleted in this process.

<pre class="language-python"><code class="lang-python">GodotEnvironment.flush_environment_variables(".env")
<strong>GodotEnvironment.flush_environment_variables(".env", ["IP_ADDRESS", "COUNTRY"])
</strong></code></pre>

#### add\_var\_to\_file(filename: String, key: String, value: String = "") -> void

Add a key-value pair to an environment file and set the environment variable

```python
GodotEnvironment.add_var_to_file("env", "PORT", 4500)
GodotEnvironment.add_var_to_file("env", "APP_NAME", 'FightingTournament')
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://godot-essentials.gitbook.io/addons-documentation/autoload/environment-variables.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
