getFile
Returns: FileOfficial docs ↗
Use this method to get basic information about a file and prepare it for downloading. For the moment, bots can download files of up to 20MB in size. On success, a File object is returned. The file can then be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>, where <file_path> is taken from the response. It is guaranteed that the link will be valid for at least 1 hour. When the link expires, a new one can be requested by calling getFile again.
Parameters
file_idStringRequiredFile identifier to get information about
Returns
On success, the File object is returned.
GramIO Usage
ts
// Get file info and build the download URL
const file = await bot.api.getFile({ file_id: "AgACAgIAAxk..." });
if (file.file_path) {
const token = process.env.BOT_TOKEN ?? "";
const downloadUrl = `https://api.telegram.org/file/bot${token}/${file.file_path}`;
console.log("Download URL:", downloadUrl);
// URL is valid for at least 1 hour
}ts
// Retrieve file info inside a message handler
bot.on("message", async (ctx) => {
const document = ctx.payload.document;
if (!document) return;
const file = await bot.api.getFile({ file_id: document.file_id });
await ctx.reply(
`File ready — ${file.file_size ?? 0} bytes\nPath: ${file.file_path}`
);
});ts
// Renew an expired download link by calling getFile again with the same file_id
async function refreshDownloadUrl(fileId: string, token: string) {
const file = await bot.api.getFile({ file_id: fileId });
if (!file.file_path) throw new Error("File not available");
return `https://api.telegram.org/file/bot${token}/${file.file_path}`;
}See also GramIO file download helpers for a higher-level API.
Errors
| Code | Error | Cause |
|---|---|---|
| 400 | Bad Request: wrong file identifier/HTTP URL specified | file_id is invalid, malformed, or belongs to a different bot — double-check the source |
| 400 | Bad Request: file is too big | File exceeds the 20 MB cloud API limit — use a local Bot API server for larger files |
| 400 | Bad Request: file_id not found | The file no longer exists on Telegram servers (some files expire) |
| 429 | Too Many Requests: retry after N | Rate limit hit — check retry_after, use the auto-retry plugin |
Tips & Gotchas
- 20 MB cloud limit. The Telegram cloud API only serves files up to 20 MB. For larger files (up to 2 GB), run a local Bot API server — the local server returns the absolute path on disk instead of a download URL.
file_idvsfile_unique_id. Always passfile_idtogetFile. Thefile_unique_idfield cannot be used to download or reuse files — it only identifies the underlying file across different bots.- Download URL expires after ~1 hour. Cache the URL only for short-lived tasks. For persistent access, store the
file_idand callgetFileagain to refresh the URL. file_pathcan be absent. The field is optional — check before building the URL. It is absent if the file is too large or currently unavailable.- Token in URL is sensitive. The download URL contains your bot token. Never expose it to end users directly — proxy downloads through your server if needed.
See Also
- File — return type of
getFile - GramIO file download guide — higher-level download helpers
- GramIO file upload guide —
MediaUploadfor sending files