Skip to content
TGWAY
Channels & Bots STEP 79 / 99

Telegram API Login Flow: From Code to Two-Step Verification

How a user logs in through the Telegram API: auth.sendCode, signIn or signUp, then SRP for Two-Step Verification. Code delivery types and test numbers.

  • Applies toDevelopers
  • LevelL4
  • Reading time4 min
  • Verified

The short answer

  1. 1auth.sendCode, then auth.signIn (or signUp for a new number), then SRP if 2FA is on.
  2. 2The server chooses how the code is delivered; show matching instructions.
  3. 3Develop against the test server and test numbers to avoid rate limits.
In this article
  1. Three steps, and one more
  2. The code can arrive several ways
  3. Use the test server and test numbers while developing
  4. Two-Step Verification: the password is never sent
  5. After login: sessions and logging out
  6. FAQ
  7. At a glance

Three steps, and one more

Logging a user account in through the Telegram API is the same thing the official apps do. With TDLib the library reports each stage to you as an authorization state; if you work with MTProto directly, you call these methods in order.

StepMethodWhat it does
1. Request a codeauth.sendCodeSends the phone number with your api_id and api_hash. The server replies with how the code was sent and a phone_code_hash
2. Enter the codeauth.signInLogs in with the code and the phone_code_hash
2a. New numberauth.signUpIf the reply says sign-up is needed, collect a name and register, including acceptance of the terms of service
3. Two-Step Verificationaccount.getPassword → auth.checkPasswordIf SESSION_PASSWORD_NEEDED comes back, prove the password with SRP

How to get an api_id and api_hash is in getting an api_id safely, and choosing between the APIs in the four Telegram APIs.

The code can arrive several ways

The server decides where to send the code and tells you which way in its reply. Show instructions that match, or users get lost.

DeliveryWhat to tell the user
AppLook for the official Telegram notification on another device that is already logged in
SMS or callWait for a text message or an automated voice call
Missed callThe last digits of the incoming number are the code
EmailCheck the login email address for the code
FragmentAnonymous +888 numbers receive their code on fragment.com

auth.resendCode asks for the code another way and auth.cancelCode gives up. When the server tells you the next method and how long to wait, do not ask again before then. There is also account.invalidateSignInCodes, which cancels codes when there are signs a user passed one on to someone else.

Use the test server and test numbers while developing

Requesting a real code every time you fix your login flow piles up requests and can get you rate-limited. The official documentation describes test numbers for the test data centers: they start with 99966, followed by the data center number and four random digits, and their login code is the data center number repeated five times. The test server's address is on your app's page at my.telegram.org.

Two-Step Verification: the password is never sent

For an account with Two-Step Verification, the cloud password is asked after the code. The password itself never goes to the server. You take the salts and parameters from account.getPassword, compute a value that proves you know the password, and send that to auth.checkPassword. This is the SRP method. It is long and easy to get wrong, so a library such as TDLib is the better choice; see building TDLib.

After login: sessions and logging out

The new session appears in the user's Devices list under your app's name. Tell users they can end it from there. When a session is ended, the next request fails with a 401 error such as AUTH_KEY_UNREGISTERED or SESSION_REVOKED; send the user back to the login screen. See reading API errors. To log out, call auth.logOut and delete the session file left on the device.

FAQ

What is phone_code_hash?

A value auth.sendCode returns that you must pass back to auth.signIn together with the code.

What does SESSION_PASSWORD_NEEDED mean?

The account has Two-Step Verification. Call account.getPassword and prove the password with SRP via auth.checkPassword.

How do I test login without real phone numbers?

Use the test data centers and their test numbers starting with 99966; the code is the data center number repeated five times.

Sources

Summary

At a glance

Telegram API Login Flow: From Code to Two-Step Verification — at a glance illustration
  1. 1Flow

    sendCode → signIn/signUp → getPassword → checkPassword.

  2. 2Delivery

    App, SMS, call, missed call, email or Fragment.

  3. 32FA

    SRP proves the password without sending it; use TDLib.

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 ›