Hosting operations2 minutes to read

How do I keep my bot token secret when I host my bot?

Keep Discord and Telegram tokens out of your code and Git history, store them as environment variables, and what to do if a token leaks.

Store it as a variable on the host, never in the code

Put the token in an environment variable on the host, and have your code read it at startup. On Bothosting, save it in the deployment’s Variables as BOT_TOKEN for Discord or TELEGRAM_BOT_TOKEN for Telegram. Keep it out of source files, Git, screenshots and chat messages. Anyone holding the token can control the bot.

Read the token from the environment

Node.js
client.login(process.env.BOT_TOKEN);
Python
import os
bot.run(os.environ['BOT_TOKEN'])

For local development, keep a .env file and add it to .gitignore. Don’t upload it to the host. Variables replaces it there. The Bothosting CLI skips common environment and key files when it uploads.

If a token leaked

  1. Reset it straight away: in the Discord Developer Portal on the Bot page, or with /revoke in BotFather for Telegram.
  2. Save the new token in Variables and restart the deployment.
  3. If it was committed to Git, deleting the file is not enough, because the old commit still holds it. The reset is what makes it safe. Clean the history afterwards if the repository is public.

Discord scans public GitHub repositories for bot tokens and may reset a token it finds. A bot that suddenly fails to log in after a push may have leaked its token.

Other secrets work the same way

Database URLs, API keys and webhook secrets belong in Variables too. When you connect an AI coding agent, give it its own scoped token, not your account password or bot token. See setting and rotating secrets and connecting an AI agent safely.