How to fix "Invalid token" errors on your Discord bot

If your bot refuses to log in and the console says the token is invalid, improper or unauthorised, the problem is almost always small: a copy mistake, an old token or a variable that is not set. Because a failing login can also trigger blocks if the bot keeps retrying, it is worth fixing methodically.

What the messages look like

LibraryTypical message
discord.jsAn invalid token was provided or TokenInvalid
discord.pyLoginFailure: Improper token has been passed
Raw API401 Unauthorized

They all mean Discord rejected the token you sent.

Check these causes in order

1. The token variable is empty or missing. If your code reads the token from an environment variable and the variable is not set, the library receives nothing, and complains about an invalid token. Print whether it exists, without printing the value itself, for example by checking its length. Then look at where it is set. See Environment variables and keeping your bot token safe.

2. The name does not match. The variable in your hosting panel must have exactly the name the code uses, including capitals. TOKEN and token are different.

3. Extra characters. A space, a line break or quote marks copied with the token will break it. A valid token is a single line with no spaces. Re-paste it carefully, and check for a hidden space at the end.

4. You pasted the wrong thing. The Developer Portal shows several long values: the application ID, the public key, the client secret and the bot token. Only the bot token works. The token is shown on the Bot tab after you press Reset Token.

5. The token was reset. Resetting a token immediately invalidates the old one. If anyone reset it, or Discord did, any program still using the old value fails. Create a fresh one and update your settings. Discord resets tokens it finds posted on public sites. See How to create a Discord bot and keep its token safe.

6. The application or bot was deleted. If the application no longer exists, no token for it works.

7. A bot token versus a user token. Never use a normal account's token. It is against Discord's rules, and it will not work as a bot.

How to test the token safely

Without sharing it, you can check it from a terminal on your own computer, using Discord's API:

curl -s -o /dev/null -w "%{http_code}" -H "Authorization: Bot YOUR_TOKEN" https://discord.com/api/v10/users/@me

A result of 200 means the token is valid. 401 means it is not. Do this only on your own machine, never in a shared place, and clear your terminal history afterwards if you pasted a real token.

Stop the retry loop

A bot that fails to log in and then restarts automatically will keep trying, over and over. Each failed login counts as an invalid request, and too many in a short time can get the address your bot connects from blocked, which can affect other bots on the same host. It also uses up Discord's daily limit on new sessions.

So if a token problem happens:

  1. Stop the bot. Do not let it keep restarting.
  2. Fix the token.
  3. Start it once, and watch.

How to handle Discord rate limits explains the limits in detail, and Keep your bot online describes how restart behaviour works on your server.

Do not leak the token while debugging

When you ask for help, do not post the token, and be careful with screenshots of your settings or console. If you pasted it anywhere public, treat it as leaked and reset it right away. Secure your bot and its permissions covers what to check afterwards.

Checklist

  1. The variable is set and has the right name.
  2. The value is the bot token from the Bot tab.
  3. No spaces, quotes or line breaks.
  4. It has not been reset since you copied it.
  5. The curl test returns 200.
  6. The bot restarted after you saved the change.

If the token is right and the bot still fails, the next suspects are intents, covered in How to fix the "Used disallowed intents" error, or a library version that is out of date. The first line of the console tells you which, as shown in How to read your server logs.


Still stuck? Open a ticket on Discord - run /ticket in any channel, pick a department, and include your Server ID or Invoice ID so we can jump straight in.