<metaproperty="description"content="How to connect to the closest DC access point for faster interaction with the API, and things to watch out for when developing a client.">
<metaproperty="og:title"content="Working with Different Data Centers">
<metaproperty="og:description"content="How to connect to the closest DC access point for faster interaction with the API, and things to watch out for when developing a client.">
<divclass="dev_page_bread_crumbs"><ulclass="breadcrumb clearfix"><li><ahref="/api">API</a></li><iclass="icon icon-breadcrumb-divider"></i><li><ahref="/api/datacenter">Working with Different Data Centers</a></li></ul></div>
<h1id="dev_page_title">Working with Different Data Centers</h1>
<divid="dev_page_content"><p>The servers are divided into several data centers (hereinafter “DCs”) in different parts of the world.
A complete list of proxy access points for these DCs may be obtained using <ahref="/method/help.getConfig">help.getConfig</a>:</p>
<p>In this context, <strong>this_dc</strong> is the number of the current DC, <strong>dc_options</strong> is a list of all DCs available at the moment, each of which has an <strong>id</strong>, <strong>ip</strong>, and <strong>port</strong> for establishing a connection. Please note that <strong>ip</strong> and <strong>port</strong> may change frequently, based on proxy server load and the user's current location.<br>
Typically, each DC has at least one IPv4 and one IPv6 endpoint available. </p>
<p>To optimize client communication with the API, each client must use the connection to the closest access point for its main queries (sending messages, getting contacts, etc.). Therefore, knowing how to select a DC is required before communicating with the API.</p>
<p>The <ahref="/method/auth.sendCode">auth.sendCode</a> method is the basic entry point when registering a new user or authorizing an existing user. 95% of all redirection cases to a different DC will occur when invoking this method.</p>
<p>The client does not yet know which DC it will be associated with; therefore, it establishes an encrypted connection to a random address and sends its query to that address.
Having received a <strong>phone_number</strong> from a client, we can find out whether or not it is registered in the system. If it is, then, if necessary, instead of sending a text message, we request that it establish a connection with a different DC first (PHONE_MIGRATE_X error).
If we do not yet have a user with this number, we examine its IP-address. We can use it to identify the closest DC. Again, if necessary, we redirect the user to a different DC (NETWORK_MIGRATE_X error).</p>
<p>There are reserved phone number prefixes to test the correctness of the application's handling of redirects between DCs. Read more in <ahref="https://core.telegram.org/api/auth#test-phone-numbers">User Authorization</a> article.</p>
<p>A file saved by a user with <ahref="/method/upload.saveFilePart">upload.saveFilePart</a> will be available for direct download only from the DC where the query was executed. That is why each file has a <strong>dc_id</strong> parameter:</p>
<p>To download the file, an encrypted connection to DC <strong>dc_id</strong> must be established and used to execute the <ahref="/method/upload.getFile">upload.getFile</a> query.
If an attempt is made to download the file over a wrong connection, the FILE_MIGRATE_X error will be returned.</p>
<p>Please note that encryption keys are not copied between DCs; therefore, the process of establishing an encrypted connection is started from the very beginning for each new DC. An issued auth_key can be associated with the current authorized user by using an <ahref="#authorization-transfer">authorization transfer</a>.</p>
<p>During the process of working with the API, user information is accumulated in the DC with which the user is associated. This is the reason a user cannot be associated with a different DC by means of the client. However, in the future, during prolonged communication from an unusual location, we may decide that the user's data must be moved to a different DC. After some time, the data will be copied and the association will be updated. Once this happens, when executing any query transmitted to the old DC, the API will return the USER_MIGRATE_X error. The client will then have to establish a connection with the new DC and repeat the query.</p>
<p><ahref="/method/auth.exportAuthorization">auth.exportAuthorization</a> must be executed in the current DC (the DC with which a connection has already been established), passing in <strong>dc_id</strong> as the value for the new DC. The method should return the user identifier and a long string of random data. An import operation can be performed at the new DC by sending it what was received. Queries requiring authorization can then be successfully executed in the new DC.</p></div>
</div>
</div>
</div>
<divclass="footer_wrap">
<divclass="footer_columns_wrap footer_desktop">
<divclass="footer_column footer_column_telegram">
<h5>Telegram</h5>
<divclass="footer_telegram_description"></div>
Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.