How to Set Environment Variables for Your Bot
Keep tokens, API keys, and secrets out of your code with env vars.
Introduction
Environment variables hold the secrets your code needs but shouldn't be committed to Git, Discord tokens, API keys, database URLs.
Set Env Vars on the Panel
- 1Open your bot β Startup β Environment Variables
- 2Click Add Variable
- 3Enter the key (e.g.
DISCORD_TOKEN) - 4Enter the value
- 5Click Save
- 6Restart the bot for changes to apply
The values are encrypted at rest and never shown in logs.
Read Env Vars From Code
Node.js:
const token = process.env.DISCORD_TOKEN;
client.login(token);Python:
import os
token = os.environ["DISCORD_TOKEN"]
bot.run(token)Common Variables to Set
DISCORD_TOKEN, your bot's token from Discord Developer PortalCLIENT_ID, your bot's application IDGUILD_ID, your test server ID (for slash command testing)DATABASE_URL, connection string for your databaseOPENAI_API_KEY, if your bot uses OpenAI
Using a .env File Locally
- Locally, use a
.envfile with the dotenv package - Add `.env` to `.gitignore`, never commit it
- On Endercloud, don't upload `.env`, use the panel's env vars instead
Tip: Name secrets consistently across local, staging, and prod. DISCORD_TOKEN everywhere, not token in dev and bot_token in prod.
Warning: If you accidentally commit a token, regenerate it immediately in the Discord Developer Portal. Don't just delete the commit, it's still in Git history.
Need More Help?
Variables not loading? Make sure the bot was restarted after saving. Ping Discord for help.
Was this helpful?
Your feedback helps us write better guides.
Related Articles
More from Bot Hosting.
How to Upload and Run Your Discord Bot
From local code to a live bot on Endercloud in five minutes.
How to Keep Your Bot Online 24/7
Auto-restart on crash, auto-start on reboot, the only two settings you need.
How to Use Python for Your Discord Bot on Endercloud
Set up a discord.py bot from scratch, the right Python version, the right libs.
Still need help?
Our team is on Discord around the clock. Real humans, real answers.