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_idandapi_hashobtained frommy.telegram.org. It powers custom clients, user-account automation, and full access to Telegram’s nativeTelegram’s custom binary messaging and encryption protocol. layer. - Telegram Bot API: Uses a bot token generated through
@BotFatherinside 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.
- 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.
- 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.
- Open API development tools: Once logged in, select API development tools.
- 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.
- Save your credentials: Copy your numeric
api_idand secretapi_hashimmediately. Store them in environment variables or a secret manager rather than hardcoding them into source files.
Important limit: Telegram generally allows one
api_idper phone number through the self-service portal. Treatapi_hashlike 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.
- Open
@BotFather: Search for@BotFatherin Telegram (verify the official checkmark/handle) and open a chat. - Create the bot: Send
/newbotand follow the prompts. - Choose a display name and username: Pick any display name. Choose a unique username ending in
bot(for example,example_notifier_bot). - Copy the HTTP API token:
@BotFatherreplies with a token formatted like123456789: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 / |
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, orTELEGRAM_BOT_TOKENin a.envfile 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
.sessionor 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.
Leave a Reply