The short answer
- 1Show the exportLoginToken token as a tg://login QR code.
- 2Tokens expire quickly; keep replacing the QR code while the screen is open.
- 3On updateLoginToken call exportLoginToken again to get success or a data center move.
In this article
What QR login is
Instead of typing a phone number and a code into a desktop or web app, the user scans a QR code on the screen with Telegram on a phone that is already logged in. If your app supports this, users never wait for a code. How it looks to users is in logging in to Telegram Desktop with a QR code.
The steps in your app (the side that logs in)
- Call
auth.exportLoginTokenwith your api_id and api_hash. If the app already has other accounts logged in, pass their IDs so they are excluded. The server returns a token and an expiry time. - Encode the token as base64url, build a
tg://login?token=…address, and show it on screen as a QR code. - The token expires quickly. Before the expiry time in the reply, call
exportLoginTokenagain and replace the QR code, for as long as the screen is open. - When the user scans and approves it on the phone, your app receives an
updateLoginTokenupdate. CallexportLoginTokenonce more. - If the answer is success (
loginTokenSuccess), you are logged in. If it tells you to move to another data center (loginTokenMigrateTo), connect to that data center and finish withauth.importLoginToken; see data centers and _MIGRATE errors.
If the account has Two-Step Verification, SESSION_PASSWORD_NEEDED arrives at this point and you continue with the password check; see the user login flow.
The scanning side (the phone)
An official app that scans the code calls auth.acceptLoginToken with that token to approve the login. If your own app offers "approve a login on another device", this is the method you use. Before approving, always show the user which app is trying to log in, from which device and from where.
Where it usually gets stuck
| Symptom | Cause and fix |
|---|---|
| The user scans and nothing happens | The QR code had already expired. Make sure you replace it before the expiry time |
| Approved, but login never completes | You did not call exportLoginToken again after updateLoginToken |
| Told to move to another data center | Call importLoginToken on that data center; it needs a new connection |
| Token error (400) | Expired or already used token; create a new QR code |
FAQ
How does QR login work in the Telegram API?
Your app gets a token from auth.exportLoginToken and shows it as a tg://login QR code; a logged-in phone scans it and approves with auth.acceptLoginToken.
Why does scanning my QR code do nothing?
The token had expired. Call exportLoginToken again before the expiry time and refresh the QR code.
What if the account has Two-Step Verification?
After the QR step you get SESSION_PASSWORD_NEEDED and must complete the password check with SRP.
Sources
At a glance
- 1Show
base64url token in tg://login?token=…, refreshed before expiry.
- 2Finish
loginTokenSuccess, or loginTokenMigrateTo then importLoginToken.
- 3Approve
acceptLoginToken; show app, device and location first.
Updated