Fix discord.js TokenInvalid: "An invalid token was provided"

Bot-Hosting team,

Your bot refuses to log in and the console shows:

Error [TokenInvalid]: An invalid token was provided.

Or, in Python:

discord.errors.LoginFailure: Improper token has been passed.

This TokenInvalid error means Discord did not accept the string you passed to client.login() or bot.run(). In nine cases out of ten the token itself is fine and the code never received it. Go through the causes in this order.

1. The environment variable is empty

The most common cause by far. Your code reads process.env.TOKEN, but the variable is called DISCORD_TOKEN, or it is not set at all where the bot runs. discord.js then receives undefined and throws TokenInvalid before even contacting Discord.

Check it in one line before logging in:

console.log('Token loaded:', Boolean(process.env.DISCORD_TOKEN));
client.login(process.env.DISCORD_TOKEN);
import os
print("Token loaded:", bool(os.environ.get("DISCORD_TOKEN")))
bot.run(os.environ["DISCORD_TOKEN"])

If it prints false:

  • Locally with a .env file: a .env file is not read on its own. Install dotenv and add require('dotenv').config() at the very top of your entry file, or from dotenv import load_dotenv; load_dotenv() in Python.
  • On a host: the .env file is usually ignored by git and never uploaded. Set the variable in the host’s settings instead. On Bot-Hosting, that is the deployment’s Environment tab: Add variable, key DISCORD_TOKEN, tick Secret. Saving restarts the bot with the value.
  • Typo in the name: DISCORD_TOKEN and DISCORD_TOKEN (trailing space) are two different keys.

2. You copied the wrong secret

The Developer Portal shows several secrets that look alike:

WhereWhat it isWorks with login()?
Bot > TokenThe bot tokenYes
OAuth2 > Client SecretFor OAuth web loginsNo
General Information > Public KeyFor interaction webhooksNo
General Information > Application IDA public numberNo

Only the token from the Bot page logs a bot in. It is long, has three parts separated by dots, and is shown once when you click Reset Token.

3. Discord reset your token

If the bot worked yesterday and fails today without any change, Discord probably reset the token. It does so automatically when it finds the token somewhere public, most often:

  • a public GitHub repository, including an old commit or a config.json;
  • a paste site, a public gist or a screenshot.

Discord usually sends a DM from the System account when it does. Click Reset Token in the portal, put the new one in your environment variable, and remove the old one from your repository’s history. Then make sure it can never happen again:

# .gitignore
.env
config.json

4. Quotes, spaces or a “Bot” prefix

Values pasted into an environment variable are taken literally:

  • "MTE2..." with quotes: the quotes become part of the token;
  • a trailing space or newline from the clipboard;
  • Bot MTE2...: the Bot prefix is added by the library, not by you.

Paste the raw token, nothing around it.

5. You are calling the API with a user token

Logging in with a token taken from your own Discord account (a “self-bot”) is against Discord’s Terms of Service, and libraries refuse it. Bots need a bot token from an application.

6. The token is fine, the request is not

If you see 401: Unauthorized on REST calls rather than at login, for example in a separate script that registers slash commands, that script is reading a different or empty token: apply cause 1 to it. If you get 403 Missing Access while registering commands, the token and the application ID belong to two different applications, typically a test bot’s token with the production bot’s ID.

Let the host spot TokenInvalid for you

On Bot-Hosting, the Console recognises this error the moment it is printed and says “Discord refused the bot token.” with a banner. Click Ask AI: the assistant reads your entry file and your logs, and tells you which of the six causes it is.

Next step: keep the bot online

Once it logs in, keep it running when your computer is off, on a free plan renewed in one click every 4 days. Follow how to host a Discord bot 24/7 for free, or deploy straight from your repository with the GitHub guide.

Deploy your bot for free Browse bot templates