---
title: "Discord Bot Environment Variables - Secure Config | FPS.ms"
description: "Store your bot token and config values securely using a .env file on FPS.ms. Never hardcode secrets in your Discord bot code."
url: https://fps.ms/docs/discord-bots/using-environment-variables
---
# Using environment variables

Environment variables let you store your bot token, API keys, and configuration values outside of your code. On FPS.ms, you do this with a `.env` file. This keeps secrets secure and makes it easy to change settings without editing your bot's source files.

## Why use environment variables?

| Approach | Security | Convenience |
|----------|----------|-------------|
| Hardcoded in code | Anyone who sees your code sees your token | Must edit code to change values |
| Config file (config.json) | Better, but still in your code repo | Need to upload new file for changes |
| **.env file** | **Not part of your code** | **Edit one file to change values** |

!!! danger "Never hardcode your token" "If your bot token is in your code and you push to a public GitHub repo, your bot will be compromised within minutes. Bots scrape GitHub for leaked tokens. Always use a `.env` file."

## Creating a .env file on FPS.ms

1. Go to [panel.fps.ms](https://panel.fps.ms) and select your Discord bot server
2. Open the **Files** tab
3. Create a new file named `.env` in the root directory
4. Add your variables in `KEY=value` format, one per line:

```env title=".env"
BOT_TOKEN=MTIzNDU2Nzg5.abc123.xyz789
BOT_PREFIX=!
GUILD_ID=123456789
```

5. Save the file and **restart your server** for the changes to take effect

!!! warning "No spaces around the equals sign" "Write `BOT_TOKEN=yourtoken`, not `BOT_TOKEN = yourtoken`. Extra spaces can cause the value to be read incorrectly."

!!! tip "Keep .env out of Git" "If you use a GitHub repository to deploy your bot, add `.env` to your `.gitignore` file so your token is never pushed to GitHub. Create the `.env` file directly on the FPS.ms panel instead."

## Reading environment variables in your code

### Python

Python needs the `python-dotenv` package to load `.env` files. Add it to your `requirements.txt`:

```txt title="requirements.txt"
discord.py
python-dotenv
```

Then load the `.env` file at the top of your `app.py`:

```py title="app.py"
import os
from dotenv import load_dotenv
import discord

load_dotenv()

TOKEN = os.environ['BOT_TOKEN']
PREFIX = os.environ.get('BOT_PREFIX', '!')  # defaults to '!' if not set

intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)
client.run(TOKEN)
```

| Method | Behavior |
|--------|----------|
| `os.environ['BOT_TOKEN']` | Raises `KeyError` if not set — use for required values |
| `os.environ.get('BOT_PREFIX', '!')` | Returns default value if not set — use for optional values |

### Node.js

Node.js needs the `dotenv` package to load `.env` files. Add it to your `package.json`:

```json title="package.json (dependencies section)"
{
    "dependencies": {
        "discord.js": "latest",
        "dotenv": "latest"
    }
}
```

Then load the `.env` file at the top of your `index.js`:

```js title="index.js"
require('dotenv').config();
const { Client, GatewayIntentBits } = require('discord.js');

const TOKEN = process.env.BOT_TOKEN;
const PREFIX = process.env.BOT_PREFIX || '!'; // defaults to '!' if not set

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.login(TOKEN);
```

## Common environment variables for Discord bots

Here are some useful variables you might want to set in your `.env` file:

| Variable | Example value | Purpose |
|----------|---------------|---------|
| `BOT_TOKEN` | `MTIz...abc` | Your Discord bot token (required) |
| `BOT_PREFIX` | `!` | Command prefix for message-based commands |
| `GUILD_ID` | `123456789` | Your test server ID for development |
| `OWNER_ID` | `987654321` | Your Discord user ID for owner-only commands |
| `CLIENT_ID` | `111222333` | Your application ID for [slash command](https://fps.ms/docs/discord-bots/adding-slash-commands.md) registration |
| `DATABASE_URL` | `sqlite:///bot.db` | Database connection string |

## Migrating from hardcoded tokens

If your bot currently has the token hardcoded, here is how to migrate:

### Python — before and after

```py title="Before (insecure)"
client.run('MTIzNDU2Nzg5.abc123.xyz789')
```

```py title="After (secure)"
import os
from dotenv import load_dotenv

load_dotenv()
client.run(os.environ['BOT_TOKEN'])
```

Don't forget to add `python-dotenv` to your `requirements.txt`.

### Node.js — before and after

```js title="Before (insecure)"
client.login('MTIzNDU2Nzg5.abc123.xyz789');
```

```js title="After (secure)"
require('dotenv').config();
client.login(process.env.BOT_TOKEN);
```

If you were using a `config.json` file:

```js title="Before (config.json)"
const { token } = require('./config.json');
client.login(token);
```

```js title="After (.env file)"
require('dotenv').config();
client.login(process.env.BOT_TOKEN);
```

You can then delete the `config.json` file from your server.

## Troubleshooting

| Problem | Cause | Fix |
|---------|-------|-----|
| `KeyError: 'BOT_TOKEN'` (Python) | `.env` file missing or `load_dotenv()` not called | Create `.env` and add `load_dotenv()` before accessing variables |
| `TOKEN_INVALID` | Token value is wrong | Copy the token again from the [Developer Portal](https://fps.ms/docs/discord-bots/getting-your-bot-token.md) |
| `ModuleNotFoundError: dotenv` | `python-dotenv` not installed | Add `python-dotenv` to `requirements.txt` |
| `Cannot find module 'dotenv'` | `dotenv` not in dependencies | Add `dotenv` to `package.json` dependencies |
| Changes not taking effect | Server not restarted | Restart your server after editing `.env` |

## Next steps

- [Install your Discord bot](https://fps.ms/docs/discord-bots/installation.md) — full setup guide for Python and Node.js
- [Get your bot token](https://fps.ms/docs/discord-bots/getting-your-bot-token.md) — create an application and generate a token
- [Edit your code](https://fps.ms/docs/discord-bots/editing-code.md) — update files via the panel, SFTP, or VS Code
- [Add slash commands](https://fps.ms/docs/discord-bots/adding-slash-commands.md) — modern Discord bot commands

---

[FPS.ms](https://fps.ms/index.html.md) · [All pages](https://fps.ms/llms.txt) · [Documentation](https://fps.ms/docs/llms.txt)
