<divclass="dev_page_bread_crumbs"><ulclass="breadcrumb clearfix"><li><ahref="/api">API</a></li><iclass="icon icon-breadcrumb-divider"></i><li><ahref="/api/bots%2Fids">Bot API dialog IDs</a></li></ul></div>
<h1id="dev_page_title">Bot API dialog IDs</h1>
<divid="dev_page_content"><!-- scroll_nav -->
<p>The <code>id</code> field of <ahref="/api/peers">peers»</a> (i.e. users, chats, channels, secret chats) contain four distinct sequences of unique 64-bit IDs used to identify a specific user, chat, channel or secret chat. </p>
<p>The ID sequences of users, chats, channels and secret chats <strong>overlap</strong>, so it is a good idea to transform the peer IDs to bot API dialog IDs as specified below. </p>
<p>A bot API dialog ID is a single, unique 64-bit peer ID sequence derived from the user, chat, channel and secret chat ID sequences, maintaining uniqueness across all of them.<br>
Bot API dialog IDs are already used in the <ahref="/bots/api">bot API</a> and in the underlying <ahref="https://github.com/tdlib/td">tdlib</a> library to identify peers. </p>
<p>As specified above, a bot API dialog ID may have more than 32 significant bits and some programming languages may have difficulty/silent defects in interpreting it. But, thanks to the ranges of the underlying MTProto IDs (specified below), it has at most 52 significant bits, so a 64-bit integer or even a double-precision float type are safe for storing this identifier. </p>
<p>More specifically, a bot API dialog ID ranges from <code>-2002147483648</code> to <code>1099511627775</code>. </p>
<p>It's a good idea to transform peer IDs to bot dialog API IDs even if you do decide to use separate databases to <ahref="/api/peers">store info about peers</a>, as it will make IDs more visually recognizable both for you and your users, as well as guarantee compatibility with the bot API, and allow your client to easily identify the type of a peer just by using its ID, thanks to the range checks specified below.</p>
<p><ahref="/constructor/user">User</a> IDs in the MTProto API range from <code>1</code> to <code>0xffffffffff</code> (inclusive). </p>
<p>MTProto user IDs are equal to bot API user dialog IDs and vice versa, you don't have to do anything to convert them, except validate that they fall within the range specified above. </p>
<p><ahref="/constructor/chat">Chat</a> IDs in the MTProto API range from <code>1</code> to <code>999999999999</code> (inclusive). </p>
<p>To convert MTProto chat IDs to bot API chat dialog IDs, make them negative (and vice versa). </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API chat dialog IDs, i.e. <code>-999999999999</code> to <code>-1</code> inclusively). </p>
<p><ahref="/constructor/channel">Supergroup/channel</a> IDs share the same sequence in the MTProto API, and they range from <code>1</code> to <code>997852516352</code> (inclusive). </p>
<p>To convert MTProto channel IDs to bot API channel dialog IDs, add <code>1000000000000</code> and make them negative (and vice versa). </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API channel dialog IDs, i.e. <code>-1997852516352</code> to <code>-1000000000001</code> inclusively). </p>
<p><ahref="/api/end-to-end">Secret chat IDs</a> in the MTProto API range from <code>-2147483648</code> to <code>2147483647</code> (inclusive, treat the secret chat ID as a signed little-endian 32-bit integer). </p>
<p>To convert MTProto chat IDs to bot API chat secret chat IDs, subtract <code>2000000000000</code>. </p>
<p>Before conversion, always validate that they fall within the range specified above (appropriately transformed for bot API secret chat dialog IDs, i.e. <code>-2002147483648</code> to <code>-1997852516353</code> inclusively). </p>
<p>Note: while the official instance of the bot API does not support secret chats, the underlying <ahref="https://github.com/tdlib/td/">tdlib</a> library does support them, and uses the format mentioned above for secret chat IDs.</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.