discord-hosting.cloud
20% Off First MonthSee prices

Guide

Discord bot token security: keep it out of your code

The bot token is the bot's password. Anyone who has it can log in as your bot, read what it can read and act in every server it is in. Keep it in one place, out of your code.

Updated 5 min read

Keep the token in .env

Put the token in a .env file next to your code and load it with dotenv (Node.js) or python-dotenv (Python). Upload .env to the server in Files; never paste the token into source files.

.gitignore
# .gitignore
.env
node_modules/
__pycache__/
.venv/

If the token leaks

  1. 1Reset it now

    In the Developer Portal, open the application, Bot, Reset Token. The old token stops working at once.

  2. 2Update the server

    Put the new token in .env on the server and restart the bot.

  3. 3Clean up

    Remove the token from wherever it leaked. If it was committed to git, it stays in the history: resetting is what makes it harmless. Discord may also reset a token by itself when it appears in a public GitHub repository.

  4. 4Check what happened

    Look at the audit log of your servers for actions the bot did not take, and remove permissions the bot does not need.

Good habits

  • Give the bot only the permissions it uses. Avoid Administrator.
  • Use a separate application and token for testing.
  • Never share the token in screenshots, support tickets or chat. Our support team will never ask for it.
  • Add people to your server in the panel's Users settings instead of sharing your account.