SDKs overview

The Telegram MTProto API is a low-level interface designed for building custom Telegram clients. It requires developers to manage cryptographic operations, network communication, and data serialization directly according to the MTProto protocol specification. To simplify this process, several SDKs and libraries are available. These tools encapsulate the complexities of the MTProto protocol, providing higher-level abstractions for interacting with Telegram's servers.

Both Telegram itself and the developer community contribute to the ecosystem of MTProto SDKs. Official SDKs are provided for core languages like C++ and Java, typically forming the foundation for Telegram's own applications. Community libraries, such as Telethon for Python and gotd for Go, extend this support to a wider range of programming languages, often offering more idiomatic interfaces and simplified development workflows.

When selecting an SDK, developers typically consider factors such as language preference, the level of abstraction required, community support, and the specific features of the Telegram API they intend to utilize. The MTProto API includes methods for user authorization, message exchange, file transfer, and bot interactions, all of which are exposed through these libraries.

Official SDKs by language

Telegram maintains official client libraries that serve as reference implementations for interacting with the MTProto API. These libraries are developed in C++ and Java, reflecting their use in Telegram's primary desktop and Android applications, respectively. They provide a direct, low-level interface to the MTProto protocol, handling the cryptographic handshakes, data serialization, and network transport as specified in the Telegram API documentation.

Developers using these official SDKs gain direct access to the protocol's capabilities, which can be beneficial for highly optimized or custom-feature clients. However, the low-level nature means developers need a deeper understanding of the MTProto specification compared to using higher-level community libraries.

Language Package/Project Installation Maturity
C++ Telegram-CLI / TDLib (Telegram Database Library) Typically built from source; check TDLib GitHub repository for build instructions. Mature, actively developed
Java Telegram-TAndroid / MTProto client classes Integrated within the Android application source; typically not provided as a standalone installable JAR for external projects. Developers often adapt relevant sections or use community alternatives. Mature, integral to Telegram Android app

TDLib (Telegram Database Library) is a notable official project. It is a cross-platform library designed to simplify the creation of custom Telegram clients. It manages network stack, encryption, and local data storage, providing a C++ interface that can be bound to other languages. More information on TDLib's architecture and usage can be found in its official documentation.

Installation

Installation methods for Telegram MTProto SDKs vary significantly between official and community-maintained libraries.

Official C++ (TDLib)

TDLib is typically built from its source code. This involves cloning the repository and using a build system like CMake. The exact steps can depend on the operating system and desired bindings (e.g., C++, Python, Java).

git clone https://github.com/tdlib/td.git
cd td
mkdir build
cd build
cmake ..
cmake --build .

Detailed installation instructions and prerequisites are available in the TDLib README on GitHub.

Community Python (Telethon)

Telethon is a popular asynchronous MTProto library for Python. It can be installed using pip, the standard Python package manager.

pip install telethon

For more advanced usage, including optional dependencies or development versions, refer to the Telethon installation guide.

Community Go (gotd)

gotd is a Go-based MTProto client library. It can be added to a Go project using the go get command.

go get github.com/gotd/td/telegram

Further setup and usage examples are available in the gotd GitHub repository.

Quickstart example

The following quickstart examples demonstrate basic initialization and message sending using a popular community library (Telethon for Python) and outline the approach for TDLib (C++).

Python (Telethon)

This example demonstrates how to initialize a Telethon client, connect to Telegram, and send a message. Before running, ensure you have obtained api_id and api_hash from Telegram's developer portal.

import asyncio
from telethon.sync import TelegramClient

api_id = 1234567  # Replace with your actual API ID
api_hash = 'YOUR_API_HASH' # Replace with your actual API Hash
phone = '+1234567890' # Replace with your phone number

client = TelegramClient('session_name', api_id, api_hash)

async def main():
    await client.connect()

    if not await client.is_user_authorized():
        await client.send_code_request(phone)
        # You will be prompted to enter the code in the console
        # In a real application, handle this input securely
        await client.sign_in(phone, input('Enter the code: '))

    # Send a message to a specific user (using their username or entity ID)
    # Replace 'username_or_id' with the recipient's username or chat ID
    # For example, await client.send_message('me', 'Hello from Telethon!')
    # Or, await client.send_message(some_user_entity, 'Hello!')
    # For this example, sending to yourself for simplicity.
    await client.send_message('me', 'Hello from my custom Telegram client via Telethon!')
    print("Message sent!")

    await client.disconnect()

if __name__ == '__main__':
    asyncio.run(main())

This script connects to the Telegram network, prompts for authorization if needed, and sends a message to the authenticated user. More comprehensive examples and API usage details are available in the Telethon documentation.

C++ (TDLib)

A C++ quickstart with TDLib typically involves initializing the library, setting up a client, and handling updates. Due to the verbose nature of C++ and the event-driven architecture of TDLib, a full quickstart snippet is extensive. The core steps include:

  1. Include TDLib headers.
  2. Create a td::Client instance.
  3. Set up an update handler (callback function or message loop) to process incoming events from Telegram.
  4. Send requests using td::Client::send and handle responses.

An example of interacting with TDLib through its C++ interface involves sending authorization requests, processing updates, and sending messages. For a complete example, including error handling and event loops, developers are directed to the TDLib example folder on GitHub, which contains a basic console client showing these steps.

Community libraries

Beyond the official offerings, the Telegram developer community has created numerous libraries that provide alternative interfaces to the MTProto API. These libraries often aim to offer more language-idiomatic APIs, higher-level abstractions, or support for languages not covered by official SDKs. They abstract away the low-level MTProto protocol details, allowing developers to focus on application logic rather than network and cryptographic intricacies. This approach aligns with modern API client design, as discussed by sources like Google's API design guidelines for client libraries.

  • Python (Telethon): An asynchronous MTProto library for Python. Telethon supports a wide range of Telegram API features, including bots, user accounts, and channel management. Its asynchronous nature (using asyncio) makes it suitable for high-performance applications that need to handle many concurrent operations. Telethon is widely used for creating custom Telegram bots and user scripts due to its comprehensive features and active community. The official documentation for Telethon is available at docs.telethon.dev.
  • Go (gotd): A pure Go implementation of the Telegram MTProto protocol. gotd provides a robust and efficient way to interact with the Telegram API in Go applications. It handles the full MTProto stack, including encryption, sessions, and TL (Type Language) serialization. gotd is designed for performance and reliability, making it suitable for server-side applications and bots. Developers can find the project and examples on its GitHub repository.
  • Other Languages: Various other community-contributed libraries exist for languages such as JavaScript/TypeScript (e.g., gramjs, node-telegram-bot-api for bot API), PHP (e.g., php-mtproto), and others. These libraries often leverage existing cryptographic primitives and network libraries in their respective ecosystems to implement the MTProto specification. Developers should evaluate the maintenance status, feature completeness, and community support before integrating any third-party library into production systems.

When choosing a community library, it is important to review its documentation, community activity, and update frequency to ensure it remains compatible with the evolving Telegram API and MTProto protocol. Many community libraries focus on specific use cases, such as bot development (which often uses the simpler Telegram Bot API) or full-featured client replication.