<p>Authorization is associated with a client's encryption key identifier: <strong>auth_key_id</strong>. No additional parameters need to be passed into methods following authorization. </p>
<p>To log in as a <ahref="/bots">bot</a>, follow <ahref="/api/bots">these instructions»</a>.</p>
<p>An alternative <ahref="/api/qr-login">QR code-based login flow is also available»</a>.</p>
<h3><aclass="anchor"href="#sending-a-verification-code"id="sending-a-verification-code"name="sending-a-verification-code"><iclass="anchor-icon"></i></a>Sending a verification code</h3>
<p>To show a nicely formatted and validated phone number field, the <ahref="/constructor/help.countriesList">help.countriesList</a> constructor can be obtained using the <ahref="/method/help.getCountriesList">help.getCountriesList</a> method.<br>
The <ahref="/constructor/help.countriesList">help.countriesList</a> config and other configuration values are then used as described <ahref="/api/config#country-information-and-login-phone-patterns">here»</a>. </p>
<p>Then, a text message containing an authorization code is sent to the user's phone using <ahref="/method/auth.sendCode">auth.sendCode</a>.<br>
However, this is not always the case, if future auth tokens are used: </p>
<p>When invoking <ahref="/method/auth.logOut">auth.logOut</a> on a previously authorized session, the server may return a <code>future_auth_token</code>, which should be stored in the local database.<br>
A <code>future_auth_token</code> is also contained in the <ahref="/constructor/auth.authorization">auth.authorization</a> returned when logging in.<br>
At all times, the future auth token database should contain at most 20 tokens: evict older tokens as new tokens are added to stay below this limit.<br>
When invoking <ahref="/method/auth.sendCode">auth.sendCode</a>, all future auth tokens present in the database should be provided to <code>codeSettings.logout_tokens</code>.<br>
If any of the future auth tokens matches the account we're trying to login into and the token hasn't expired:</p>
<li>If <ahref="/api/srp">2FA</a> is not enabled, <ahref="/method/auth.sendCode">auth.sendCode</a> will is directly return an <ahref="/constructor/auth.sentCodeSuccess">auth.sentCodeSuccess</a> constructor with session info, indicating the session is authorized.</li>
<li>If <ahref="/api/srp">2FA</a> is enabled, <ahref="/method/auth.sendCode">auth.sendCode</a> will return a <code>SESSION_PASSWORD_NEEDED</code> RPC error, asking the user to <ahref="#2fa">enter the 2FA password</a>, without sending any authorization code. </li>
<p>The <ahref="/method/auth.sendCode">auth.sendCode</a> method has parameters for enabling/disabling use of flash calls and missed calls, and allows passing an SMS token that will be included in the sent SMS.
For example, the latter is required in newer versions of android, to use the <ahref="https://developers.google.com/identity/sms-retriever/overview">android SMS receiver APIs</a>.</p>
<td>Phone code type that will be sent next, if the phone code is not received within <code>timeout</code> seconds: to send it use <ahref="/method/auth.resendCode">auth.resendCode</a></td>
<p>The system will automatically choose how to send the authorization code; there are multiple possible ways the code can arrive, signaled to the client via the <code>type</code> field of the <ahref="/type/auth.SentCodeType">auth.SentCodeType</a> constructor. </p>
<p>Note that in some conditions when signing up or logging in using an SMS code/call, only the <ahref="/constructor/auth.sentCodeTypeFirebaseSms">auth.sentCodeTypeFirebaseSms</a> code type may be used.<br>
Currently, only official apps can make use of Firebase SMS authentication: this means that in some conditions, only the official applications can receive a login/signup code via SMS/call.<br>
Third-party apps may log in using any of the other code delivery methods (Telegram codes, Fragment codes, email codes, future auth tokens, <ahref="/api/qr-login">QR codes</a>).</p>
<li><ahref="/constructor/auth.sentCodeTypeSetUpEmailRequired">auth.sentCodeTypeSetUpEmailRequired</a>: if the user logins often enough, Telegram will ask the user to verify an email that will be used to send the login code.<br>
See <ahref="#email-verification">here»</a> for more info on the verification process.</li>
<li><ahref="/constructor/auth.sentCodeTypeEmailCode">auth.sentCodeTypeEmailCode</a>: the code was sent to the configured login email.</li>
<li><ahref="/constructor/auth.sentCodeTypeFragmentSms">auth.sentCodeTypeFragmentSms</a>: the code was sent via <ahref="https://fragment.com">fragment.com</a>: open the specified <code>url</code> to log into the <ahref="https://fragment.com">Fragment</a> platform with your wallet and view the code. </li>
<li><ahref="/constructor/auth.sentCodeTypeApp">auth.sentCodeTypeApp</a>: the code was sent as a Telegram service notification to all other logged-in sessions.</li>
<li><ahref="/constructor/auth.sentCodeTypeFirebaseSms">auth.sentCodeTypeFirebaseSms</a>: firebase login flow, only for official apps. <ul>
<li>On Android, can only be received if the <ahref="/constructor/codeSettings">codeSettings</a>.<code>allow_firebase</code> flag is set.<br>
The client must pass the received <ahref="/constructor/auth.sentCodeTypeFirebaseSms">auth.sentCodeTypeFirebaseSms</a>.<code>nonce</code>/<code>play_integrity_nonce</code> to the <ahref="https://developer.android.com/training/safetynet/attestation">SafetyNet Attestation API</a>/<ahref="https://developer.android.com/google/play/integrity/overview">Google Play Integrity API</a>, and then pass the obtained JWS object to <ahref="/method/auth.requestFirebaseSms">auth.requestFirebaseSms</a>.<code>safety_net_token</code>/<code>play_integrity_token</code>, along with the <code>phone_number</code> and the <code>phone_code_hash</code>.<br>
If the method returns <ahref="/constructor/boolTrue">boolTrue</a>, the code will be sent via SMS; otherwise, the <code>next_type</code> authentication method must be used, with <ahref="/method/auth.resendCode">auth.resendCode</a>.<br>
The <code>next_type</code> authentication method must also be used if the device integrity verification failed, and no token could be obtained to invoke <ahref="/method/auth.requestFirebaseSms">auth.requestFirebaseSms</a>: in this case, the device integrity verification failure reason must be passed to <ahref="/method/auth.resendCode">auth.resendCode</a> in <code>reason</code>. </li>
<li>On iOS, can only be received if the device token for Apple Push was passed to <ahref="/constructor/codeSettings">codeSettings</a>.<code>token</code>.<br>
The client then waits for a new push notification for <ahref="/constructor/auth.sentCodeTypeFirebaseSms">auth.sentCodeTypeFirebaseSms</a>.<code>push_timeout</code> seconds.<br>
If a push notification isn't received within <code>push_timeout</code> seconds, the <code>next_type</code> authentication method must be used, with <ahref="/method/auth.resendCode">auth.resendCode</a>.<br>
If a push notification is received with <code>receipt</code> and <code>ios_push_secret</code> fields, and the value of the <code>receipt</code> field matches <ahref="/constructor/codeSettings">codeSettings</a>.<code>receipt</code>, the value of <code>ios_push_secret</code> is passed to <ahref="/method/auth.requestFirebaseSms">auth.requestFirebaseSms</a>.<code>ios_push_secret</code>, along with the <code>phone_number</code> and the <code>phone_code_hash</code>.<br>
If the method returns <ahref="/constructor/boolTrue">boolTrue</a>, the code will be sent via SMS; otherwise, the <code>next_type</code> authentication method must be used, with <ahref="/method/auth.resendCode">auth.resendCode</a>.<br>
The <code>next_type</code> authentication method must also be used if the device integrity verification failed, and no secret could be obtained to invoke <ahref="/method/auth.requestFirebaseSms">auth.requestFirebaseSms</a>: in this case, the device integrity verification failure reason must be passed to <ahref="/method/auth.resendCode">auth.resendCode</a> in <code>reason</code>. </li>
<li><ahref="/constructor/auth.sentCodeTypeSmsWord">auth.sentCodeTypeSmsWord</a>: the code was sent via SMS containing a single word, which is the SMS code to use.<br>
The <code>beginning</code> flag, if set, contains the first letter of the secret word. </li>
<li><ahref="/constructor/auth.sentCodeTypeSmsPhrase">auth.sentCodeTypeSmsPhrase</a>: the code was sent via SMS containing a phrase with multiple words, which are the SMS code to use.<br>
The <code>beginning</code> flag, if set, contains the first word of the secret phrase. </li>
<li><ahref="/constructor/auth.sentCodeTypeCall">auth.sentCodeTypeCall</a>: the user will receive a phone call and a synthesized voice will tell the user the verification code to input.</li>
<li><ahref="/constructor/auth.sentCodeTypeFlashCall">auth.sentCodeTypeFlashCall</a>: the code will be sent via a flash phone call, that will be closed immediately.<br>
In this case, the phone code will then be the phone number itself, just make sure that the phone number matches the specified pattern (see <ahref="/constructor/auth.sentCodeTypeFlashCall">auth.sentCodeTypeFlashCall</a>).</li>
<li><ahref="/constructor/auth.sentCodeTypeMissedCall">auth.sentCodeTypeMissedCall</a>: the code will be sent via a flash phone call, that will be closed immediately.<br>
The last digits of the phone number that calls are the code that must be entered manually by the user.</li>
<p>If the message takes too long (<code>timeout</code> seconds) to arrive at the phone, the <ahref="/method/auth.resendCode">auth.resendCode</a> method may be invoked to resend a code of type <code>next_type</code>.
If the same happens again, you can use <ahref="/method/auth.resendCode">auth.resendCode</a> with the <code>next_type</code> returned by the previous call to <ahref="/method/auth.resendCode">auth.resendCode</a>.
To cancel the verification code use <ahref="/method/auth.cancelCode">auth.cancelCode</a>.</p>
<p>Telegram may return a <ahref="/constructor/auth.sentCodeTypeSetUpEmailRequired">auth.sentCodeTypeSetUpEmailRequired</a> code type in the <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor returned by <ahref="/method/auth.sendCode">auth.sendCode</a>.<br>
In this case, clients should ask the user to verify an email address that will be used to receive the login code as follows: </p>
<p>If the <code>google_signin_allowed</code> or <code>apple_signin_allowed</code> flags are set, users can directly verify their email with Google/Apple ID as specified <ahref="https://developers.google.com/identity/sign-in/android/sign-in">here (Google ID) »</a> and <ahref="https://developer.apple.com/documentation/sign_in_with_apple">here (Apple ID) »</a>.<br>
After obtaining the ID token, call <ahref="/method/account.verifyEmail">account.verifyEmail</a>, providing the following parameters:</p>
<li><code>purpose</code> - A <ahref="/constructor/emailVerifyPurposeLoginSetup">emailVerifyPurposeLoginSetup</a> constructor</li>
<li><code>purpose.phone_number</code> - The phone number used with <ahref="/method/auth.sendCode">auth.sendCode</a></li>
<li><code>purpose.phone_code_hash</code> - The phone code hash contained in the <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor returned by <ahref="/method/auth.sendCode">auth.sendCode</a></li>
<li><code>verification</code> - <ahref="/constructor/emailVerificationGoogle">emailVerificationGoogle</a> or <ahref="/constructor/emailVerificationApple">emailVerificationApple</a></li>
<li><code>verification.token</code> - The ID token returned by the Google ID API.</li>
<p>On success, the <ahref="/method/account.verifyEmail">account.verifyEmail</a> method will return a <ahref="/constructor/account.emailVerifiedLogin">account.emailVerifiedLogin</a> constructor with an <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor that should be handled <ahref="#code-types">as usual»</a>. </p>
<p>Otherwise, ask the user to enter an email address and then call <ahref="/method/account.sendVerifyEmailCode">account.sendVerifyEmailCode</a>, providing the following parameters:</p>
<li><code>purpose</code> - A <ahref="/constructor/emailVerifyPurposeLoginSetup">emailVerifyPurposeLoginSetup</a> constructor</li>
<li><code>purpose.phone_number</code> - The phone number used with <ahref="/method/auth.sendCode">auth.sendCode</a></li>
<li><code>purpose.phone_code_hash</code> - The phone code hash contained in the <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor returned by <ahref="/method/auth.sendCode">auth.sendCode</a></li>
</ul>
<p>Once the user receives and inputs the verification code, call <ahref="/method/account.verifyEmail">account.verifyEmail</a>, providing the following parameters:</p>
<ul>
<li><code>purpose</code> - A <ahref="/constructor/emailVerifyPurposeLoginSetup">emailVerifyPurposeLoginSetup</a> constructor</li>
<li><code>purpose.phone_number</code> - The phone number used with <ahref="/method/auth.sendCode">auth.sendCode</a></li>
<li><code>purpose.phone_code_hash</code> - The phone code hash contained in the <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor returned by <ahref="/method/auth.sendCode">auth.sendCode</a></li>
<p>On success, the <ahref="/method/account.verifyEmail">account.verifyEmail</a> method will return a <ahref="/constructor/account.emailVerifiedLogin">account.emailVerifiedLogin</a> constructor with an <ahref="/constructor/auth.sentCode">auth.sentCode</a> constructor that should be handled <ahref="#code-types">as usual»</a>. </p>
<p>If the user cannot access their email address, an email reset may be requested using <ahref="/method/auth.resetLoginEmail">auth.resetLoginEmail</a>. </p>
<p>To change the login email after login, pass <ahref="/constructor/emailVerifyPurposeLoginChange">emailVerifyPurposeLoginChange</a> as <code>purpose</code>, following the exact same Google ID/Apple ID/email code login flow as above: on success, the <ahref="/method/account.verifyEmail">account.verifyEmail</a> method will return an <ahref="/constructor/account.emailVerified">account.emailVerified</a> constructor.</p>
<p>When user enters verification code, the <ahref="/method/auth.signIn">auth.signIn</a> method must be used to validate it and possibly sign user in.</p>
<p>If the code was entered correctly, but the method returns <ahref="/constructor/auth.authorizationSignUpRequired">auth.authorizationSignUpRequired</a>, it means that account with this phone number doesn't exist yet: user needs to provide basic information, accept terms of service and then the new user registration method (<ahref="/method/auth.signUp">auth.signUp</a>) must be invoked.</p>
<p>When trying to sign in using <ahref="/method/auth.signIn">auth.signIn</a>, an <ahref="/method/auth.signIn#possible-errors">error 400 SESSION_PASSWORD_NEEDED</a> may be returned, if the user has two-factor authentication enabled.
In this case, instructions for <ahref="/api/srp">SRP 2FA authentication</a> must be followed.</p>
<p>When logging in, other logged-in sessions will receive an <ahref="/constructor/updateNewAuthorization">updateNewAuthorization</a> update.<br>
If the <code>unconfirmed</code> flag is set, clients should display a notification, asking the user if they recognize the session. </p>
<p>If the user clicks on the Yes button, invoke <ahref="/method/account.changeAuthorizationSettings">account.changeAuthorizationSettings</a> with the new session's <code>hash</code> and the <code>confirmed</code> flag set, confirming the specified session. </p>
<p>If the user clicks on the No button, invoke <ahref="/method/account.resetAuthorization">account.resetAuthorization</a> with the new session's <code>hash</code>, logging out the specified session. </p>
<p>If no action is taken by the user, the session will be autoconfirmed <code>authorization_autoconfirm_period</code> seconds after login (see the associated <ahref="/api/config#authorization-autoconfirm-period">client configuration parameter»</a>). </p>
<p>Telegram's servers will automatically invalidate login codes if they are sent by the user to another Telegram chat, either by forwarding them or by sending them inside of a message: however, clients should also manually and immediately invalidate login codes if the user attempts to screenshot or forward a message sent by the login notification service user (ID <code>777000</code>) containing login codes. </p>
<li>AND contains one or more login codes, defined as a sequence of 5 to 7 decimal digits, optionally interleaved with or followed by any number of <code>-</code> characters (<ahref="https://github.com/tdlib/td/blob/07c1d53a6d3cb1fad58d2822e55eef6d57363581/td/telegram/MessagesManager.cpp#L6513">example implementation»</a>)</li>
<p><ahref="/method/account.invalidateSignInCodes">account.invalidateSignInCodes</a> should be invoked, passing the extracted login <code>codes</code> (excluding any <code>-</code> characters). </p>
<p>Each phone number is limited to only a certain amount of logins per day (e.g. 5, but this is subject to change) after which the API will return a FLOOD error until the next day. This might not be enough for testing the implementation of User Authorization flows in client applications.</p>
<p>There are several reserved phone number prefixes for testing that your application handles redirects between DCs, sign up, sign in and 2FA flows correctly. These numbers are only available on <strong>Test DCs</strong> (their IP addresses for TCP transport are available in <ahref="https://my.telegram.org/apps">API development tools</a> panel after <ahref="https://core.telegram.org/api/obtaining_api_id#obtaining-api-id">api_id was obtained</a>, <ahref="https://core.telegram.org/mtproto/transports#uri-format">URI format</a> for HTTPS/WebSocket transport).</p>
<p>If you wish to emulate an application of a user associated with DC number X, it is sufficient to specify the phone number as <code>99966XYYYY</code>, where YYYY are random numbers, when registering the user. A user like this would always get XXXXX as the login confirmation code (the DC number, repeated five times). Note that the value of X must be in the range of 1-3 because there are only 3 Test DCs. When the flood limit is reached for any particular test number, just choose another number (changing the YYYY random part).</p>
<p>Do not store any important or private information in the messages of such test accounts; anyone can make use of the simplified authorization mechanism – and we periodically wipe all information stored there.</p>
<p>Proceed with User Authorization flows in <strong>Production DCs</strong> only after you make sure everything works correctly on <strong>Test DCs</strong> first to avoid reaching flood limits.</p>
<blockquote>
<p>To help you with working on production DCs, logins with the same phone number with which the <code>api_id</code> was registered have more generous flood limits.</p>
<h3><aclass="anchor"href="#we-are-authorized"id="we-are-authorized"name="we-are-authorized"><iclass="anchor-icon"></i></a>We are authorized</h3>
<p>As a result of authorization, the client key, <strong>auth_key_id</strong>, becomes associated with the user, and each subsequent API call with this key will be executed with that user's identity. The authorization method itself returns the relevant user. It is best to immediately store the User ID locally in a binding with the key.</p>