Updated 30 November 2026
Your bot token, API keys and database passwords belong in environment variables, not in your code. On a ServerSpark Discord bot hosting plan you keep them in a .env file on the server, which your bot reads when it starts. This guide shows how for discord.js (Node.js) and discord.py (Python).
Why not put the token in your code?
- Code ends up in places you don't control: a public GitHub repository, a help channel, a screenshot. Anyone with your token controls your bot.
- With a
.envfile you can share or publish your code without secrets, and use another token for a test bot. - When you deploy from GitHub, the
.envfile stays on the server while new code is pulled.
1. Make the .env file on the server
In SparkPanel, open Files. New bot servers already have a .env file from the starter bot; otherwise create one. Put one variable per line, without quotes or spaces around the =:
DISCORD_TOKEN=your-bot-token
GUILD_ID=123456789012345678
OPENAI_API_KEY=your-key
DATABASE_URL=sqlite:///data/bot.db
Save the file and restart your bot. Files starting with a dot can be hidden in some SFTP programs; SparkPanel shows them.
2. Read the variables in your code
Node.js (discord.js): add dotenv to your package.json, then at the very top of your main file:
require('dotenv').config();
const token = process.env.DISCORD_TOKEN;
With ES modules use import 'dotenv/config';. Node.js 22 and newer can also load the file itself when you add --env-file=.env under Extra start options in Settings, Bot settings.
Python (discord.py): add python-dotenv to your requirements.txt, then:
import os
from dotenv import load_dotenv
load_dotenv()
token = os.getenv("DISCORD_TOKEN")
Packages in package.json or requirements.txt are installed automatically when your bot starts.
3. Keep .env out of Git
- Add a line
.envto your.gitignorebefore your first commit. - Commit a
.env.examplewith the variable names and no values, so you (and others) know what the bot needs. - Already committed a token? Reset it in the Discord Developer Portal; see Discord bot token leaked?
Coming from Replit, Railway, Render or Heroku?
Those hosts keep secrets in their dashboard (Replit Secrets, Railway and Render variables, Heroku config vars) and give them to your bot as environment variables. Your code that reads process.env.X or os.getenv("X") keeps working: copy each name and value into .env on the server and load it as above. More about moving: Replit, Render or Railway for a Discord bot?
Common mistakes
- The token is undefined or None:
dotenvis loaded after the code that reads the variable, the file is not called exactly.env, or it is in another folder than the one your bot starts in. - An invalid token error: quotes or a space around the value, or an old token. Copy it again from the Developer Portal.
- The token is printed in the console: never log your variables. Log only that they are set.
Frequently asked questions
Where do I put my Discord bot token on ServerSpark? In the .env file under Files in SparkPanel, as DISCORD_TOKEN=your-token, and read it with dotenv (Node.js) or python-dotenv (Python).
Is the .env file on the server safe? It is only reachable through your SparkPanel login and SFTP. It is not served on your bot's web address, as long as your own web code does not serve the bot folder as static files.
Do I need to restart after changing .env? Yes. Variables are read when the bot starts.