Discord · Token

Discord bot token invalid

Discord did not accept the string your bot logged in with. Either it is not the bot token, it never reached your code, or Discord has since replaced it.

← All errors

What you are seeing

discord.js

Error [TokenInvalid]: An invalid token was provided.

discord.py

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

Gateway

WebSocket closed with code 4004: Authentication failed

Check it is the right value

The Developer Portal shows several long strings, and only one is the bot token: it is on the Bot page, behind Reset Token. The Application ID and Public Key on General Information, and the Client Secret under OAuth2, are not tokens and will always be rejected.

Discord only shows a token once. If you did not copy it at the time, reset it and copy the new one — which also invalidates the old one everywhere it was used.

Check it reached your code

In discord.js, an empty or missing token gives the same "invalid token" error as a wrong one. If you read the token from an environment variable, the most common problem is that the variable is not set where the bot actually runs: a .env file on your computer does nothing on a server unless you upload it and load it, and a variable set in your hosting panel only exists after a restart.

Quotes and spaces cause the rest. A token pasted as "abc" with the quotation marks, or with a trailing space or newline, is not the token.

Node.js

// Prints whether the token arrived, without printing the token itself.
const token = process.env.DISCORD_TOKEN;
console.log('token length:', token ? token.trim().length : 'missing');

client.login(token?.trim());

Python

import os

token = os.environ.get("DISCORD_TOKEN", "").strip()
print("token length:", len(token) or "missing")

client.run(token)

Check Discord has not reset it

Discord resets a bot's token when it finds it somewhere public, such as a GitHub repository, and messages the owner through the Discord system account. It also resets it if the bot logs in too many times in a day. If your token suddenly stopped working without you touching it, check your DMs from Discord, reset the token, and make sure the new one only ever lives in an environment variable.

FAQ

Questions

Does the token need "Bot " in front of it?

Not when you pass it to discord.js or discord.py; the library adds that itself. Only raw HTTP requests to the API need the "Bot " prefix in the Authorization header.

How many logins is too many?

Discord allows a bot 1000 gateway logins in 24 hours. A bot that crashes on startup and is restarted in a loop can burn through that, after which Discord resets the token. Fix the crash first; the console shows it.