Learning objectives
After reading this article you will be able to:
- Find the token on the Bot tab of the Developer Portal
- Store the token in an environment variable, outside Git
- Reset the token and act fast if it leaks
The token is the credential your code uses to sign in to Discord as the bot. There is no username and password: whoever holds the token can do everything the bot can, such as deleting channels, banning members and posting messages as the bot.
That gives the topic two halves. The first is finding the token. The second, the one that matters, is keeping it out of repositories, screenshots and support chats.
Where do you find the bot token?#
The token lives in the Discord Developer Portal:
- Sign in to the Portal and open your application (or create one with New Application).
- In the left menu, open the Bot tab.
- Click Reset Token and confirm. The token appears on screen.
- Copy it straight into the place where you will store it (see the section below).
If you close the page without copying it, you cannot read the value again: reset it once more and use the new token.
Every reset invalidates the bot's old tokens. That is useful when something goes wrong, but it also knocks offline any process still using the old value.
How is the token used?#
Every call to Discord's REST API carries the token in the Authorization header with the Bot prefix, according to the API reference. Libraries (discord.js, discord.py, JDA) do this for you when you pass the token at login. To see it in action, this command returns the bot's own user data:
curl -H "Authorization: Bot $DISCORD_TOKEN" https://discord.com/api/v10/users/@meWith a valid token, the response includes the bot's id and username. With a wrong or reset token, you get 401: Unauthorized. This is the quickest way to check that the value you copied works, without starting the whole bot.
How do you store the token safely?#
There is one rule: the token lives in the environment, not in code. In development that means a .env file that Git ignores. In production it means the host's environment variable. The article on environment variables explains the mechanics.
Create .env in the project root:
DISCORD_TOKEN=paste_the_token_hereAnd make Git ignore it before the first commit:
echo ".env" >> .gitignore
git status # .env must not appear in the listHow do you read the token in Node.js?#
In recent Node.js versions, the --env-file flag loads .env without an extra package and is no longer experimental, per the Node.js documentation.
import { Client, Events, GatewayIntentBits } from "discord.js";
const token = process.env.DISCORD_TOKEN;
if (!token) {
console.error("Set DISCORD_TOKEN in the environment.");
process.exit(1);
}
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.once(Events.ClientReady, (c) => console.log(`Online as ${c.user.tag}`));
client.login(token);node --env-file=.env index.jsHow do you read the token in Python?#
With discord.py 2.x and the python-dotenv package:
import os
import discord
from dotenv import load_dotenv
load_dotenv()
token = os.environ["DISCORD_TOKEN"] # clear KeyError if missing
client = discord.Client(intents=discord.Intents.default())
@client.event
async def on_ready():
print(f"Online as {client.user}")
client.run(token)Using os.environ["..."] instead of os.getenv makes the program fail immediately, with a clear message, when the variable is missing.
What mistakes make a token leak?#
- Pasting the token into code.
client.login("MTA...")works and looks harmless until the firstgit push. - Committing
.env. It happens when.gitignoreis created after the first commit. The file is already in the history. - Showing the token in a screenshot or log. A
console.log(process.env)or a terminal screenshot posted to ask for help is enough. - Sharing the project as a ZIP.
.envtravels with the folder. - Sending the token in a message. In a support server, nobody needs your token to help you. Anyone who asks for it is a scammer.
GitHub scans repositories for Discord bot tokens: the discord_bot_token pattern is in the list of secret scanning patterns, with push protection and a partner alert. That helps, but do not treat it as your only defense, and do not assume Discord resets the token for you.
What do you do if the token leaked?#
Act in this order:
- Reset the token now. Portal, Bot tab, Reset Token. The leaked value stops working immediately.
- Update the new token on your host and in your local
.env, then restart the bot. - Check your server. Look for channels, roles, webhooks or invites you did not create.
- Clean the repository if you want (for example with
git filter-repo), but only after step 1. Rewriting history does not help if the old value was already copied, which is why resetting comes first.
If your bot had the Administrator permission, the possible damage is larger. Give the bot only the permissions it needs.
If the bot stays offline after the swap, read why a Discord bot goes offline: an old token forgotten in some environment is a common cause.
How do you separate test and production tokens?#
Create two applications in the Portal: one for development and one for the bot that stays online. Each has its own token. A leak of the test token then does not take down the production bot, and you can test changes without touching the bot that is live.
Use the same variable name (DISCORD_TOKEN) in both environments and change only the value. The code does not need to know which is which. To take the bot to a host, follow the guide on how to host a discord.js bot.
How to do this on Square Cloud#
On Square Cloud the token does not need to sit in any project file. Environment variables are set per application, in the dashboard, through the API, the CLI or the VS Code extension. The dashboard also imports a .env and exports the set as KEY=VALUE.
Create a DISCORD_TOKEN variable with the value from the Portal and keep .env out of the ZIP and the repository. The change takes effect on the next restart, with no rebuild. If you need to swap the token after a leak, edit the value and restart.
Bot hosting supports discord.js, discord.py, JDA, Discord.Net or any Gateway library, and automatic restart (on by default for dashboard deploys) brings the bot back when the process exits with an error. You do the deploy and the configuration yourself. See the details on Discord bot hosting.
Bot hosting
Keep your bot online 24/7 with automatic restarts
FAQs
Where is the Discord bot token?
In the Developer Portal, open Applications, pick your app and go to the Bot tab. The token appears when you click Reset Token and confirm.
I lost my token. Can I see it again?
No. An old token cannot be read back. Click Reset Token to generate a new one and update the value everywhere the bot uses it.
What happens if I push my token to a public repository?
Anyone who copies it controls your bot. Reset the token in the Portal right away; deleting the commit afterwards is not enough, because the old value may already be copied.
Do I need a separate token for each bot?
Yes. Each Portal application has its own bot and token. Use one application for development and another for the production bot.
More in Bots
See all 33 articles- How much does it cost to host a Discord bot?The cost of hosting a Discord bot depends on RAM, uptime, database and outside services. Learn how to estimate your own bill and where the money goes.
- Running a bot on your phone or PC vs hosting itA bot on your PC or phone (Termux) goes offline when the device shuts down, sleeps or loses its network. Compare the three options and when each one fits.
- How to host a Discord bot 24/7To keep a Discord bot online 24/7, run its process on an always-on machine with automatic restarts. Compare your PC, a VPS with pm2 or systemd, and bot hosting.
- Why your Discord bot keeps going offline (and how to fix it)A Discord bot goes offline when the process dies, the token is invalid, an intent is disabled or memory runs out. Learn to diagnose each cause and fix it.