Skip to content
telegram-sync-cli
GitHubInstall

telegram-sync-cli · Installs the tg command

A Telegram CLI
for you and your agents.

Search your Telegram messages offline, query them with SQL, read a chat’s history, send messages and files, download attachments or call any Telegram API method from your shell. tg logs in to your own account and saves your messages to a local SQLite cache. Commands answer in JSON by default.

brew install neoromantic/tap/tg

macOS and Linux. You also need your own Telegram API ID and hash. Setup

View on GitHub

Free · Open source · What’s new in 0.16.0

zsh

$ tg messages search --query "lisbon" \
    | jq -r '.data.results[]
      | "\(.chat.title) · \(.text)"'
Ana Ribeiro · Flights to Lisbon are cheaper on the 14thTomás Klein · I left the climbing shoes in Lisbon, can you grab them?Maya Chen · Lisbon or Porto for the offsite?$ 
Search, piped through jq. Real tg output from a cache of made-up messages.

Sync daemon

The daemon saves your messages
and never sends anything.

Run tg daemon start and leave it running in tmux or as a service. It syncs every account you’re logged in to, reads from Telegram and writes only to your local cache.

On start
It loads your chat list, then catches up your private chats and small groups: the newest 100 messages the first time, or everything that arrived while the daemon was stopped. It catches up again after a reconnect.
While it runs
Every new message is saved as it arrives, in every chat, including large groups and channels. Edits update the saved copy. A deletion only sets a flag, so --includeDeleted still finds the message. Your contact list stays current too.
Older history
Fetched in the background, page by page, back to each chat’s first message. Large groups and channels get it once you turn them on with tg chats sync <chat>.
Cache size
Messages stay. The daemon prunes old API activity, expired rate-limit windows and old sync jobs on a schedule. tg status shows each account’s cache size, and tg db vacuum gives free space back to the disk.

tg send · tg media · tg api

Send text and files,
or call any API method.

Built-in commands cover search, contacts, chats, sending messages and files, and downloading attachments. For everything else, tg api calls a Telegram API method by name and prints the result as JSON.

zsh

$ tg api account.checkUsername \
    --username=myuser
{
  "success": true,
  "data": {
    "method": "account.checkUsername",
    "result": false
  }
}
Real tg output: myuser is taken.
tg send
Text to an @username, a user ID, a phone number or me for Saved Messages, with --reply-to and --silent. --file sends one file with an optional --caption: a JPEG, PNG or WebP image up to 10 MB as a photo, anything else as a document. The sent message goes into the cache right away.
tg media download
Saves a message’s file to the account’s media cache and prints its path; --out copies it where you want. A cached file needs no network, and a partial download resumes. Media from chats with content protection is refused. tg media clean --all empties the cache.
tg api <method>
Arguments are the method’s raw fields: flags such as --username=myuser, or a --json object for nested ones.
Telegram API methods

For AI agents

Agents get JSON
and an exit code.

Results go to stdout as JSON. Errors go to stderr as JSON with an error code and whether a retry can work. Paged reads suggest the next command with the same account and filters. Any agent that can run shell commands can use tg, with no MCP server or plugin.

zsh

$ tg me
{
  "success": false,
  "error": {
    "code": "AUTH_REQUIRED",
    "message": "No account found. Please log in first with \"tg auth login\"",    "suggestion": "Log in with tg auth login",    "retryable": false
  }
}
$ echo $?
2
Real output on a machine with no account yet.
Learn tg before you log in
tg --help, tg --version and tg sql print-schema need no account and no API credentials, so an agent can learn the commands and the cache schema first.
Flood waits
A command waits out Telegram flood waits of up to 60 seconds. A longer one returns RATE_LIMITED with wait_seconds, on that call and on later calls until it ends.
Read a chat page by page
Each tg messages list page carries nextBefore and nextAfter, so an agent can walk a chat. tg messages get --context 5 opens a search hit with the messages around it.
Wait for a reply
tg messages wait returns new messages as soon as the daemon stores them. tg messages unread prints unread ones, and tg messages read marks a chat read.

Get started

Set it up
in four steps.

  1. Install tg

    brew install neoromantic/tap/tg

    Or with Bun 1.4 or newer:

    bun i -g @goodit/telegram-sync-cli
  2. Add your API credentials

    export TELEGRAM_API_ID=123456export TELEGRAM_API_HASH=your_api_hash

    Create them at my.telegram.org/apps. Put the exports in your shell profile so the daemon sees them too.

    If Telegram is blocked, set TELEGRAM_PROXY to a SOCKS5, HTTP or MTProxy URL for the CLI and daemon. Proxy setup ↗

  3. Log in

    tg auth login-qr

    Scan the code in Telegram on your phone: Settings → Devices → Link Desktop Device. Or log in by phone with tg auth login --phone +15550000000.

  4. Start the daemon, then search

    tg daemon start

    Leave it running. In another terminal:

    tg messages search --query "lisbon"

    Search covers what the daemon has saved so far. tg status shows its progress, any failed sync jobs and the cache size.

Privacy & data

Your session and cache
stay on your machine.

Usage stats. Since 0.7.0 tg sends its author usage stats: how often it runs, which commands run, which errors come up, the version and the OS, with a random install ID. Never messages, names, phone numbers, IDs or queries. Nothing is sent before tg has shown a one-line notice, and tg telemetry off turns it off. Everything it sends ↗

Local files. Sessions, caches and settings live in ~/.telegram-sync-cli, or wherever TELEGRAM_SYNC_CLI_DATA_DIR points. tg reads your API ID and hash from the environment and connects to Telegram directly or through your configured proxy.

Deleted messages stay. A message already in your cache stays there after it is deleted in Telegram. Delete the data directory to remove everything.

This website uses cookieless PostHog analytics for page visits, link clicks and the steps a browser’s AI agent takes on its controls: the kind of control, the action and whether it was allowed, and whether the browser offers the page to its agent at all. Session recording is off. Website analytics are separate from tg and never see your Telegram data.

Questions

Before you log in.

Install tg and log in
with a QR code.

Install tgHelp shape what’s next