How to Get a Telegram API Key: A Complete Developer Guide
Building on Telegram usually starts with a simple bottleneck: finding the right credentials to authenticate your code. Unlike platforms that issue a single “API key,” Telegram splits access into two distinct paths depending on what you are building—MTProto client credentials (api_id and api_hash) for custom clients and user-account automation, and Bot API tokens for standard bots managed through @BotFather.
Which Credential Do You Actually Need?
Choosing the right path upfront saves hours of debugging later:
| Credential Type | Best For | Where You Get It | Protocol / Interface |
|---|---|---|---|
api_id + api_hash |
Custom Telegram clients, personal automation, scripts acting as a user account, bulk data tasks | my.telegram.org under API development tools |
MTProto API (via Telethon, Pyrogram, GramJS, TDLib, etc.) |
Bot Token (123456:ABC-DEF...) |
Chatbots, notification alerts, slash commands, inline bots, webhook/polling services | @BotFather inside Telegram (/newbot) |
Telegram Bot API (HTTPS JSON / webhooks) |
If you are building a typical chatbot or automated alert sender, start with a Bot Token. If you need full user-session capabilities that a standard bot account cannot perform, get an api_id and api_hash.
Path 1: Getting api_id and api_hash (MTProto API)
MTProto credentials identify your application to Telegram’s servers when establishing a client connection.
- Open the developer portal: Visit
[https://my.telegram.org](https://my.telegram.org)in a desktop or mobile browser. - Sign in with your phone number: Enter the phone number tied to your active Telegram account in international format (for example,
+886...or+1...), then confirm the login code delivered inside your Telegram app. - Go to API tools: Click API development tools on your account dashboard.
- Register your application:
- App title: Enter a clear name for your project (alphanumeric characters work best; avoid spaces or symbols if a form rejects them).
- Short name: Enter a concise identifier for your app.
- Platform: Select the platform that matches your runtime or target (such as Desktop, Android, iOS, or Web/Other).
- Description: Optionally add a short note on what the app does.
- Save your credentials: Submit the form and copy both the numeric
api_idand the hexadecimalapi_hash.
Path 2: Getting a Bot Token via @BotFather (Bot API)
For most automation workflows, a Bot Token is faster to provision and safer to isolate from your personal phone number.
- Search for
@BotFather: Open Telegram and search forBotFather—verify the official blue checkmark so you do not message a lookalike. - Start a new bot: Send
/newbotand follow the interactive prompts. - Choose display name and username:
- Provide a name (shown in chats and profiles; can be changed later).
- Provide a username ending in
bot(for example,my_project_helper_bot; must be unique across Telegram).
- Copy the HTTP API token:
@BotFatherreplies with a token formatted like123456789:AAH.... Treat this string like a password.
Security & Operational Best Practices
- Never hardcode secrets in source code: Store
API_ID,API_HASH, orBOT_TOKENin environment variables or a secret manager, and keep.envfiles listed in.gitignore. - Keep
api_hashprivate: Treat yourapi_hashas a secret key—leaking it makes it easier for others to impersonate your registered app identity. - Revoke and rotate if exposed: If a bot token is ever committed to a public repository or leaked in logs, open
@BotFather, run/revoke, select the bot, and immediately update your deployment with the new token. - Respect rate limits and Terms of Service: Automated scraping, spam, or aggressive unrequested messaging can trigger temporary flood waits or account restrictions. Pace requests and handle
FloodWaiterrors gracefully in your client library.
Troubleshooting Common Hiccups
- Login code not arriving on
my.telegram.org: Check active Telegram sessions on another device first—Telegram often sends the confirmation code to an already-logged-in app rather than via SMS. - Form errors when creating an app: Simplify the App title and Short name to plain letters and numbers, and ensure you are not connected through a flaky proxy or VPN that blocks web requests.
- Bot username taken: Telegram usernames are globally unique and case-insensitive; try a more specific prefix or suffix ending in
bot.
Leave a Reply