Skip to Content
GuidesClient session to tdata

Client session to tdata

Turn a logged-in Telethon, mtcute or grammers client into a Telegram Desktop tdata folder. There are two ways, and the choice decides which session ends up in the folder:

  • A new session. The logged-in client authorises a new session by QR login (the 2FA password included when the account has one), on the device of the API profile you pass. The folder gets the new key. Needs the network.
  • The current session. The folder gets the client’s own auth key. Nothing is logged in, and the same device keeps using the key.
Warning:

A QR login adds a real session to the account, visible in its active sessions. Run it on an account you own, keep the 2FA password out of your source (read it from the environment) and never share the folder it produces.

Generate the device

A new session gets a device of its own. Generate picks it from the client’s catalog, seeded by a unique id you store with the account, so the same id gives the same device in Python, Node and Rust. See API profiles and devices.

device.py
from opentele2.api import API device = API.TelegramDesktop.Generate("windows", "my-id") print(device.device_model, device.system_version) # Vostro 3520 Windows 11 x64

Log a new session in by QR code

The flow is the same in every package: align the new client with the authorised client’s DC, export a login token on the new client, accept it on the authorised one, answer the 2FA check with the password through SRP, then send the requests an official client sends after a login. It makes up to five attempts.

qr_login.py
import asyncio import os from opentele2.api import API, CreateNewSession from opentele2.exception import NoPasswordProvided, PasswordIncorrect from opentele2.tl import TelegramClient async def main(): # A logged-in session, on the profile it was created with. client = TelegramClient("telethon.session", api=API.TelegramDesktop) try: tdesk = await client.ToTDesktop( CreateNewSession, api=API.TelegramDesktop.Generate("windows", "my-id"), password=os.environ.get("TG_PASSWORD"), ) except (NoPasswordProvided, PasswordIncorrect) as error: print("2FA:", error) return tdesk.SaveTData("tdata_new") asyncio.run(main())

ToTDesktop connects the client when it is not connected and needs it authorised. client.QRLoginToNewClient(session, api, password) runs the same login and returns the new TelegramClient instead of a folder.

Save the folder

save.py
# tdesk: the TDesktop from ToTDesktop tdesk.SaveTData("tdata_new") # SaveTData("tdata_new", passcode="...") to lock it

The folder is written as Telegram Desktop 7.3.1 writes it. Without a passcode, performance mode (TDesktop.kPerformanceMode, on by default) uses fixed keys and leaves the configs files out.

Or store the current session offline

current.py
import asyncio from opentele2.api import UseCurrentSession from opentele2.td import TDesktop async def from_pair(): # The companion's id is the user id, so nothing is sent. tdesk = await TDesktop.FromSessionJson("account.session") tdesk.SaveTData("tdata_current") async def from_client(client): # client: an opentele2 TelegramClient; get_me() runs first without a known user id. tdesk = await client.ToTDesktop(UseCurrentSession) tdesk.SaveTData("tdata_current") asyncio.run(from_pair())
Note:

A folder that holds the current session shares its key with the client it came from: both are the same session to the server. Use a new session when the folder will run on another device.

Next

Last updated on