<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>
<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 is 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 logout tokens are used: </p>
<p>When invoking <ahref="/method/auth.logOut">auth.logOut</a> on a previously authorized session with 2FA enabled, the server may return a <code>future_auth_token</code>, which should be stored in the local database.<br>
When invoking <ahref="/method/auth.sendCode">auth.sendCode</a>, all logout tokens present in the database should be provided to <code>codeSettings.logout_tokens</code>.<br>
If any of the logout tokens matches the account we're trying to login into and the token hasn't expired, <ahref="/method/auth.sendCode">auth.sendCode</a> will directly return a <code>SESSION_PASSWORD_NEEDED</code> RPC error, directly asking the user to <ahref="#2fa">enter the 2FA password</a>, without sending any authorization code. </p>
<p>Otherwise, the system will send an authorization code using the following logic:</p>
<li><ahref="/constructor/auth.sentCodeTypeCall">Phone call</a>: a synthesized voice will tell the user which verification code to input.</li>
<li><ahref="/constructor/auth.sentCodeTypeFlashCall">Flash phone call</a>: the code will be sent via a flash phone call, that will be closed immediately.
In the last 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">Missed phone call</a>: the code will be sent via a flash phone call, that will be closed immediately. The last digits of the phone number that calls are the code that must be entered manually by the user.</li>
<p>The <ahref="/method/auth.sendCode">auth.sendCode</a> method also 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>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>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>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>