Host a Telegram bot on a VPS so it stays online day and night

A Telegram bot needs a computer that never sleeps and has internet access, and little else. With long polling a small VPS is enough and no incoming port has to be opened for the bot; a webhook also needs a domain, HTTPS and an open port.

Updated: · Prices in the text are with the public IP (₼10 a month) unless stated otherwise.

Polling or webhook: decide this first

Long polling (getUpdates)Webhook
Who opens the connectionYour botTelegram's servers
Incoming connectionsNone for the botTelegram must reach your server over IPv4; IPv6 is not supported
Domain and certificateNot neededHTTPS with TLS 1.2 or newer
Ports Telegram acceptsNone to open443, 80, 88 and 8443 only
Best forAlmost every new botBusy bots and serverless-style apps

Polling is not a beginner's shortcut. Telegram holds the request open and answers as soon as a message arrives, so users do not notice a difference. Move to a webhook when the bot is part of a web application that already has HTTPS, or when you want several workers behind a load balancer. A bot cannot use both at once: while a webhook is set, getUpdates returns an error.

The smallest plan that is enough

A text bot in Python or Node.js usually fits in 100–200 MB of memory. Treat these as starting points:

  • Nano (1 vCPU, 1 GB RAM, 30 GB NVMe): one or a few polling bots with SQLite. ₼24.90 a month.
  • Starter (1 vCPU, 2 GB RAM, 30 GB NVMe): several bots, PostgreSQL or Redis beside them, or a webhook behind nginx. ₼29.90 a month.
  • Basic (2 vCPU, 2 GB RAM, 40 GB NVMe) or larger: bots that convert video, process images or run a headless browser. There the media work decides, not Telegram. Basic is ₼37.90 a month.
  • Nano₼14.90/mo

    1 vCPU · 1 GB RAM · 30 GB NVMe

    Public IP +₼10/mo, With public IP: ₼24.90/mo

  • Starter₼19.90/mo

    1 vCPU · 2 GB RAM · 30 GB NVMe

    Public IP +₼10/mo, With public IP: ₼29.90/mo

Prices in manat (AZN), billed monthly. The public IP is +₼10 a month and is assigned automatically.

The prices in the list are with public IP (₼10 a month of each), so the bot can reach Telegram, the server can download updates and you can open SSH from outside. A polling bot needs no other port, so leave everything except SSH closed in the firewall. If two or three small bots share one server, they also share its price; the page on cheap VPS plans breaks the bill down.

Deploy a polling bot step by step

The example uses Python with aiogram on Ubuntu 24.04 or Debian 12. First a system user, a code folder and a virtual environment:

sudo apt update && sudo apt install -y python3-venv
sudo adduser --system --group --home /opt/mybot mybot
sudo python3 -m venv /opt/mybot/venv
sudo /opt/mybot/venv/bin/pip install aiogram

Save this as /opt/mybot/bot.py:

import asyncio, os
from aiogram import Bot, Dispatcher
from aiogram.filters import CommandStart
from aiogram.types import Message

dp = Dispatcher()

@dp.message(CommandStart())
async def start(message: Message):
    await message.answer("The bot is running.")

async def main():
    bot = Bot(os.environ["BOT_TOKEN"])
    await dp.start_polling(bot)

asyncio.run(main())

Then /etc/systemd/system/mybot.service:

[Unit]
Description=Telegram bot
After=network-online.target
Wants=network-online.target

[Service]
User=mybot
WorkingDirectory=/opt/mybot
EnvironmentFile=/etc/mybot.env
ExecStart=/opt/mybot/venv/bin/python bot.py
Restart=always
RestartSec=5
MemoryMax=400M
StateDirectory=mybot
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now mybot
journalctl -u mybot -f

Restart=always brings the bot back after a crash and enable starts it after a reboot. The code folder is read-only for the bot; databases and downloads belong in /var/lib/mybot, which StateDirectory creates. The same unit works for Node.js with a different ExecStart; the Node.js page covers the details, and the Python page covers workers and queues.

Keep the token out of the code

Anyone with the token controls the bot. Create /etc/mybot.env containing one line, BOT_TOKEN=..., then:

sudo chown root:root /etc/mybot.env && sudo chmod 600 /etc/mybot.env

systemd reads the file as root, so even the bot's own user cannot open it. Do not commit the token, do not paste it into chats, and do not print full request URLs in logs, because the token is part of the Bot API address. If it leaks, revoke it in BotFather at once and restart the service with the new one.

If you need a webhook

Run the bot's HTTP listener on 127.0.0.1:8080, put nginx with a valid certificate in front (how to get one), and register the address. The secret_token comes back in the X-Telegram-Bot-Api-Secret-Token header of every request; reject requests without it.

# run in a root shell so the token is read from the file
set -a; . /etc/mybot.env; set +a
curl -F "url=https://bot.example.com/tg" -F "secret_token=$WEBHOOK_SECRET" \
  "https://api.telegram.org/bot$BOT_TOKEN/setWebhook"
curl "https://api.telegram.org/bot$BOT_TOKEN/getWebhookInfo"

Add WEBHOOK_SECRET to the same env file first. getWebhookInfo shows the last delivery error, which is the fastest way to find a certificate or firewall problem.

Symptoms and causes

  • 409 Conflict: two copies use the same token. Stop the one on your laptop or delete the webhook.
  • Messages missed after downtime: Telegram keeps undelivered updates for 24 hours at most.
  • 429 Too Many Requests: wait the number of seconds in retry_after. Send broadcasts slowly, at most about 30 messages a second in total.
  • A file will not download: the cloud Bot API lets bots download files up to 20 MB and upload up to 50 MB.
  • Bot silent, service active: run curl -sI https://api.telegram.org on the server to separate a network problem from a code problem.

Telegram bot hosting questions

Does a polling bot need a public IP address?

Polling needs no open port and no address that Telegram can reach; the server only has to open outgoing connections. An ordinary Cloudraft VPS has a public IPv4 anyway, for ₼10 a month on top of the server price, so you can switch to a webhook later without changing the server. A webhook additionally needs a name and HTTPS on port 443, 80, 88 or 8443.

Can I host several bots on one VPS?

Yes. Give each bot its own user, env file and systemd unit so that one crash or leaked token does not affect the others.

Why not run the bot on my own computer?

You can while developing. In production sleep mode, power cuts and home internet drop the bot, and the 24-hour update queue does not cover longer gaps.

Can a bot use Telegram user accounts?

That is a different interface with different rules and a real risk of account limits. This page covers bots created through BotFather with the official Bot API.

Does Cloudraft write or maintain the bot?

A VPS is self-managed, so the code and updates are yours. Support helps with the server by phone, WhatsApp, email and tickets in Azerbaijani, Russian and English.

Read next

All VPS pages

This page in other languages: Azərbaycanca · Русский

Servers in Baku, prices in manat

Create an account, top up your balance and order the plan you need.