---
title: "Telegram Bot Environment Variables - Secure Config | FPS.ms"
description: "Store your Telegram bot token and config values securely using a .env file on FPS.ms. Never hardcode secrets in your bot code."
url: https://fps.ms/docs/telegram-bots/using-environment-variables
---
# Using environment variables

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?

| Approach | Security | Convenience |
|----------|----------|-------------|
| Hardcoded in code | Anyone who sees your code sees your token | Must edit code to change values |
| **.env file** | **Not part of your code** | **Edit one file to change values** |

!!! danger "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](https://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 title=".env"
BOT_TOKEN=7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
BOT_NAME=MyCoolBot
ADMIN_CHAT_ID=123456789
```

5. Save the file and **restart your server** for the changes to take effect

!!! warning "No spaces around the equals sign" "Write `BOT_TOKEN=yourtoken`, not `BOT_TOKEN = yourtoken`. Extra spaces can cause the value to be read incorrectly."

!!! tip "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`:

```txt title="requirements.txt"
python-telegram-bot
python-dotenv
```

Then load the `.env` file at the top of your `app.py`:

```py title="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()
```

| Method | Behavior |
|--------|----------|
| `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`:

```json title="package.json (dependencies section)"
{
    "dependencies": {
        "telegraf": "latest",
        "dotenv": "latest"
    }
}
```

Then load the `.env` file at the top of your `index.js`:

```js title="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

| Variable | Example value | Purpose |
|----------|---------------|---------|
| `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 |

!!! tip "Finding your chat ID" "To find your Telegram user ID, send a message to [@userinfobot](https://t.me/userinfobot) on Telegram. It will reply with your numeric ID."

## Migrating from hardcoded tokens

### Python — before and after

```py title="Before (insecure)"
app = ApplicationBuilder().token('7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw').build()
```

```py title="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

```js title="Before (insecure)"
const bot = new Telegraf('7123456789:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw');
```

```js title="After (secure)"
require('dotenv').config();
const bot = new Telegraf(process.env.BOT_TOKEN);
```

## Troubleshooting

| Problem | Cause | Fix |
|---------|-------|-----|
| `KeyError: 'BOT_TOKEN'` (Python) | `.env` file missing or `load_dotenv()` not called | Create `.env` and add `load_dotenv()` before accessing variables |
| `Unauthorized` | Token value is wrong | Copy the token again from [BotFather](https://fps.ms/docs/telegram-bots/creating-your-bot-with-botfather.md) |
| `ModuleNotFoundError: dotenv` | `python-dotenv` not installed | Add `python-dotenv` to `requirements.txt` |
| `Cannot find module 'dotenv'` | `dotenv` not in dependencies | Add `dotenv` to `package.json` dependencies |
| Changes not taking effect | Server not restarted | Restart your server after editing `.env` |

## Next steps

- [Install your Telegram bot](https://fps.ms/docs/telegram-bots/installation.md) — full setup guide for Python and Node.js
- [Create your bot with BotFather](https://fps.ms/docs/telegram-bots/creating-your-bot-with-botfather.md) — generate a token
- [Edit your code](https://fps.ms/docs/telegram-bots/editing-code.md) — update files via the panel, SFTP, or VS Code
- [Add commands](https://fps.ms/docs/telegram-bots/adding-commands.md) — handle user commands

---

[FPS.ms](https://fps.ms/index.html.md) · [All pages](https://fps.ms/llms.txt) · [Documentation](https://fps.ms/docs/llms.txt)
