Discord bot environment variables
Environment variables keep secrets out of your source code. On shared Discord bot hosting they are the standard way to pass tokens, client IDs, and feature flags into the process.
Why not hard-code the token
If the token sits in index.js or bot.py and you push to a public repository, scrapers can find it within minutes. Anyone with the token can control the bot until you regenerate it in the Discord Developer Portal.
Common patterns
Panel environment fields
Most game-style panels expose a variables tab. Values there are injected into the container as real environment variables. Your code reads process.env.DISCORD_TOKEN (Node) or os.environ["DISCORD_TOKEN"] (Python).
.env files
A .env file in the project root is convenient for local development. Load it with dotenv (Node) or python-dotenv (Python). Add .env to .gitignore so it never ships to GitHub.
Hybrid
Production can prefer panel variables, while developers use .env on their machines. Read the panel value first, then fall back to .env only in development.
Names worth standardizing
DISCORD_TOKENโ bot tokenCLIENT_ID/APPLICATION_IDโ application id for slash commandsGUILD_IDโ optional, for guild-scoped command registration while testing- Feature flags such as
LOG_LEVELorPREFIX
Checklist before you go live
- Token is not in any tracked file.
- Startup still works after a fresh install (dependencies only from package manifests).
- Missing token fails fast with a clear error instead of a vague gateway failure.
- If you ever leaked a token, reset it in the Developer Portal and update the panel.