Keeping your bot token safe

Keep your Discord bot token out of your code and out of sight.

Your bot token is the password to your bot. Anyone who has it can control your bot and anything it has access to.


Use a .env File

Keep your token in a file called .env instead of in your code:

DISCORD_TOKEN=your-token-here

Then load it in your code. In Node.js, use the dotenv package. In Python, use python-dotenv. Upload the .env file to your server with Files or SFTP.


Keep It Off GitHub

  • Add .env to your .gitignore so it's never pushed.

  • If you deploy from GitHub, upload the .env file to your server separately.


Be Careful Who Can See It

  • Anyone with file access on your server can read your .env. Only give subusers the permissions they need. See Adding subusers to your server.

  • Don't post screenshots of your files or console with the token showing.

  • Our staff will never ask for your bot token.


If Your Token Leaks

  1. Go to the Discord Developer Portal, open your application, then Bot.

  2. Click Reset Token. The old token stops working straight away.

  3. Put the new token in your .env file and restart your bot.


Related Articles

Was this article helpful?

Your feedback helps us improve the documentation. Thanks — we’re glad this article helped. Sorry about that. What were you hoping to find? Thanks — we’ll use your feedback to improve this article.