Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Telegram 400: Bad Request: wrong file identifier/HTTP URL specified — how to fix it

Bad Request: wrong file identifier/HTTP URL specified means Telegram could not use the file you passed as a photo, document or other media. Usually the file_id belongs to a different bot (a file_id works only for the bot that got it), a file_unique_id was sent instead, the id was cut, or Telegram could not download your URL. Upload the file again with this bot and save the new file_id, or upload it instead of sending a link.

By Livops team, the people who build and run LivopsUpdated

The error, as the Telegram Bot API returns it with error code 400[2][3]:

Bad Request: wrong file identifier/HTTP URL specified

Each library wraps it in its own words:

Library What you see
aiogram 3 TelegramBadRequest: Telegram server says - Bad Request: wrong file identifier/HTTP URL specified[6][7]
python-telegram-bot telegram.error.BadRequest: Wrong file identifier/http url specified[9][10]
grammY GrammyError: Call to 'sendPhoto' failed! (400: Bad Request: wrong file identifier/HTTP URL specified)[11]
Telegraf TelegramError: 400: Bad Request: wrong file identifier/HTTP URL specified[13]

python-telegram-bot drops the Bad Request: prefix and runs the rest through Python’s capitalize(), which also lowercases HTTP URL[9]; search your logs case-insensitively. This page explains why one message covers both a file_id and a URL, lists the causes from the most common, shows the fix for each, and ends with what to check when the bot runs on Livops.

What does the error mean?

Every media field — photo, document, video and the rest — takes one of three things: a file_id of a file already on Telegram’s servers, an HTTP URL for Telegram to download, or a new file uploaded with multipart/form-data[1]. The Bot API server hands the first two to Telegram as the same kind of “remote file” string[2]. A string with a dot in it is read as a URL; anything else is decoded as a file_id[4].

When Telegram then cannot turn that string into media it can send, it reports empty media, and the Bot API translates that into wrong file identifier/HTTP URL specified[3]. It does not say which of the two went wrong, because at that point it does not know: a valid-looking file_id this bot may not use and a URL that did not give a usable file end the same way.

The close variants

The same family has more specific messages. The Bot API lowercases the first letter of each and puts Bad Request: in front[2]:

Message What it means
wrong remote file identifier specified: Wrong string length, ...: Wrong character in the string The string is not a file_id at all: it was cut, or has characters a file_id never contains[4][5]
wrong remote file identifier specified: can't unserialize it It decodes, but does not have the structure of a file_id[4]
can't use file of type ... as ... A real file_id, sent with the method for another file type[4]
failed to get HTTP URL content Telegram’s attempt to download the URL failed[2]
wrong type of the web page content Telegram reached the URL, but what it got is not the expected media[2]
invalid file HTTP URL specified: ... The URL itself cannot be parsed[4]

Causes, most common first

1. A file_id from a different bot

A file_id is unique for each individual bot and can’t be transferred from one bot to another[1]. The id is real, but it belongs to someone else. The usual ways it happens:

  • the database was filled by the production bot, and a test bot with its own token now reads it — or the other way round;
  • the bot’s token was replaced with a new bot’s;
  • the id was copied from another bot, a helper bot or a forum answer.

Check which bot the token belongs to:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/getMe"

getMe returns the bot’s id and username[1]; it must be the bot that saved the file_id.

2. file_unique_id instead of file_id

Every file object has both. file_id can be used to download or reuse the file; file_unique_id is meant to stay the same over time and across bots, and can’t be used to download or reuse the file[1]. Storing both in one table and reading the wrong column gives this error, or one of the wrong remote file identifier variants.

3. The id was cut or changed on the way

A VARCHAR column shorter than the id, a log line or chat message that wrapped it, a value pasted with quotes or a newline into an environment variable. A broken id usually fails before Telegram sees it, with wrong remote file identifier specified and the reason after the colon[4][5]. Print the exact value you send, with its length, and compare it with the one you saved.

4. The right id for the wrong method

