TSocial TSocial
Open in Telegram
Better in TelegramOpen in Telegram

Keep Your Telegram Bot Token Safe (and What to Do if It Leaks)

· 4 min read · 18+ readers

Keep Your Telegram Bot Token Safe (and What to Do if It Leaks): TSocial guide cover card

The token bot icon BotFather gives you is the only thing needed to control your bot. Whoever holds it can read the bot's updates, send icon (paper plane) send messages to everyone who ever used it and change its settings that the API allows. Treat it like a password. This guide covers where tokens leak, how to store them, how to rotate them and what to do if one is exposed.

What a token looks like

123456789:AAHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

A number, a colon and a long string. The number is the bot's id, which is public. The part after the colon is the secret.

Mock BotFather chat showing the /newbot command, a bot name, a username ending in bot and a masked token
Creating a bot with /newbot in BotFather. Generic illustration, not a real chat or user data.

Where tokens leak

  • Source code in public repositories. Automated scanners find tokens within minutes.
  • Screenshots and screen sharing of a terminal, an editor or BotFather.
  • Chat messages and support tickets where you pasted the token.
  • Logs that print request URLs. Remember that Bot code icon API URLs contain the token.
  • Front-end code. A Mini App icon (grid) Mini App page must never contain the bot token.
  • Shared documents and issue trackers.
  • Contractors and old team members who still have access.
  • Browser extensions or random tools that ask for your token.

Store it properly

  1. Put it in an environment variable or a secrets manager of your host. Never in source code.
  2. Keep .env files out of version control by listing them in .gitignore.
  3. Use separate bots for development and production, with separate tokens.
  4. Limit who can read production secrets, and write down who has them.
  5. If your platform has a secret store, use it instead of plain environment files.

Example loading in Node:

const token = process.env.BOT_TOKEN;
if (!token) throw new Error("BOT_TOKEN is not set");

Scrub tokens from logs. If you log URLs, replace the token with ***.

Webhook protection is separate

The webhook secret token is a different secret that protects your server from fake updates. Use both. See webhook setup.

Rotate on a schedule and on any doubt

In BotFather, send /revoke, choose the bot and receive a new token. The old one stops working immediately. Update your deployment. Consider doing this when:

  • A team member leaves.
  • You moved hosting.
  • You are unsure whether it was exposed.
  • A token appeared in a repository, even briefly.

Plan a short downtime, since the bot will fail until the new token is deployed. With a webhook you may need to run setWebhook again.

Signs of misuse

  • Messages you did not send.
  • Webhook URL changed. Check getWebhookInfo.
  • Unknown commands or description changes.
  • 409 Conflict errors when polling: someone else may be calling getUpdates.
  • Complaints from users about strange messages.

Leak response checklist

  1. Revoke the token in BotFather immediately.
  2. Deploy the new token.
  3. Check getWebhookInfo and reset the webhook and secret.
  4. Review recent bot activity and logs.
  5. Find the source of the leak and remove it: delete the commit history if needed, rotate other secrets that were in the same place.
  6. Warn users if messages were sent in your bot's name. Be honest and brief.
  7. Add a scanner or a pre-commit check to prevent a repeat.

Removing a secret from a public repository does not make it safety icon safe again. Assume it was copied.

Team and vendor practice

  • Give vendors a development bot, not the production one.
  • If a vendor must run your bot, be the owner in BotFather so that you can revoke access. See what to ask a vendor.
  • Never share your own Telegram login code or two-step password. A bot token is not your account password, but your account owns the bot.
  • Use two-step verification on your Telegram account, since the account controls BotFather.

Quick checklist

  • Token stored in a secret manager or environment variable.
  • .env files ignored by version control.
  • Separate bots for development and production.
  • Logs scrub URLs that contain the token.
  • Token never appears in front end code or screenshots.
  • /revoke practised so the team knows the steps.
  • Webhook secret kept separate from the token.
  • Access reviewed whenever a team member leaves.

Automate detection

Add a secret scanner to your repository and to your continuous integration, so that a token pasted into code is caught before it is public. Many hosting providers and code platforms also scan for leaked tokens. Treat any alert as a real leak and rotate the token at once.

Frequently asked questions

Is the bot id secret?

No. The number before the colon is public.

Can I check who used my token?

Telegram does not provide a usage log. Watch your own logs and bot behaviour.

Does revoking break my users' chats?

No. Chats continue. Only the old token stops working.

Can I put the token in a Mini App?

No. Mini Apps run in the user's browser. Keep the token on your server.

Related guides

More in Build on Telegram

All topics: Build on Telegram · Guide index · Explore Telegram · About TSocial

Keep Your Telegram Bot Token Safe (and What to Do if It Leaks) | TSocial