Skip to content
TGWAY
Channels & Bots STEP 85 / 99

Uploading and Downloading Files With the Telegram API

Upload Telegram API files in parts with saveFilePart or saveBigFilePart, follow the 1 KB and 512 KB size rules, and download with getFile in 1 MB ranges.

  • Applies toDevelopers
  • LevelL4
  • Reading time4 min
  • Verified

The short answer

  1. 1Up to 10 MB use saveFilePart; above that saveBigFilePart, in parts.
  2. 2Parts are multiples of 1 KB that divide 512 KB evenly.
  3. 3Download with getFile: multiples of 4 KB, never crossing a 1 MB boundary.
In this article
  1. Files move in parts
  2. Uploading a file in parts
  3. Downloading with upload.getFile
  4. Errors you will meet
  5. FAQ
  6. At a glance

Files move in parts

In the Telegram API a file is never sent in one go. You upload it in parts, and you download it part by part, choosing a position (offset) and a length (limit) each time. The official apps and TDLib do this for you; if you implement it yourself, the server only accepts parts that follow the size rules below.

Uploading a file in parts

File sizeMethodPassed at the end
Up to 10 MBupload.saveFilePartinputFile
Over 10 MBupload.saveBigFilePartinputFileBig
  1. Pick a random 64-bit number of your own as the file's ID (file_id).
  2. Choose a part size. It must be a multiple of 1 KB, and 512 KB must be divisible by it (512 KB, 256 KB, 128 KB and so on). Only the last part may be smaller.
  3. Upload the parts in order, numbering them from 0. The maximum number of parts comes from a server configuration value, and it differs between ordinary and Premium accounts.
  4. When every part is up, put that file ID in inputFile (or inputFileBig) and pass it to a request such as sending a message. For small files you can include the MD5 so the server can detect corruption.

An uploaded file can be downloaded straight away only from the data center it was uploaded to. To get it elsewhere, follow the FILE_MIGRATE error; see data centers and _MIGRATE errors.

Downloading with upload.getFile

ModeRules for offset and limit
DefaultBoth multiples of 4 KB, and 1 MB must be divisible by limit
precise onBoth multiples of 1 KB, with limit up to 1 MB
BothThe requested range must not cross a 1 MB boundary

Large files download faster over several separate connections. Set those connections not to receive updates; see layers and initConnection. For popular files the server may redirect you to a CDN; download from the CDN data center it names, and verify the hashes of the parts you receive.

Errors you will meet

ErrorWhat to do
FILE_MIGRATE_XDownload from data center X
FILE_REFERENCE_EXPIREDThe file reference has expired; fetch the original message again for a fresh one. See expired file references
FLOOD_WAIT_XWait X seconds; see reading API errors

Sending files through the Bot API needs none of these part rules: you upload over HTTP or reuse a file_id, but the Bot API has its own size limits.

FAQ

When should I use saveBigFilePart?

For files larger than 10 MB. Smaller files use saveFilePart.

What part size can I use for Telegram uploads?

A multiple of 1 KB that divides 512 KB evenly, such as 512 KB or 256 KB; only the last part may be smaller.

Why does getFile reject my offset?

In default mode offset and limit must be multiples of 4 KB and the range must not cross a 1 MB boundary.

Sources

Summary

At a glance

Uploading and Downloading Files With the Telegram API — at a glance illustration
  1. 1Upload

    Random 64-bit file ID, numbered parts, then inputFile or inputFileBig.

  2. 2Download

    offset and limit rules; precise mode allows 1 KB steps.

  3. 3Errors

    FILE_MIGRATE, FILE_REFERENCE_EXPIRED, FLOOD_WAIT.

Share this guide

Send it to a friend who has the same question.

Telegram WhatsApp
More

Open on your phonePoint your phone camera here to open this guide.

Updated

Related articles

View all ›