It is not possible to change the file type when resending by file_id: a video can’t be sent as a photo, a photo can’t be sent as a document[1]. sendPhoto with a video’s file_id fails with can't use file of type ... as ...[4]. Store the type next to the id and pick the method from it.

5. A URL Telegram cannot download

With a URL, Telegram’s servers download the file and send it[1] — not your server, and not your browser. A link that opens for you can still fail for them:

  • it needs a login, a cookie or an Authorization header;
  • it points at localhost, a private address or a server that only your network can reach;
  • it is a share page (a cloud drive’s preview, a login redirect) that answers with HTML instead of the file.

A failed download comes back as failed to get HTTP URL content[2]; content that is not usable media as this error or wrong type of the web page content[2][3].

6. A URL Telegram does not accept for this method

Sending by URL has its own rules[1]:

  • at most 5 MB for photos and 20 MB for other content;
  • the file must have the correct MIME type, for example audio/mpeg for sendAudio;
  • sendDocument by URL currently works only for .PDF and .ZIP files;
  • for sendVoice, the file must be audio/ogg and no more than 1 MB; voice notes of 1–20 MB are sent as files.

Telegram adds that other configurations may work but are not guaranteed[1]. For larger files, see Bad Request: file is too big.

How to fix it

Upload again with this bot, and keep the new file_id

A file_id from another bot cannot be repaired. Upload the original file once with the current bot and save the file_id from the Message it returns. Sending by file_id has no size limit[1], so the next sends cost nothing. Keep the original file somewhere, so this works again after the next token change:

# aiogram 3
from aiogram import Bot
from aiogram.exceptions import TelegramBadRequest
from aiogram.types import FSInputFile


async def send_cached_photo(bot: Bot, chat_id: int, key: str, path: str, db) -> None:
    file_id = await db.get_file_id(key)  # saved by this bot
    if file_id:
        try:
            await bot.send_photo(chat_id, file_id)
            return
        except TelegramBadRequest as e:
            if "file identifier" not in e.message:
                raise
    msg = await bot.send_photo(chat_id, FSInputFile(path))
    await db.save_file_id(key, msg.photo[-1].file_id)

"file identifier" matches both wrong file identifier/HTTP URL specified and wrong remote file identifier specified. Resending a photo by file_id sends all of its sizes[1], so saving the last one is enough.

If several bots serve the same files, key your cache by file_unique_id, which is the same for every bot[1], and keep one file_id per bot.

Let the bot download the URL and upload it

When Telegram cannot fetch a link — behind a login, on a private network, or not the right MIME type — download it in the bot and upload it. The upload limits then apply: 10 MB for photos, 50 MB for other files[1].

aiogram’s URLInputFile streams the URL through the bot’s own HTTP session and accepts headers, so a link behind a token works[8]:

from aiogram.types import URLInputFile

await bot.send_document(
    chat_id,
    URLInputFile(url, headers={"Authorization": f"Bearer {api_token}"}, filename="report.xlsx"),
)

grammY’s InputFile takes a URL object and fetches it itself; a failed download throws Download failed, received HTTP status ... in your code instead of a Bot API error[12]:

import { InputFile } from "grammy";

await ctx.replyWithPhoto(new InputFile(new URL(imageUrl)));

With curl, the same is a normal upload:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" \
  -F "chat_id=$CHAT_ID" \
  -F "[email protected]"

Or fix the URL

If you want Telegram to fetch it, check the link from a machine outside your network:

curl -sI "$FILE_URL"

It should answer 200 without a login, with the file’s own Content-Type (image/jpeg, application/pdf …), not text/html, and within the URL limits above[1].

How to confirm the fix

Send the stored id with the token the app uses:

curl -s "https://api.telegram.org/bot$BOT_TOKEN/sendDocument" \
  --data-urlencode "chat_id=$CHAT_ID" \
  --data-urlencode "document=$FILE_ID"

"ok":true means the id and the token belong together, and the problem is in how the code reads the id. The same error again means the id is another bot’s or not a file_id: run getMe, and check which column the value came from.

