Complete guide to generating and using Telegram string sessions
Go to my.telegram.org and log in with your phone number. Navigate to "API Development Tools" and create a new application. Note your API ID (a number) and API Hash (a 32-char hex string).
Choose between Pyrogram or Telethon, and select either User or Bot session type. User sessions use your personal account. Bot sessions require a bot token from @BotFather on Telegram.
Input your API ID, API Hash, and your phone number (with country code, e.g. +91XXXXXXXXXX) or bot token. Click "Send OTP" for user sessions or "Generate Session" for bot sessions.
Telegram will send a verification code to your Telegram app (not SMS). Open Telegram and find the code from "Telegram" in your chats. Enter it in the field — it auto-submits when all 5 digits are entered.
If your account has Two-Step Verification enabled, you will be prompted for your cloud password after the OTP step. Enter it to proceed.
Your string session will be displayed. Copy it immediately and store it securely as an environment variable. Never share it with anyone — it is equivalent to your account password.
from pyrogram import Client
client = Client(
name="my_account",
api_id=API_ID,
api_hash=API_HASH,
session_string=STRING_SESSION
)
async def main():
async with client:
me = await client.get_me()
print(f"Logged in as {me.first_name}")from telethon import TelegramClient
from telethon.sessions import StringSession
client = TelegramClient(
StringSession(STRING_SESSION),
API_ID,
API_HASH
)
async def main():
async with client:
me = await client.get_me()
print(f"Logged in as {me.first_name}")Join our Telegram channel for support, updates, and more tools.