telegram-sync-cli
Agent-friendly Telegram CLI with a local cache, a read-only sync daemon and full MTProto API access.
Website: tg.sergeybaus.com. Made by Sergey Baus.

Install
With Homebrew on macOS or Linux, a standalone binary:
brew install neoromantic/tap/tg
With Bun >= 1.4:
bun i -g @goodit/telegram-sync-cli
The npm package runs the TypeScript source with Bun; nothing is compiled at install time.
npm i -g @goodit/telegram-sync-cli also works, as long as bun is on your PATH. Binaries
for every platform, Windows included, are attached to each
GitHub Release.
The agent skill for Claude Code, Codex and other coding agents:
bunx skills add neoromantic/telegram-sync-cli
tg skill install --dir and the tg skill validate that the skill describes need tg newer than
0.14.0. In 0.14.0 and older, tg skill install ignores --dir and writes skill.json into the
data directory.
From source:
git clone https://github.com/neoromantic/telegram-sync-cli.git
cd telegram-sync-cli
bun install
bun run src/index.ts --help # or: bun run build, then ./dist/tg
Quickstart
# Telegram API credentials from https://my.telegram.org/apps
export TELEGRAM_API_ID=123456
export TELEGRAM_API_HASH=your_api_hash
tg auth login --phone +15551234567 # or: tg auth login-qr
tg chats list --limit 20 --format pretty
tg daemon start # keep the cache in sync (foreground)
tg messages search --query "hello" --format pretty
tg --help and tg --version work before you set any credentials. When running from source,
Bun auto-loads .env, so you can cp .env.example .env and fill it in instead.
Features
- Multi-account management (
tg accounts, --account)
- Phone and QR login (
tg auth)
- Read-only daemon for real-time sync and history backfill that keeps the contact list current;
catch-up and history for large groups and channels on request (
tg chats sync); the CLI does
all mutations (tg send)
- Groups are cached only while you take part: a group adds new messages only if you sent a
message there in the last 90 days; what is cached stays
(sync strategy)
- Full Telegram API access through
tg api <method>
- Cache-first reads (stale-while-revalidate) with
--fresh to bypass the cache
- Chat history with cursor paging (
tg messages list), filtered by type (voice notes, round
videos, photos…) and date, and a message in context (tg messages get), cache-first
- Unread messages: chats with unread messages (
tg chats list --unread), their unread messages
(tg messages unread) and marking them read (tg messages read); the daemon keeps the
counts current
- Full-text search (FTS5) over cached messages;
--forms also finds other forms of each word
(квартира finds квартиру, meeting finds meetings)
- Rich messages:
tg send --markdown or --html sends headings, tables, lists, quotations,
code blocks and formulas that Telegram renders natively
- Files: send one with
tg send --file; download the files of one or many messages into a
per-account media cache with tg media download (cached files need no network, partial
downloads resume, name templates for --out); every message item shows its attachment
(kind, name, MIME type, size)
- Voice notes and video notes to text with a speech-to-text program on your machine
(
tg media transcribe, whisper.cpp or an openai-whisper CLI); tg messages search finds spoken words, and tg messages shows the
transcript
- Service messages say what happened: calls (video, duration, why they ended), pins and group
changes in each item's
action
- Rich messages (headings, tables, lists, task lists, code, formulas) read as rich Markdown in
text, and search finds them by any word
- Contacts, chats and users (
tg contacts, tg chats, tg me, tg user)
- Read-only SQL over the cache (
tg sql) with an annotated schema
- Rate-limit tracking, config and status tooling (
tg config, tg status)
- JSON output and a stable error schema for scripts and agents; paged reads add
next, the
command for the next page with the same account and filters
How it compares
tg keeps your Telegram in a local SQLite cache for offline search and SQL;
telegram-mcp is an MCP server that makes one live call
per tool. Compared on 2026-10-21 from its README and tool list:
| Feature |
tg |
telegram-mcp |
| Interface |
CLI, JSON output, fixed exit codes |
MCP server (stdio, HTTP, SSE) |
| Agent integration |
any agent with a shell |
MCP clients, Claude Desktop bundle |
| Local cache, offline queries |
✅ SQLite, cache-first, --fresh |
❌ live calls only |
| Message search |
✅ full-text (FTS5) over cached messages |
✅ Telegram server search |
| SQL over your data |
✅ read-only tg sql |
❌ |
| Background sync |
✅ read-only daemon: real-time, catch-up after restarts and reconnects, history backfill; catch-up and history for large groups and channels on request (tg chats sync) |
partial: waits for events, stores nothing |
| Any MTProto method |
✅ tg api <method> |
❌ fixed tool set |
| Read chat history |
✅ tg messages list (paging) and tg messages get (context), cache-first, --fresh |
✅ |
| Unread messages, mark as read |
✅ tg chats list --unread, tg messages unread, tg messages read |
✅ |
| Send text |
✅ replies, silent, rich messages from Markdown or HTML (--markdown, --html) |
✅ replies, Markdown/HTML |
| Edit, delete, forward, pin |
partial: via tg api (#23) |
✅ |
| Send and download media |
✅ one file per message (tg send --file), cached downloads with resume (tg media download), local transcription of voice and video notes (tg media transcribe); sending albums and voice messages later |
✅ files, albums, voice, transcription |
| Wait for new messages |
✅ tg messages wait (JSON, timeout, settle) and tg messages watch (NDJSON), from the daemon's cache |
✅ |
| Groups, channels, admin rights |
partial: via tg api |
✅ |
| Contacts |
list, search, get; the rest via tg api |
✅ add, import, block, export |
| Reactions, polls, drafts, scheduled, folders |
partial: via tg api |
✅ |
| Multi-account |
✅ --account; one cache per account, the daemon syncs each account with its own client |
✅ |
| Guardrails for agents |
✅ read-only mode, chat allowlist (readOnly, allowedChats) |
✅ read-only mode, chat allowlist |
| Install |
Homebrew binary, or bun/npm with Bun |
git clone + uv, pip from git, Docker |
| Runtime, license |
TypeScript on Bun (mtcute), MIT |
Python (Telethon), Apache-2.0 |
Usage
tg <command> [options]
tg accounts list
tg contacts list --limit 50
tg chats list --limit 20
tg chats sync @examplenews # also sync a large group or channel
tg messages list --chat @username --limit 20
tg messages list --chat @username --type voice,round --since 7d # voice notes and round videos
tg messages get --chat @username --id 42 --context 3
tg messages search --query "hello world" --since 7d # both words, any order
tg messages search --query "квартира" # квартиру, квартирой… too
tg messages wait --chat @username --after 42 --timeout 60 # block until the next reply
tg messages unread # unread messages of every chat
tg messages read --chat @username # mark the chat read
tg send --to @username --message "Hi there"
tg send --to me --file ./report.pdf --caption "Q3"
tg media download --chat me --id 42 --out ./ # into the media cache, then copied here
tg media download --chat me --id 42 --id 43 --out './media/{date}_{id}_{name}'
tg api account.checkUsername --username=myuser
tg sql --query "SELECT * FROM users_cache LIMIT 10"
tg daemon start
tg status
tg db vacuum # give an account cache's free space back to the disk
tg config path
Global options: --format json|pretty|quiet, --verbose, --quiet. Many commands accept
--account (each account has its own cache; without the flag a command uses the active
account); cache-aware commands accept --fresh. See tg <command> --help. Upgrading from 0.3
with several accounts: see per-account caches.
| Variable |
Description |
TELEGRAM_API_ID |
Your Telegram API ID |
TELEGRAM_API_HASH |
Your Telegram API hash |
TELEGRAM_PROXY |
Optional proxy URL for CLI and daemon connections |
TELEGRAM_SYNC_CLI_DATA_DIR |
Override the data directory (default ~/.telegram-sync-cli) |
TELEGRAM_SYNC_CLI_TELEMETRY |
0 turns usage stats off |
TELEGRAM_SYNC_CLI_READ_ONLY |
1 refuses commands that change Telegram (read-only mode) |
TELEGRAM_SYNC_CLI_ALLOWED_CHATS |
Comma-separated chats this process may use, such as @teamchat,me |
DO_NOT_TRACK |
1 turns usage stats off |
MTCUTE_LOG_LEVEL |
mtcute log level |
VERBOSE |
Verbose logging (1) |
For a proxy, set TELEGRAM_PROXY=socks5://127.0.0.1:1080. Supports socks4://,
socks5://, http://, https:// and MTProxy links, with credentials in the URL.
socks5h:// and addresses without a scheme are unsupported. See
proxy configuration.
Reference: CLI commands, authentication,
configuration, architecture,
daemon, sync strategy, caching,
database schema, storage, SQL,
search, media, rate limiting.
Known gaps are tracked in issues.
For AI agents