On Livops

  • One bot, one set of file_ids. The token goes under Variables as BOT_TOKEN; new values take effect on the next deploy. A new environment such as staging copies production’s variables and gets its own empty database. Give it its own test bot’s token: it then starts with no saved file_ids and fills them with its own uploads. Copying production’s table of file_ids into staging brings this error with it.
  • file_ids in PostgreSQL. Add PostgreSQL under Databases, and DATABASE_URL is written into every service in the environment. Store ids as TEXT, next to the file’s type and file_unique_id.
  • Keep the originals. Files the bot must be able to upload again go to /app/data, whose path is in DATA_DIR and which is kept across deploys and restarts; /tmp is wiped on every restart and deploy. For many or large files, use a storage bucket. See Files and data.
  • A link Telegram can open. A bucket is private by default and hands out temporary signed links (presigned URLs), or can be made public with a ..._PUBLIC_URL variable. Either one gives Telegram a URL that needs no login; the URL limits above still apply.
  • Logs. Log the file_id, its length and the bot’s username next to each failed send; the project’s Logs page and livops logs <service> -f (CLI) show them.

A bot costs 19 credits a month (19,000 so’m); see Pricing.

Questions

Can two bots share a file_id?

No. A file_id is unique for each bot and cannot be transferred from one bot to another. If a second bot needs the file, it has to upload it once itself and keep the file_id that upload returns.

What is the difference between file_id and file_unique_id?

file_id is what you send and download with, and it is specific to your bot. file_unique_id stays the same over time and across bots, which makes it good for spotting duplicates, but it cannot be used to download or resend the file.

My bot worked yesterday and now every saved file_id fails. Why?

Most often the token changed: a new bot, or a test bot's token in production, or the other way round. The file_ids in your database belong to the old bot. Run getMe to see which bot the token belongs to, and re-upload the files with that bot.

Why does sending a photo by URL fail when the link opens in my browser?

Telegram's servers download the link, not your browser. A link that needs a login or cookie, points at localhost or a private network, returns an HTML page instead of the file, or has the wrong MIME type will fail there. Photos by URL are also limited to 5 MB.

How do I send a file Telegram cannot download itself?

Download it in your bot and upload it with multipart/form-data. aiogram's URLInputFile and grammY's InputFile with a URL do this for you. Uploads allow up to 10 MB for photos and 50 MB for other files.

Can I change a video into a photo by resending its file_id?

No. Telegram does not allow changing the file type when resending by file_id: a video cannot be sent as a photo and a photo cannot be sent as a document. Use the send method that matches the file's type.

Sources

  1. Telegram Bot API — Sending files, File, getMe, sendPhoto, sendDocument (core.telegram.org). Checked .
  2. Telegram Bot API server source — telegram-bot-api/Client.cpp (raw.githubusercontent.com). Checked .
  3. TDLib source — td/telegram/MessageQueryManager.cpp (raw.githubusercontent.com). Checked .
  4. TDLib source — td/telegram/files/FileManager.cpp (raw.githubusercontent.com). Checked .
  5. TDLib source — tdutils/td/utils/base64.cpp (raw.githubusercontent.com). Checked .
  6. aiogram source — aiogram/exceptions.py (raw.githubusercontent.com). Checked .
  7. aiogram source — aiogram/client/session/base.py (raw.githubusercontent.com). Checked .
  8. aiogram source — aiogram/types/input_file.py (raw.githubusercontent.com). Checked .
  9. python-telegram-bot source — telegram/error.py (raw.githubusercontent.com). Checked .
  10. python-telegram-bot source — telegram/request/_baserequest.py (raw.githubusercontent.com). Checked .
  11. grammY source — src/core/error.ts (raw.githubusercontent.com). Checked .
  12. grammY source — src/types.node.ts (raw.githubusercontent.com). Checked .
  13. Telegraf source — src/core/network/error.ts (raw.githubusercontent.com). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your bot

Got a question? — @livops_uz