Special Offer: Save Upto 10% on Game Servers & Bot Hosting
Bot Hosting2 min read

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

  1. 1Open your bot β†’ Startup β†’ Environment Variables
  2. 2Click Add Variable
  3. 3Enter the key (e.g. DISCORD_TOKEN)
  4. 4Enter the value
  5. 5Click Save
  6. 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 Portal
  • CLIENT_ID, your bot's application ID
  • GUILD_ID, your test server ID (for slash command testing)
  • DATABASE_URL, connection string for your database
  • OPENAI_API_KEY, if your bot uses OpenAI

Using a .env File Locally

  • Locally, use a .env file 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.

Still need help?

Our team is on Discord around the clock. Real humans, real answers.