Keep your bot token safe

With your bot's token, anyone can control your bot: write messages in its name, delete channels, ban members. That is why it never belongs in the code. Here is how to keep it properly and what to do if it ever becomes public.

Updated 5 minute read

The rule

A token is never in the code. Not in index.js, not in config.json, not in a comment and not in a commit you delete later. It comes from the environment your bot runs in:

const token = process.env.DISCORD_TOKEN;
token = os.environ["DISCORD_TOKEN"]

That way the code is the same on your PC, on EStage and everywhere else, and the token lives only where it is needed.

On your PC: the .env file

So you do not have to type the token on every start, put it in a .env file next to your code:

DISCORD_TOKEN=your-token

And make sure it never ends up in the repo:

# .gitignore
.env

Node.js reads the file itself since version 20.6:

node --env-file=.env index.js

In Python the package python-dotenv helps, with load_dotenv() at the start of your program.

Also put a .env.example in the repo, with all the names and no values. That way everyone working on the bot knows which variables are needed.

On the server: secrets

On EStage you enter tokens in the dashboard as secrets. They are stored encrypted, and your service receives them as environment variables when it starts. They do not show up in the repo, the build or the logs, as long as your code does not print them.

That leads to a second rule: never print tokens. No console.log(process.env), no print(os.environ) for debugging, no error message that contains the token. Logs get read out on stream quickly.

Two bots instead of one

Create a second bot in the Developer Portal for development, with its own token, and invite it to a test server. Your main bot runs only on EStage, the test bot only on your PC. If something goes wrong on your PC, only the test bot is affected.

If a token became public

It happens: a screenshot on stream, a commit with the wrong file, a paste into a help channel. Then every minute counts.

  1. Generate a new token at once. On Discord in the Developer Portal under your application, in the Bot section, with Reset Token. On Twitch you create a new client secret in the developer console. The old value is worthless from then on.
  2. Enter the new token, on EStage as a secret, and restart the service.
  3. Check what happened. Look at your Discord server’s audit log to see whether the bot did anything in the meantime that was not you.
  4. Fix the cause. .env in .gitignore, token out of the code, logs without secrets.

Deleting the token from the repo is not enough. It stays in the Git history, and automated scanners search public repos for tokens within minutes.

In short

  • Read the token from the environment, never from the code.
  • .env locally, in .gitignore.
  • On the server as a secret, not as a file in the repo.
  • Never print it, never in logs.
  • After a leak: regenerate first, clean up second.

Common questions

Is it enough to delete the token from the repo?

No. It is still in the Git history, and anyone who cloned or searched the repo before knows it. A token that was public once has to be regenerated.

Is a private repo safe enough?

Safer than a public one, but not safe. Everyone with access to the repo, every tool with read access and every future share sees the token. Tokens do not belong in the code of private repos either.

Where do I keep tokens on EStage?

As a secret in the dashboard. Secrets are stored encrypted and reach your service only as environment variables, not through the repo and not in the build log.

Ready when you are

EStage launches in early 2027. Tell us what you want to run, and we will set up the first channels together.

Early access See pricing