Discord Bots

Using environment variables

CanerAkar Mar 17, 2026

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?

ApproachSecurityConvenience
Hardcoded in codeAnyone who sees your code sees your tokenMust edit code to change values
Config file (config.json)Better, but still in your code repoNeed to upload new file for changes
**.env file****Not part of your code****Edit one file to change values**
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 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
BOT_TOKEN=MTIzNDU2Nzg5.abc123.xyz789
BOT_PREFIX=!
GUILD_ID=123456789
  1. Save the file and restart your server for the changes to take effect
No spaces around the equals sign
Write BOT_TOKEN=yourtoken, not BOT_TOKEN = yourtoken. Extra spaces can cause the value to be read incorrectly.
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:
requirements.txt
discord.py
python-dotenv
Then load the .env file at the top of your app.py:
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)
MethodBehavior
`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:
package.json (dependencies section)
{
    "dependencies": {
        "discord.js": "latest",
        "dotenv": "latest"
    }
}
Then load the .env file at the top of your index.js:
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:
VariableExample valuePurpose
`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](/discord-bots/adding-slash-commands/) 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

Before (insecure)
client.run('MTIzNDU2Nzg5.abc123.xyz789')
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

Before (insecure)
client.login('MTIzNDU2Nzg5.abc123.xyz789');
After (secure)
require('dotenv').config();
client.login(process.env.BOT_TOKEN);
If you were using a config.json file:
Before (config.json)
const { token } = require('./config.json');
client.login(token);
After (.env file)
require('dotenv').config();
client.login(process.env.BOT_TOKEN);
You can then delete the config.json file from your server.

Troubleshooting

ProblemCauseFix
`KeyError: 'BOT_TOKEN'` (Python)`.env` file missing or `load_dotenv()` not calledCreate `.env` and add `load_dotenv()` before accessing variables
`TOKEN_INVALID`Token value is wrongCopy the token again from the [Developer Portal](/discord-bots/getting-your-bot-token/)
`ModuleNotFoundError: dotenv``python-dotenv` not installedAdd `python-dotenv` to `requirements.txt`
`Cannot find module 'dotenv'``dotenv` not in dependenciesAdd `dotenv` to `package.json` dependencies
Changes not taking effectServer not restartedRestart your server after editing `.env`

Next steps

Was this article helpful?