<p>When working with the API, it is sometimes necessary to send a relatively large file to the server. For example, when sending a message with a photo/video attachment or when setting the current user's profile picture.</p>
<p>The file's binary content is then split into parts. All parts must have the same size (<strong>part_size</strong>) and the following conditions must be met:</p>
<p>The following <ahref="/api/config#client-configuration">appConfig fields</a> specify the maximum number of uploadable file parts: </p>
<ul>
<li><ahref="/api/config#upload-max-fileparts-default">upload_max_fileparts_default »</a> - Maximum number of file parts uploadable by non-Premium users. </li>
<li><ahref="/api/config#upload-max-fileparts-premium">upload_max_fileparts_premium »</a> - Maximum number of file parts uploadable by Premium users. </li>
</ul>
<p>Note that the limit of uploadable file parts does not account for the <strong>part_size</strong>: thus the total file size limit can only be reached with the biggest possible <strong>part_size</strong> of 512KB, which is actually the recommended <strong>part_size</strong> to avoid excessive protocol overhead. </p>
<p>Each part should have a sequence number, <strong>file_part</strong>, with a value ranging from 0 to the value of the appropriate config parameter minus 1. </p>
<p>After the file has been partitioned you need to choose a method for saving it on the server. Use <ahref="/method/upload.saveBigFilePart">upload.saveBigFilePart</a> in case the full size of the file is more than <strong>10 MB</strong> and <ahref="/method/upload.saveFilePart">upload.saveFilePart</a> for smaller files.</p>
<p>Each call saves a portion of the data in a temporary location on the server to be used later. The storage life of each portion of data is between several minutes and several hours (depending on how busy the storage area is). After this time, the file part will become unavailable. To increase the time efficiency of a file save operation, we recommend using a call queue, so X pieces of the file are being saved at any given moment in time. Each successful operation to save a part invokes the method call to save the next part. The value of X can be tuned to achieve maximum performance.</p>
<p>When using one of the methods mentioned above to save file parts, one of the following <ahref="/api/errors#400-bad-request">data input errors</a> may be returned:</p>
<p>While the parts are being uploaded, an <ahref="https://en.wikipedia.org/wiki/MD5">MD5 hash</a> of the file contents can also be computed to be used later as the <strong>md5_checksum</strong> parameter in the <ahref="/constructor/inputFile">inputFile</a> constructor (since it is checked only by the server, for encrypted secret chat files it must be generated from the encrypted file).<br>
After the entire file is successfully saved, the final method may be called and passed the generated <ahref="/type/InputFile">inputFile</a> object. In case the <ahref="/method/upload.saveBigFilePart">upload.saveBigFilePart</a> method is used, the <ahref="/constructor/inputFileBig">inputFileBig</a> constructor must be passed, in other cases use <ahref="/constructor/inputFile">inputFile</a>.</p>
<ul>
<li><ahref="/method/messages.sendMedia">messages.sendMedia</a> - Sends a media file to a chat</li>
<li><ahref="/method/messages.uploadMedia">messages.uploadMedia</a> - Uploads a media file to a chat, without sending it, returning only a <ahref="/type/MessageMedia">MessageMedia</a> constructor that can be used to later send the file to multiple chats, without reuploading it every time. </li>
<li><ahref="/method/photos.uploadProfilePhoto">photos.uploadProfilePhoto</a> - Used to set a <ahref="#uploading-profile-or-chat-pictures">profile or chat picture or video</a></li>
</ul>
<p>The file save operation may return one of the following <ahref="/api/errors#400-bad-request">data input errors</a>:</p>
<p>Telegram allows grouping photos into <ahref="https://telegram.org/blog/albums-saved-messages">albums</a> and generic files (audio, docuemnts) into media groups. </p>
<p>To do this, <ahref="/method/messages.sendMultiMedia">messages.sendMultiMedia</a> is used, wrapping each <ahref="/type/InputMedia">InputMedia</a> constructor (uploaded or pre-existing, maximum 10 per media group) into an <ahref="/constructor/inputSingleMedia">inputSingleMedia</a> constructor, optionally providing a custom per-file caption in <code>message</code>. </p>
<p>For photo albums, clients should display an album caption only if exactly one photo in the group has a caption, otherwise no album caption should be displayed, and only when viewing in detail a specific photo of the group the caption should be shown.<br>
Other grouped media can display a caption under each file. </p>
<p>For some types of documents like GIFs, <ahref="/method/messages.getDocumentByHash">messages.getDocumentByHash</a> can be used to search for the document on Telegram servers.
The SHA256 hash of the file is computed, and it is passed along with the file's mime type and size to the method: if the file type is correct and the file is found, a <ahref="/constructor/document">document</a> is returned.</p>
<h3><aclass="anchor"href="#uploading-profile-or-chat-pictures"id="uploading-profile-or-chat-pictures"name="uploading-profile-or-chat-pictures"><iclass="anchor-icon"></i></a>Uploading profile or chat pictures</h3>
<p>User profile pictures can be uploaded using the <ahref="/method/photos.updateProfilePhoto">photos.uploadProfilePhoto</a> method: the actual profile picture has to be <ahref="#uploading-files">uploaded as for normal files</a>.<br>
<ahref="/method/photos.updateProfilePhoto">photos.uploadProfilePhoto</a> can also be used to reupload previously uploaded profile pictures. </p>
<p>The optional <code>bot</code> flag can contain info of a <ahref="/api/bots">bot</a> we own, to change the profile photo of that bot, instead of the current user.</p>
<p>Animated profile pictures are also supported, by populating the <code>video</code> flag: square MPEG4 videos up to <code>1080x1080</code> are supported, <code>800x800</code> is the recommended resolution.<br>
The <code>video_start_ts</code> is a floating point UNIX timestamp in seconds, indicating the frame of the video that should be used as static preview. </p>
<p><ahref="/api/channel">Chat, channel and supergroup</a> profile photos and videos can be uploaded using <ahref="/method/messages.editChatPhoto">messages.editChatPhoto</a> (<ahref="/api/channel#basic-groups">basic groups</a>) or <ahref="/method/channels.editPhoto">channels.editPhoto</a> (<ahref="/api/channel">channels, supergroups</a>).<br>
<p><ahref="/api/stickers">Sticker</a> and <ahref="/api/custom-emoji">custom emoji sticker</a>-based profile pictures are also supported, by populating the <code>video_emoji_markup</code> flag with either a <ahref="/constructor/videoSizeStickerMarkup">videoSizeStickerMarkup</a> or a <ahref="/constructor/videoSizeEmojiMarkup">videoSizeEmojiMarkup</a> constructor. </p>
<p>The profile picture should be rendered by placing the sticker at the center of a square canvas, in such a way that it occupies at most 67% of it. The background of the canvas is generated from <code>background_colors</code>, which contains a vector of 1, 2, 3 or 4 RBG-24 colors used to generate a solid (1), gradient (2) or freeform gradient (3, 4) background, similar to how <ahref="/api/wallpapers#fill-types">fill wallpapers</a> are generated. The rotation angle for gradient backgrounds is 0.<br>
If animated or video stickers/custom emojis are used, the <code>video_start_ts</code> flag can contain a floating point UNIX timestamp in seconds, indicating the frame of the profile picture that should be used as static preview. </p>
<p><ahref="/method/account.getDefaultProfilePhotoEmojis">account.getDefaultProfilePhotoEmojis</a> may be used to fetch a list of suggested <ahref="/api/custom-emoji">custom emojis</a> that can be used as profile pictures even by non-premium accounts; <ahref="/method/account.getDefaultGroupPhotoEmojis">account.getDefaultGroupPhotoEmojis</a> is the counterpart for group profile pictures.</p>
<p>There are methods available to download files which have been successfully uploaded. The schema of the types and methods used is presented below:</p>
<p>For photos, <ahref="/constructor/inputPhotoFileLocation">inputPhotoFileLocation</a> is used:</p>
<ul>
<li><code>id</code>, <code>file_reference</code> and <code>access_hash</code> taken from the <ahref="/constructor/photo">photo</a> constructor</li>
<li><code>thumb_size</code> taken from the <code>type</code> field of the desired <ahref="/type/PhotoSize">PhotoSize</a> of the <ahref="/constructor/photo">photo</a></li>
</ul>
</li>
<li>
<p>For profile pictures of users, channels, supergroups and groups, <ahref="/constructor/inputPeerPhotoFileLocation">inputPeerPhotoFileLocation</a> has to be used:</p>
<ul>
<li><code>peer</code> is the identifier of the peer whose photo we want to download</li>
<li><code>big</code> is used to choose whether to download the full-resolution picture, or just the thumbnail</li>
<li><code>photo_id</code> is extracted from the <ahref="/constructor/chatPhoto">chatPhoto</a> or <ahref="/constructor/userProfilePhoto">userProfilePhoto</a> of the desired profile photo</li>
</ul>
</li>
<li>
<p>For documents, <ahref="/constructor/inputDocumentFileLocation">inputDocumentFileLocation</a> is used:</p>
<ul>
<li><code>id</code>, <code>file_reference</code> and <code>access_hash</code> taken from the <ahref="/constructor/document">document</a> constructor</li>
<li>If downloading the thumbnail of a document, <code>thumb_size</code> should be taken from the <code>type</code> field of the desired <ahref="/type/PhotoSize">PhotoSize</a> of the <ahref="/constructor/photo">photo</a>; otherwise, provide an empty string.</li>
</ul>
</li>
<li>
<p>For previews of sticker sets, <ahref="/constructor/inputStickerSetThumb">inputStickerSetThumb</a> is used (note: to download stickers and previews of stickers use the document method described above for documents):</p>
<ul>
<li><code>stickerset</code> is set to the <ahref="/type/InputStickerSet">InputStickerSet</a> constructor generated from <ahref="/constructor/stickerSet">stickerSet</a></li>
<li><code>thumb_version</code> is copied from the same field in <ahref="/constructor/stickerSet">stickerSet</a></li>
</ul>
</li>
<li>
<p>For encrypted secret chat and telegram passport documents, respectively <ahref="/constructor/inputEncryptedFileLocation">inputEncryptedFileLocation</a> and <ahref="/constructor/inputSecureFileLocation">inputSecureFileLocation</a> have to be used, with parameters extracted from <ahref="/constructor/encryptedFile">encryptedFile</a> and <ahref="/constructor/secureFile">secureFile</a> (<ahref="/passport">passport docs</a>).</p>
</li>
<li>
<p>For livestream chunks, <ahref="/constructor/inputGroupCallStream">inputGroupCallStream</a> is used:</p>
<ul>
<li><code>call</code> contains the related group call ID+access hash, taken from the <ahref="/constructor/groupCall">groupCall</a> constructor.</li>
<li><code>time_ms</code> specifies the timestamp to fetch</li>
<li><code>scale</code> specifies the duration of the video segment to fetch in milliseconds, by bitshifting <code>1000</code> to the right <code>scale</code> times: <code>duration_ms := 1000 >> scale</code></li>
<li><code>video_channel</code> specifies the video channel to fetch</li>
<li><code>video_quality</code> specifies the selected video quality (0 = lowest, 1 = medium, 2 = best)</li>
<p>For old <strong>deprecated</strong> photos, if the client has cached some old fileLocations with the <strong>deprecated</strong><code>secret</code> identifier, <ahref="/constructor/inputFileLocation">inputFileLocation</a> or <ahref="/constructor/inputPhotoLegacyFileLocation">inputPhotoLegacyFileLocation</a> is used (this is mainly used for backwards compatibility with bot API file IDs, all user clients must use the modern <ahref="/constructor/inputPhotoFileLocation">inputPhotoFileLocation</a> file IDs): </p>
<li>All fields are taken from the previously cached fileLocation except for <code>file_reference</code>, <code>access_hash</code> and <code>id</code>, which are taken from the <ahref="/constructor/photo">photo</a> constructor (the last two fields are used only if available, in which case <ahref="/constructor/inputPhotoLegacyFileLocation">inputPhotoLegacyFileLocation</a> is used instead of <ahref="/constructor/inputFileLocation">inputFileLocation</a>).</li>
</ul>
</li>
</ul>
<p>The size of each file in bytes is available, which makes it possible to download the file in parts using the parameters <strong>offset</strong> and <strong>limit</strong>, similar to the way files are uploaded.</p>
<p>If <strong>precise</strong> flag is not specified, then </p>
<ul>
<li>The parameter <strong>offset</strong> must be divisible by 4 KB.</li>
<li>The parameter <strong>limit</strong> must be divisible by 4 KB.</li>
<li>1048576 (1 MB) must be divisible by <strong>limit</strong>.</li>
</ul>
<p>If <strong>precise</strong> is specified, then</p>
<ul>
<li>The parameter <strong>offset</strong> must be divisible by 1 KB.</li>
<li>The parameter <strong>limit</strong> must be divisible by 1 KB.</li>
<li><strong>limit</strong> must not exceed 1048576 (1 MB).</li>
</ul>
<p>In any case the requested part should be within one 1 MB chunk from the beginning of the file, i. e.</p>
<p>When downloading multiple files in parallel from the same DC, clients should limit the parallelism to download at most <code>small_queue_max_active_operations_count</code>/<code>large_queue_max_active_operations_count</code> files in parallel when downloading files smaller/bigger than 20MB (<ahref="/api/config#client-configuration">client configuration parameters »</a>). </p>
<p>The file download operation may return a <code>FILE_REFERENCE_EXPIRED</code> error (or another error starting with <code>FILE_REFERENCE_</code>): in this case, the <code>file_reference</code> field of the input location must be <ahref="/api/file_reference">refreshed</a>.
The file download operation may return an <ahref="/constructor/upload.fileCdnRedirect">upload.fileCdnRedirect</a> constructor: in this case, <ahref="/cdn">these</a> instructions must be followed for downloading CDN files.
The file download operation may also return one of the following <ahref="/api/errors#400-bad-request">data input errors</a>:</p>
<ul>
<li>FILE_ID_INVALID: The file address is invalid</li>
<li>OFFSET_INVALID: The <strong>offset</strong> value is invalid</li>
<li>LIMIT_INVALID: The <strong>limit</strong> value is invalid</li>
<li>FILE_MIGRATE_X: The file is in the datacenter No. X</li>
<p>In order to confirm the integrity of the downloaded file, clients are recommended to verify hashes for each downloaded part, as for <ahref="/cdn">CDN DCs</a>.
<ahref="/method/upload.getFileHashes">upload.getFileHashes</a> contain <ahref="/type/FileHash">FileHash</a> constructors. Each of these constructors contains the SHA-256 hash of a part of the file that starts with <code>offset</code> and takes <code>limit</code> bytes.</p>
<p>Before saving each portion of the data received from the DC into the file, the client can confirm that its hash matches the hash that was received from the master DC. If missing a hash for any file part, client developers must use the <ahref="/method/upload.getFileHashes">upload.getFileHashes</a> method to obtain the missing hash.</p>
<h4><aclass="anchor"href="#handling-audio-video-and-vector-previews"id="handling-audio-video-and-vector-previews"name="handling-audio-video-and-vector-previews"><iclass="anchor-icon"></i></a>Handling audio, video and vector previews</h4>
<p>A <ahref="/constructor/photoStrippedSize">photoStrippedSize</a> (with type <code>i</code>) is an extremely low-res thumbnail, embedded directly inside media location objects.<br>
It should be shown to the user in chat message previews, or while still downloading the most appropriately sized <ahref="/constructor/photoSize">photoSize</a> through the media DCs as <ahref="#downloading-files">described above</a>. </p>
<p>The stripped <code>bytes</code> payload should be inflated to a JPG payload as seen <ahref="https://github.com/telegramdesktop/tdesktop/blob/1757dd856b84d23f83d4e562c94dde825f6eb40c/Telegram/SourceFiles/ui/image/image.cpp#L43">here »</a>.</p>
<p>Messages with <ahref="/api/stickers#animated-stickers">animated</a>, <ahref="/api/stickers#video-stickers">video</a>, <ahref="/api/stickers#static-stickers">static</a> stickers can have a compressed svg (< 300 bytes) to show the outline of the sticker before fetching the actual sticker.
Sticker outlines will have a <code>j</code> type <ahref="/constructor/photoPathSize">photoPathSize</a> thumbnail. </p>
<p>This specific vector thumbnail consists of an <ahref="https://developer.mozilla.org/en-US/docs/Web/SVG/Tutorial/Paths">SVG path</a>, specially encoded to save space.<br>
<p><code>path</code> will contain the actual SVG path that can be directly inserted in the <code>d</code> attribute of an <ahref="https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path">svg <path> element</a>: </p>
<p>A <ahref="/constructor/videoSize">videoSize</a> constructor is typically used for <ahref="#animated-profile-pictures">animated profile pictures</a> and video previews.</p>
<p>Remote HTTP files sent by inline bots <ahref="/type/BotInlineResult">in response to inline queries</a> and in other places are represented by <ahref="/type/WebDocument">WebDocument</a> constructors.
When forwarding such remote HTTP files, they should be sent using <ahref="/type/InputMedia">external InputMedia constructors</a>.
Remote HTTP files can only be downloaded directly by the client if contained in a <ahref="/constructor/webDocumentNoProxy">webDocumentNoProxy</a> constructor: in this case, the file is deemed safe to download (this is the case for HTTPS files from certain trusted domains).</p>
<p>However, if the remote file is contained in a <ahref="/constructor/webDocument">webDocument</a>, to avoid leaking sensitive information the file must be downloaded through telegram's servers.
This can be done in a manner similar to <ahref="#downloading-files">normal files</a>, with the difference that <ahref="/method/upload.getWebFile">upload.getWebFile</a> must be used, instead. </p>
<p>The <ahref="/method/upload.getWebFile">upload.getWebFile</a> method is also used to generate preview pictures for maps and download music file covers, as follows. </p>
<p><strong>Note</strong>: the <ahref="/method/upload.getWebFile">upload.getWebFile</a> query must be sent to the DC specified in the <code>webfile_dc_id</code><ahref="/api/config#mtproto-configuration">MTProto configuration field</a>. </p>
<li><ahref="/constructor/inputWebFileLocation">inputWebFileLocation</a> is simply generated by taking the <code>url</code> and <code>access_hash</code> fields of the <ahref="/constructor/webDocument">webDocument</a> constructor.</li>
<li><ahref="/constructor/inputWebFileGeoPointLocation">inputWebFileGeoPointLocation</a> is used to download a server-generated image with the map preview from a <ahref="/constructor/geoPoint">geoPoint</a>.<ul>
<li><code>geo_point</code> is generated from the <code>lat</code>, <code>long</code><code>accuracy_radius</code> parameters of the <ahref="/constructor/geoPoint">geoPoint</a></li>
<li><code>access_hash</code> is the access hash of the <ahref="/constructor/geoPoint">geoPoint</a></li>
<li><code>w</code> - Map width in pixels before applying scale; 16-1024</li>
<li><code>h</code> - Map height in pixels before applying scale; 16-1024</li>
<li><ahref="/constructor/inputWebFileAudioAlbumThumbLocation">inputWebFileAudioAlbumThumbLocation</a> is used to download an album cover with <code>600x600</code> resolution for any music file missing embedded album art.
Note that the <code>document</code> field containing the music file must NOT be provided in secret chats: the locally extracted <code>title</code> and <code>performer</code> fields should be provided, instead.<br>
In normal chats <code>document</code> should always be provided instead of <code>title</code> and <code>performer</code>, as it has more lax flood limits. <ul>
<li><code>small</code> if set, downloads a <code>100x100</code> thumbnail instead.</li>
<li><code>document</code> contains the music file: MUST NOT be provided in secret chats.</li>
<li><code>title</code> contains the song title: should only be provided in secret chats.</li>
<li><code>performer</code> contains the song performer: should only be provided in secret chats.</li>
<p>It is recommended that large queries (<ahref="/method/upload.getFile">upload.getFile</a>, <ahref="/method/upload.saveFilePart">upload.saveFilePart</a>, <ahref="/method/upload.getWebFile">upload.getWebFile</a>) be handled through one or more separate sessions and separate connections, in which no methods other than these should be executed.<br>
This way, data transfer will cause less interference with <ahref="/api/updates">getting updates</a> and other method calls.<br>
If a media DC with the required DC ID is available (<ahref="/constructor/dcOption">dcOption</a> will have the <code>media</code> flag set), queries must be sent to that DC. </p>