To get your Telegram API credentials, log in to my.telegram.org with your Telegram phone number, open API development tools, fill out the application form, and copy your api_id and api_hash. Note that Telegram does not issue a single “API key” for everything—depending on what you are building, you may need an MTProto client pair (api_id + api_hash), a Bot Token from @BotFather, or a Gateway API token.
Which Credential Do You Actually Need?
Picking the right credential upfront saves hours of debugging against the wrong endpoint or library:
| Credential Type | Where You Get It | What It Powers |
|---|---|---|
api_id + api_hash |
my.telegram.org → API development tools |
Custom clients, scripts, and user-account automation over Telegram’s MTProto protocol. |
| Bot Token | @BotFather inside Telegram (/newbot) |
Standard bots interacting via the HTTP Bot API (webhooks or long polling). |
| Gateway API Token | Telegram Gateway portal | Sending one-time login/verification codes to phone numbers over Telegram instead of SMS. |
Step-by-Step: Getting api_id and api_hash
- Have an active Telegram account ready: Make sure your phone number is already registered on an official Telegram app so you can receive the login confirmation code.
- Sign in to the developer portal: Open my.telegram.org in your browser and enter your phone number in full international format (for example,
+6281234567890). - Verify your login: Enter the security code delivered to your active Telegram app. Avoid requesting codes repeatedly in a short window, which can trigger a temporary
PHONE_NUMBER_FLOODwait. - Open API development tools: Click API development tools on the portal home screen.
- Register your application: Fill in the form with an App title and Short name (alphanumeric characters), select the target Platform (such as Desktop, Android, iOS, or Web), and click Create application.
- Copy and store your credentials: Telegram immediately displays your numeric
api_idand secretapi_hash. Each phone number can only have oneapi_idtied to it.
Storing Credentials Safely & Avoiding Common Pitfalls
Treat api_hash like a password. If someone obtains both your api_id and api_hash—and especially any generated session file—they can impersonate your client configuration or authenticate sessions if not properly guarded.
- Use environment variables: Keep
API_IDandAPI_HASHin a local.envfile and add.env(along with.sessionfiles) to.gitignoreso secrets never land in public repositories. - Watch for copy-paste typos: Extra spaces, quotation marks, or mistyped characters frequently cause invalid credential errors during client initialization.
- Respect Telegram’s Terms of Service: Obtaining API access does not grant blanket permission to scrape private data, spam users, or automate abusive behavior. Accounts that violate platform rules risk automated restrictions or bans.
Quick Sanity Check in Python
Once your values are stored in your environment, you can verify that your MTProto client library (such as Telethon) accepts them before writing real logic:
import os
from telethon import TelegramClient
api_id = int(os.environ["TG_API_ID"])
api_hash = os.environ["TG_API_HASH"]
client = TelegramClient("my_session", api_id, api_hash)
async def main():
await client.start()
me = await client.get_me()
print(f"Authenticated as {me.first_name} (@{me.username})")
with client:
client.loop.run_until_complete(main())
Leave a Reply