I've got a function to download an image from telegram message, it accepts message.media.photo:
const downloadPhoto = async (photo, client, name) => {
await client.connect();
const file = new Api.InputPhotoFileLocation({
id: photo.id,
accessHash: photo.accessHash,
fileReference: photo.fileReference,
thumbSize: "y",
});
try {
const buffer = await client.downloadFile(file, {
dcId: photo.dcId,
});
fs.writeFileSync(name ? name : "output.jpg", buffer);
} catch (error) {
console.error("Error downloading photo:", error, file);
}
};
I get messages using this method:
await client.invoke(new Api.messages.GetHistory(req))
which works fine. The issue is with the download method: it fails with an error:
Error downloading photo: RPCError: 400: CONNECTION_NOT_INITED (caused by upload.GetFile)
at RPCMessageToError (/app/img-bot/node_modules/telegram/errors/index.js:28:12)
at MTProtoSender._handleRPCResult (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:561:58)
at MTProtoSender._processMessage (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:492:15)
at process.processTicksAndRejections (node:internal/process/task_queues:105:5)
at async MTProtoSender._recvLoop (/app/img-bot/node_modules/telegram/network/MTProtoSender.js:439:17)
What am I doing wrong?
asked Nov 21, 2025 at 7:37
k102
8,1199 gold badges52 silver badges72 bronze badges
1 Answer 1
Telegram stores media on different DCs.
By forcing dcId and also calling client.connect() inside the download function, you end up with a DC connection that is not properly initialized for file download CONNECTION_NOT_INITED.
- What to change
Connect only once at startup, not inside downloadPhoto:
// app startup
const client = new TelegramClient(session, apiId, apiHash, { connectionRetries: 5 });
await client.start({ /* auth options */ });
// no client.connect() inside downloadPhoto
- Do not pass
dcIdtodownloadFilelet GramJS handle it
const downloadPhoto = async (photo, name = "output.jpg") => {
const file = new Api.InputPhotoFileLocation({
id: photo.id,
accessHash: photo.accessHash,
fileReference: photo.fileReference,
thumbSize: "y",
});
try {
const buffer = await client.downloadFile(file, {});
fs.writeFileSync(name, buffer);
} catch (error) {
console.error("Error downloading photo:", error);
}
};
Sign up to request clarification or add additional context in comments.
3 Comments
k102
sadly got the same issue, will check further
k102
yeah, the issue is somehow connected to the DC: got this now "The file to be accessed is currently stored in DC 2 (caused by upload.GetFile)". The issue is the same though
k102
oh! got it! I have explicitly set
storeSession.setDC(2, '149.154.167.41', 443); and it worked! thank youlang-js