A Telegram API ID (api_id) is a numeric identifier issued by Telegram that registers your custom client application with Telegram’s MTProto protocol. Paired with a secret string called an api_hash, it tells Telegram’s servers which application is attempting to communicate over MTProto—though it still requires an account authorization step (such as a login code or QR sign-in) to act on behalf of a specific user account.
If you are building a custom Telegram client, automating tasks on a user account, or working with an MTProto library (like Telethon, Pyrogram, or GramJS), understanding how api_id works—and how it differs from a bot token—saves you from common security and compliance mistakes.
How api_id and api_hash Work Together
Think of api_id and api_hash as your application’s passport and secret stamp on the MTProto network:
api_id(Numeric ID): Identifies your registered application to Telegram. It is not sufficient on its own to authenticate requests.api_hash(Secret String): Paired with yourapi_idto authorize the application itself. If leaked publicly, treat it as compromised.- Account Authorization (
session): Having anapi_idandapi_hashdoes not automatically log into a Telegram account. You still complete a login flow (phone code, two-step verification password, or QR login), which produces a session file or session string that persists your authenticated session.
API ID + Hash vs. Bot Token
Developers often mix up MTProto client credentials and Bot API tokens because both are used to build Telegram integrations. They serve different identities and endpoints:
| Feature | api_id + api_hash (MTProto Client API) |
Bot Token (@BotFather Bot API) |
|---|---|---|
| Where you get it | my.telegram.org under API development tools |
@BotFather inside Telegram via /newbot |
| Account identity | Authenticates a user account (after login/session setup) | Authenticates a bot account (is_bot: true) |
| Protocol / Interface | Raw MTProto client protocol (via MTProto libraries) | HTTPS Bot API (api.telegram.org/bot...) |
| Best fit | Custom clients, personal account workflows, user-level MTProto features | Standard bots, webhooks/polling, group commands, bot menus |
| Risk if abused | Account restrictions or bans under Terms of Service | Token can be revoked/regenerated; bot can be removed |
If a standard bot via @BotFather can solve your use case, start there. It is simpler to operate and avoids managing user sessions.
Step-by-Step: Getting Your Telegram API ID
Obtaining an api_id and api_hash is free and takes only a few minutes:
- Have an active Telegram account: Ensure your phone number is registered on an official Telegram app and can receive login codes and developer notifications.
- Sign in to Telegram’s developer portal: Go to
my.telegram.organd log in using your phone number in international format (for example,+65...or+1...) and the confirmation code sent to your Telegram app. - Open API development tools: Select API development tools on your account dashboard.
- Register your application: Fill out the form with your application title, short name, and target platform. Keep details accurate and aligned with how you plan to use the client.
- Copy and store your credentials: Telegram displays your
api_idandapi_hash. Save both securely—for example, in environment variables or a secret manager rather than hardcoding them into source files.
Note that Telegram’s documentation states each phone number can have only one api_id associated with it.
Security, Storage, and Compliance Rules
Because api_id and api_hash—and especially the resulting session files—tie directly to real accounts and Telegram’s infrastructure, careless handling causes real headaches:
- Never commit secrets or sessions to Git: Keep
api_hashand session strings/files out of public repositories, client-side browser bundles, and unencrypted logs. Use.envfiles listed in.gitignorefor local development, and proper secret storage in production. - Do not borrow random sample credentials: Avoid reusing placeholder credentials found in tutorials or open-source samples for real or public applications; register your own application credentials.
- Protect login codes and 2FA passwords: Only enter login codes on official Telegram flows, and never share two-step verification passwords or active session files with untrusted third parties.
- Respect Telegram’s Terms of Service and rate limits: Automated messaging, aggressive requests, or unauthorized scraping/spamming can trigger flood limits or account bans. Always respect user privacy, chat permissions, and platform rules.
Quick Troubleshooting for Common Errors
API_ID and API_HASH must be set: Your app isn’t loading environment variables properly. Confirm your.envfile is in the expected directory with no extra spaces around=.api_id/api_hash invalid: Double-check for accidental leading/trailing spaces or quote characters when pasting values.PHONE_NUMBER_FLOOD: You requested login codes too many times in a short window. Pause, wait before retrying, and avoid rapid repeated auth attempts.my.telegram.orglogin issues: The web portal can occasionally be finicky or sensitive to network/browser conditions; try again later or from a clean browser session while ensuring your Telegram app is active to receive the code.
Leave a Reply