Telegram Bots

Using environment variables

CanerAkar Mar 17, 2026

Environment variables let you store your bot token, API keys, and configuration values outside of your code. On FPS.ms, you do this with a .env file. This keeps secrets secure and makes it easy to change settings without editing your bot's source files.

Why use environment variables?

ApproachSecurityConvenience
Hardcoded in codeAnyone who sees your code sees your tokenMust edit code to change values
**.env file****Not part of your code****Edit one file to change values**
Never hardcode your token
If your bot token is in your code and you push to a public GitHub repo, anyone can take control of your bot. Always use a .env file.

Creating a .env file on FPS.ms

  1. Go to panel.fps.ms and select your Telegram bot server
  2. Open the Files tab
  3. Create a new file named .env in the root directory
  4. Add your variables in KEY=value format, one per line:
.env
BOT_TOKEN=7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
BOT_NAME=MyCoolBot
ADMIN_CHAT_ID=123456789
  1. Save the file and restart your server for the changes to take effect
No spaces around the equals sign
Write BOT_TOKEN=yourtoken, not BOT_TOKEN = yourtoken. Extra spaces can cause the value to be read incorrectly.
Keep .env out of Git
If you use a GitHub repository to deploy your bot, add .env to your .gitignore file so your token is never pushed to GitHub. Create the .env file directly on the FPS.ms panel instead.

Reading environment variables in your code

Python

Python needs the python-dotenv package to load .env files. Add it to your requirements.txt:
requirements.txt
python-telegram-bot
python-dotenv
Then load the .env file at the top of your app.py:
app.py
import os
from dotenv import load_dotenv
from telegram.ext import ApplicationBuilder

load_dotenv()

TOKEN = os.environ['BOT_TOKEN']
ADMIN_ID = os.environ.get('ADMIN_CHAT_ID')  # returns None if not set

app = ApplicationBuilder().token(TOKEN).build()
MethodBehavior
`os.environ['BOT_TOKEN']`Raises `KeyError` if not set — use for required values
`os.environ.get('ADMIN_CHAT_ID')`Returns `None` if not set — use for optional values

Node.js

Node.js needs the dotenv package to load .env files. Add it to your package.json:
package.json (dependencies section)
{
    "dependencies": {
        "telegraf": "latest",
        "dotenv": "latest"
    }
}
Then load the .env file at the top of your index.js:
index.js
require('dotenv').config();
const { Telegraf } = require('telegraf');

const TOKEN = process.env.BOT_TOKEN;
const ADMIN_ID = process.env.ADMIN_CHAT_ID;

const bot = new Telegraf(TOKEN);

Common environment variables for Telegram bots

VariableExample valuePurpose
`BOT_TOKEN``7123456789:AAH...`Your Telegram bot token (required)
`ADMIN_CHAT_ID``123456789`Your Telegram user ID for admin commands
`GROUP_CHAT_ID``-100123456789`A group chat ID for notifications
`DATABASE_URL``sqlite:///bot.db`Database connection string
`LOG_LEVEL``INFO`Logging verbosity
Finding your chat ID
To find your Telegram user ID, send a message to @userinfobot on Telegram. It will reply with your numeric ID.

Migrating from hardcoded tokens

Python — before and after

Before (insecure)
app = ApplicationBuilder().token('7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw').build()
After (secure)
import os
from dotenv import load_dotenv
load_dotenv()
app = ApplicationBuilder().token(os.environ['BOT_TOKEN']).build()
Don't forget to add python-dotenv to your requirements.txt.

Node.js — before and after

Before (insecure)
const bot = new Telegraf('7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw');
After (secure)
require('dotenv').config();
const bot = new Telegraf(process.env.BOT_TOKEN);

Troubleshooting

ProblemCauseFix
`KeyError: 'BOT_TOKEN'` (Python)`.env` file missing or `load_dotenv()` not calledCreate `.env` and add `load_dotenv()` before accessing variables
`Unauthorized`Token value is wrongCopy the token again from [BotFather](/telegram-bots/creating-your-bot-with-botfather/)
`ModuleNotFoundError: dotenv``python-dotenv` not installedAdd `python-dotenv` to `requirements.txt`
`Cannot find module 'dotenv'``dotenv` not in dependenciesAdd `dotenv` to `package.json` dependencies
Changes not taking effectServer not restartedRestart your server after editing `.env`

Next steps

Was this article helpful?