Skip to content
TGWAY
Channels & Bots STEP 84 / 99

Calling Telegram API Methods: Layers and initConnection

Wrap your first Telegram API request in invokeWithLayer and initConnection, send honest device details, and use invokeAfterMsg or invokeWithoutUpdates.

  • Applies toDevelopers
  • LevelL4
  • Reading time3 min
  • Verified

The short answer

  1. 1A layer is the API's version number; declaring it keeps older apps working.
  2. 2Wrap the first request: invokeWithLayer, then initConnection, then the real request.
  3. 3Use an honest device name; users see it in their Devices list.
In this article
  1. Layers: the API's version numbers
  2. Two wrappers around your first request
  3. Other wrapper methods
  4. Compress large requests
  5. What TDLib handles for you
  6. FAQ
  7. At a glance

Layers: the API's version numbers

The Telegram API's methods and data shapes (its schema) change as features are added. Each set of changes gets the next number in sequence, called a layer. When your app declares "I speak layer N", the server answers in that layer's shapes, so older apps keep working. How to read layer numbers is in reading the TL schema and layers.

Two wrappers around your first request

The first request you send after connecting is wrapped like this: the outer layer declares the layer, the next one introduces your app, and the innermost part is the request you actually wanted to make.

invokeWithLayer(layer = the layer your app uses,
  initConnection(app details…,
    help.getConfig()))   <- the request you actually wanted
Sent in initConnectionMeaning
api_idYour app's number from my.telegram.org; see getting an api_id
device_model, system_versionDevice name and OS version, shown in the user's Devices list
app_versionYour app's version
system_lang_code, lang_pack, lang_codeThe device language, and the translation pack and language your app uses

Send it again every time the app restarts and whenever these values change.

Other wrapper methods

WrapperWhen to use it
invokeAfterMsg / invokeAfterMsgsProcess this request only after an earlier one, keeping order without waiting for the answer
invokeWithoutUpdatesDo not receive updates on this connection, for example a file-only connection
invokeWithTakeoutMake requests inside a user data export (takeout) session

With guaranteed ordering, a request blocked for too long comes back with an error asking you to resend it, and if the earlier request failed you get an error saying so. Handle them by error name; see reading API errors.

Compress large requests

You can send a request compressed with gzip. The official documentation advises against compressing data that is already compressed, such as photos and videos, or very short messages.

What TDLib handles for you

TDLib sends the layer and initConnection for you; you only pass your app details as parameters. See building TDLib.

FAQ

What is a layer in the Telegram API?

A version number for the API schema. Your app declares the layer it uses, and the server answers in that version's shapes.

Why do I need initConnection?

It tells the server about your app and device; the first request after connecting must be wrapped in it.

Does TDLib handle layers for me?

Yes. TDLib sends the layer and initConnection; you only pass your app details.

Sources

Summary

At a glance

Calling Telegram API Methods: Layers and initConnection — at a glance illustration
  1. 1Layer

    Declared with invokeWithLayer on the first request.

  2. 2initConnection

    api_id, device, app version and language details.

  3. 3Wrappers

    invokeAfterMsg for order, invokeWithoutUpdates for file connections.

Share this guide

Send it to a friend who has the same question.

Telegram WhatsApp
More

Open on your phonePoint your phone camera here to open this guide.

Updated

Related articles

View all ›