How to Get Telegram API ID and Hash: Step-by-Step Guide

Written by

in

Getting access to the Telegram API starts with one crucial distinction: are you building a bot, or are you building a client or automation that acts on a Telegram account?

Telegram offers two primary developer pathways with different credentials and setups:

  • Telegram Client API (MTProto): Uses an api_id and api_hash obtained from my.telegram.org. It powers custom clients, user-account automation, and full access to Telegram’s native Telegram’s custom binary messaging and encryption protocol. layer.
  • Telegram Bot API: Uses a bot token generated through @BotFather inside Telegram. It provides a straightforward HTTPS interface for bots interacting with users, groups, and channels.

Path 1: Getting api_id and api_hash (Client API / MTProto)

Use this path when building a custom Telegram client, writing scripts with libraries like Telethon or Pyrogram, or authenticating a user account.

  1. Create and verify a Telegram account: Sign up on an official Telegram app using an active phone number. Telegram sends developer notices to this number, so keep it current.
  2. Sign in to the developer portal: Open my.telegram.org in a browser, enter your phone number, and type the confirmation code delivered to your active Telegram app.
  3. Open API development tools: Once logged in, select API development tools.
  4. Complete the application form: Enter an App title and Short name (alphanumeric characters), choose your target Platform (such as Desktop, Android, iOS, or Web), and submit. These fields describe your application and do not need to match a published store listing.
  5. Save your credentials: Copy your numeric api_id and secret api_hash immediately. Store them in environment variables or a secret manager rather than hardcoding them into source files.

Important limit: Telegram generally allows one api_id per phone number through the self-service portal. Treat api_hash like a private key—if leaked, anyone can impersonate your app’s client identity.


Path 2: Getting a Bot Token (@BotFather)

If your project only needs a standard bot account—responding to commands, handling inline queries, posting to channels, or managing group chats—you do not need api_id and api_hash.

  1. Open @BotFather: Search for @BotFather in Telegram (verify the official checkmark/handle) and open a chat.
  2. Create the bot: Send /newbot and follow the prompts.
  3. Choose a display name and username: Pick any display name. Choose a unique username ending in bot (for example, example_notifier_bot).
  4. Copy the HTTP API token: @BotFather replies with a token formatted like 123456789:ABCdefGhIJKlmNoPQRsTUVwxyz. You can view or revoke it later using /mybots.

Quick Comparison

Feature Client API (api_id + api_hash) Bot API (Bot Token)
Where to get it my.telegram.org → API development tools @BotFather via /newbot
Protocol MTProto / Telegram’s type language defining RPC methods and data types. HTTPS JSON/REST-style Bot API
Best for Custom clients, user-session scripts, full account capabilities Standard bots, webhooks/polling, chat workflows
Setup complexity Higher (session auth, 2FA/SRP, peer resolution) Lower (token in URL/client wrapper)

Practical Tips Before You Start Coding

  • Keep secrets out of Git: Put TELEGRAM_API_ID, TELEGRAM_API_HASH, or TELEGRAM_BOT_TOKEN in a .env file excluded by .gitignore.
  • Respect rate limits and Terms of Service: Automated scraping, mass unsolicited messaging, or aggressive flood patterns can trigger temporary restrictions or account bans. If an account is ever flagged by mistake, Telegram notes that appeals sent to [email protected] should explain your legitimate use case clearly without automated spam templates.
  • Persist sessions safely: MTProto user scripts typically generate a local .session or string session after login (including two-step verification passwords if enabled). Anyone with access to that session file has active access to the logged-in account.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *