# Environment Variables

> How to configure environment variables for your application on Daki Hosting using a .env file.

Environment variables let you pass configuration values — like a Discord bot token or an API key — to your application without hardcoding them in your source code. On Daki Hosting, the recommended approach is to create a **`.env` file** directly on your server using the Portal's file manager or SFTP.

## What you'll learn

- What environment variables are and why they matter
- How to create a `.env` file on your server
- How to load `.env` values in Node.js and Python
- Best practices for keeping secrets safe

---

## What Are Environment Variables?

Environment variables are key-value pairs that your running application can read from its environment. They are the standard way to:

- Store sensitive values (tokens, API keys, passwords) separately from code
- Change configuration without modifying or redeploying source files
- Avoid committing secrets to version control

---

## Creating a `.env` File

The simplest and most reliable way to manage environment variables on Daki is to create a `.env` file on your server. You can do this in two ways:

### Option 1: Using the File Manager (quick edits)

1. Open your server in the Portal ([portal.daki.cc](https://portal.daki.cc)).
2. Go to the **Files** tab.
3. Click **New File** and name it `.env`.
4. Click the file to open it in the web editor and add your variables:
   ```
   BOT_TOKEN=your_discord_bot_token_here
   PORT=3000
   DATABASE_URL=your_db_connection_string
   ```
5. Click **Save**.
6. Restart your server.

### Option 2: Using SFTP (recommended for initial setup)

Connect to your server via SFTP (credentials in the **Settings** tab) and upload your `.env` file directly from your local machine. See [File Manager](https://daki.cc/wiki/guides/file-manager) for SFTP setup instructions.

> **Warning:** Never commit your `.env` file to a public Git repository. Add `.env` to your `.gitignore` file to prevent accidental exposure of secrets.

---

## Loading `.env` Values in Your Application

Applications do not automatically read `.env` files — you need a library to load them into the environment.

**Node.js (dotenv)**

Install the package (add it to your `package.json` as a dependency):
```json
{
  "dependencies": {
    "dotenv": "^16.0.0"
  }
}
```
Load it at the very top of your entry file:
```js
require('dotenv').config();

const token = process.env.BOT_TOKEN;
const port  = process.env.PORT || 3000;
```

**Python (python-dotenv)**

Add `python-dotenv` to your `requirements.txt`:
```
python-dotenv
```
Load it at the top of your entry file:
```python
from dotenv import load_dotenv
import os

load_dotenv()

token = os.environ.get("BOT_TOKEN")
port  = int(os.environ.get("PORT", 3000))
```

---

## `.env` File Format

Each line in a `.env` file follows the format `KEY=VALUE`:

```

# Comments start with a hash

BOT_TOKEN=your_token_here

API_KEY=some_api_key

DB_HOST=localhost

DB_PORT=5432

DEBUG=false

```
- Keys are typically uppercase with underscores.
- Values do not need quotes, but quotes are supported.
- Lines starting with `#` are comments and are ignored.

---

## Best Practices

- **Never hardcode tokens or secrets** in your source code files, especially if your code is on GitHub.
- **Add `.env` to your `.gitignore`** so it is never accidentally committed.
- **Create your `.env` file on the server**, not locally — or use SFTP to upload a local `.env` that is excluded from version control.
- **Restart your server** after creating or modifying your `.env` file.
- Use **descriptive key names** that make your configuration self-documenting.

---

## Next Steps

- [Startup Settings](https://daki.cc/wiki/guides/startup-settings) — Configure your install command, startup command, and Docker image.
- [Deploy a Discord.js Bot](https://daki.cc/wiki/guides/deploy-discordjs) — Full deployment walkthrough including token setup.
- [Deploy a Python Bot](https://daki.cc/wiki/guides/deploy-python) — Full deployment walkthrough including token setup.

---
Source: https://daki.cc/wiki/guides/environment-variables
