ESN Docs

Docs › Discord bots

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

1 min readUpdated Nov 3, 2026

Why your Discord bot fails with LoginFailure: Improper token has been passed (or TokenInvalid in discord.js), and how to get, store and load the right token.

The error:

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

Discord rejected the token your bot sent (HTTP 401). In discord.js v14 it's Error [TokenInvalid]: An invalid token was provided.; in older versions TOKEN_INVALID.

Why it happens #

  • Wrong value: the Client Secret (OAuth2 page) or the Application ID was pasted instead of the bot token.
  • The token was reset. Resetting it in the portal makes the old one stop working. Discord also resets a token on its own when it finds it posted publicly (for example in a public GitHub repo), and after a bot connects too many times in a day (see rate limits).
  • Extra characters: quotes, a space, a line break, or the word Bot in front. discord.py adds the Bot prefix itself.
  • Old .env: you updated the token on your computer but not on the server (or the other way round).

1. Get a fresh token #

  1. Open the Developer Portal, pick your app and go to Bot.
  2. Choose Reset Token, confirm, and copy it. It's shown only once.

2. Store it correctly #

In your .env file, one line, no quotes, no spaces:

DISCORD_TOKEN=MTIzNDU2Nzg5...your-real-token...

On ESN Hosting, edit .env in your server's top folder in Files. Its name must be exactly .env, not .env.txt.

3. Check your code loads it #

import os

from dotenv import load_dotenv

load_dotenv()                      # must run before os.getenv
TOKEN = os.getenv("DISCORD_TOKEN")
print("Token loaded:", bool(TOKEN), "length:", len(TOKEN or ""))  # never print the token itself

bot.run(TOKEN)

If it prints Token loaded: False, the problem is the file (wrong name, wrong folder, or python-dotenv isn't installed), not the token. Passing an empty token gives a different error in discord.py 2: TypeError: expected token to be a str, received NoneType instead.

4. Restart and confirm #

Start the bot and look for your "Logged in as" line. Then make sure only one copy runs with this token.

Keep the token private. Never commit .env: add it to .gitignore. If you think it leaked, reset it straight away.

FAQ #

Is the token the same as the client secret?

No. The client secret is for OAuth2 logins on websites. The bot token is on the Bot page and starts the bot.

My token keeps getting reset by itself.

Discord resets tokens it finds in public places, like a public GitHub repository or a paste site, and after a bot hits its daily connection limit. Make the repo private, remove the token from its history, reset it, and fix any crash-restart loop.

It works on my PC but not on the host.

The server has its own .env. Make sure the same, current token is in the .env in the server's top folder and that python-dotenv is in requirements.txt.

Want a host that shows you these errors live? ESN bot servers have a live console, crash logs and Python 3.12/3.13 or Node.js 22/24. Bot hosting from $0.50/month. See bot plans · or paste your error into /botfix in the ESN Systems Discord.

Still stuck? Use the live chat on the panel or ask in the ESN Systems Discord. Last updated November 3, 2026.