Skip to Content
ReferencePythonopentele2.api

opentele2.api

The official client API classes, with every value from the core. Each client class holds the default profile of that client, and Generate a device picked for a seed.

from opentele2.api import API, APIData, CreateNewSession, LoginFlag, UseCurrentSession

API

Classapi_idlang_packDefault deviceGenerate
API.TelegramDesktop2040tdesktopDesktop, Windows 11 x64Windows, macOS or Linux device
API.TelegramAndroid6androidsamsungSM-S938B, SDK 36Android device
API.TelegramAndroidX21724androidsamsungSM-S938B, SDK 36Android device
API.TelegramIOS10840iosiPhone 17 ProiOS device
API.TelegramMacOS2834macosMacBook Air M4Mac, bare version
API.TelegramWeb_A2496emptybrowser user agent, Windowsbrowser, OS name
API.TelegramWeb_K2496macosbrowser user agent, Win32browser, navigator.platform
API.Webogram2496emptybrowser user agent, Win32browser, navigator.platform

API.TelegramWeb_Z is API.TelegramWeb_A. Every class has lang_code en and system_lang_code en-US. The app versions come from PLATFORM_VERSIONS: on import they follow the fetched versions, and Webogram’s 0.7.0 stays fixed.

opentele2/api.py
class API: class TelegramDesktop(APIData): @classmethod def Generate(cls, system: str | None = None, unique_id: str | None = None) -> Self: ... class TelegramAndroid(APIData): ... class TelegramAndroidX(APIData): ... class TelegramIOS(APIData): ... class TelegramMacOS(APIData): ... class TelegramWeb_A(APIData): @classmethod def Generate(cls, unique_id: str | None = None) -> Self: ... TelegramWeb_Z = TelegramWeb_A class TelegramWeb_K(APIData): @classmethod def Generate(cls, unique_id: str | None = None) -> Self: ... class Webogram(APIData): @classmethod def Generate(cls, unique_id: str | None = None) -> Self: ...

API.TelegramDesktop.Generate takes the platform first: Generate("windows", "id"), with "windows", "macos" or "linux" matched case-sensitively. A single positional argument that is not a platform name is the unique_id, as in Generate("id"); without a platform the core picks one from the unique_id hash. The app version gets x64 on Windows and arm64 on macOS.

APIData

The base class of every profile, and a profile of its own when built with an api_id and api_hash.

opentele2/api.py
class APIData: api_id: int api_hash: str device_model: str system_version: str | None = None app_version: str | None = None lang_code: str | None = None system_lang_code: str | None = None lang_pack: str | None = None def __init__( self, api_id: int | None = None, api_hash: str | None = None, device_model: str | None = None, system_version: str | None = None, app_version: str | None = None, lang_code: str | None = None, system_lang_code: str | None = None, lang_pack: str | None = None, ) -> None: ... def copy(self) -> APIData: ... def get_cls(self) -> type[APIData]: ... def destroy(self) -> None: ... @staticmethod def from_json(data: dict) -> APIData: ... def to_json(self, extra: dict | None = None) -> dict: ... @classmethod def Generate(cls, unique_id: str | None = None) -> Self: ...
NameDescription
APIData(...)A profile; every argument left out takes the class value. APIData itself needs api_id and api_hash (else NoInstanceMatched) and defaults device_model to the local machine (PC 64bit, PC 32bit or the machine name).
copy()A new instance with the same values; works on a class and on an instance.
get_cls()The class of an instance, or the class itself.
destroy()Does nothing.
from_json(data)The profile of a session .json object: app_id, app_hash, device, sdk, app_version, system_lang_code (falling back to system_lang_pack), lang_pack and lang_code. Raises NoInstanceMatched without app_id or app_hash.
to_json(extra)The session .json object of the profile: the nine profile keys, then the account keys with their defaults (twoFA, role, id, phone, username, … ipv6, session_file), updated with extra.
Generate(unique_id)A profile with a device picked for unique_id: the same id gives the same device. Android, Android X, iOS and macOS use it as is; the desktop and web classes override it; any other class raises NotImplementedError.

Two profiles are equal when api_id, api_hash, device_model, system_version and app_version are; the language fields are not compared. str() of a class or instance lists its values.

profiles.py
from opentele2.api import API desktop = API.TelegramDesktop.Generate("windows", "account-42") android = API.TelegramAndroid.Generate("account-42") print(desktop.device_model, desktop.system_version, desktop.app_version) print(desktop == API.TelegramDesktop.Generate("windows", "account-42")) # True

Login flags

opentele2/api.py
class LoginFlag(int): ... class UseCurrentSession(LoginFlag): ... class CreateNewSession(LoginFlag): ...

Passed as the classes themselves, never instances, to ToTelethon, FromTelethon, ToTDesktop, FromTDesktop and FromSessionJson:

  • UseCurrentSession: the converted client or folder uses the existing auth key. Offline.
  • CreateNewSession: the existing session logs a new one in by QR code, with the 2FA password when the account has one, on the device of the api passed. Needs the network.

Any other value raises LoginFlagInvalid.

Last updated on