Skip to Content
GuidesTelethon .session + .json

Telethon .session + .json

Many tools store a Telethon account as two files with one base name: a Telethon SQLiteSession file and a .json companion with the API profile and account fields. opentele2 writes both byte for byte the same in every ecosystem, over the SQLite bundled in the core, and reads pairs that Telethon, the Python package or another tool wrote.

  • account.session
  • account.json
Warning:

The .session file holds the auth key: it is the logged-in account. Copy pairs only between machines you control, never commit them, and never paste the file or its key into a chat or an issue. The .json holds the phone number and names when they were saved.

Name the pair

A base path gives both file names: one trailing .session is removed, then one trailing .json, and each suffix is added back. account, account.session and account.json all name the same pair.

paths.py
from opentele2 import _native print(_native.session_pair_paths("account.session")) # ('account.session', 'account.json')

Write the pair

The .session file is a Telethon database of version 8, the layout Telethon 1.45 reads, with one sessions row: DC id, server address, port and auth key. The companion is written as Python’s json.dump writes it, with two-space indentation.

write.py
import asyncio from opentele2.td import TDesktop async def from_tdata(): # The folder's own session and profile. return await TDesktop("tdata").SaveSessionJson("account.session") async def from_client(client): # client: a connected opentele2 TelegramClient; fills in the account fields. return await client.SaveSessionJson("account.session", fetch_user_info=True) print(asyncio.run(from_tdata())) # ('account.session', 'account.json')

SaveSessionJson(path, api=None, fetch_user_info=False) returns the two paths. With fetch_user_info=True on a connected client it fills id, phone, username, first_name, last_name and is_premium from get_me(); id is written whenever the client knows its user.

Know the companion’s keys

The companion starts with the nine profile keys, in this order. They become the API profile when the pair is loaded, so a session keeps announcing the device it was saved with.

KeyProfile fieldWhen absent
app_idapi_idrequired
app_hashapi_hashrequired
devicedevice_model—
sdksystem_version—
app_versionapp_version—
system_lang_packsystem_lang_codeen
system_lang_codesystem_lang_coderead from system_lang_pack
lang_packlang_packempty
lang_codelang_codeen

The account keys follow with their defaults: twoFA (null), role (""), id, phone, username, date_of_birth, date_of_birth_integrity (all null), is_premium and has_profile_pic (false), spamblock, register_time, last_check_time, avatar (null), first_name and last_name (""), sex and proxy (null), ipv6 (false) and session_file, the base name of the pair. Keys you add go after them.

account.json
{ "app_id": 2040, "app_hash": "b18441a1ff607e10a989891a5462e627", "device": "Desktop", "sdk": "Windows 11 x64", "app_version": "7.3.1 x64", "system_lang_pack": "en-US", "system_lang_code": "en-US", "lang_pack": "tdesktop", "lang_code": "en", "twoFA": null, "role": "", "id": null, "phone": null, "username": null, "date_of_birth": null, "date_of_birth_integrity": null, "is_premium": false, "has_profile_pic": false, "spamblock": null, "register_time": null, "last_check_time": null, "avatar": null, "first_name": "", "last_name": "", "sex": null, "proxy": null, "ipv6": false, "session_file": "account" }
keys.py
from opentele2.api import API print(API.TelegramDesktop().to_json({"session_file": "account"})) # the dict above

APIData.to_json(extra) and APIData.from_json(data) are the profile half of the companion.

Read the pair back

read.py
import asyncio from opentele2.tl import TelegramClient async def main(): # The profile comes from the .json; the session from the .session file. client = await TelegramClient.FromSessionJson("account.session") await client.connect() print(await client.get_me()) asyncio.run(main())

FromSessionJson(session_path, json_path=None, flag=UseCurrentSession, password=None) reuses the stored session. With CreateNewSession it logs a new session in by QR code through the stored one, on the stored profile; pass the 2FA password as password=.

Turn the pair into tdata

to_tdata.py
import asyncio from opentele2.td import TDesktop async def main(): tdesk = await TDesktop.FromSessionJson("account.session") # the pair's own key, offline tdesk.SaveTData("tdata_from_pair") asyncio.run(main())

Next

Last updated on