[Google Apps Script] Upload an image to a Slack channel
Created on November 30, 2026
Slack's external upload API takes three steps. Request an upload URL and file ID, send the image bytes to that URL, then complete the upload and share the file in a channel.
Slack has retired the files.upload method.
Store a Slack bot token with the files:write scope in the Apps Script property SLACK_BOT_TOKEN, and add the bot to the destination channel. Pass an image Blob you already have, the channel ID, and a filename with an extension. You can also specify the title shown in Slack.
function uploadImageToSlack(imageBlob, channelId, filename, title = filename) {
const token = PropertiesService.getScriptProperties().getProperty("SLACK_BOT_TOKEN");
if (!token) {
throw new Error("Set the SLACK_BOT_TOKEN script property.");
}
const bytes = imageBlob.getBytes();
const upload = callSlack("files.getUploadURLExternal", token, {
filename,
length: String(bytes.length),
});
UrlFetchApp.fetch(upload.upload_url, {
method: "post",
contentType: "application/octet-stream",
payload: bytes,
});
callSlack("files.completeUploadExternal", token, {
files: JSON.stringify([{ id: upload.file_id, title }]),
channel_id: channelId,
});
}
function callSlack(method, token, payload) {
const response = UrlFetchApp.fetch("https://slack.com/api/" + method, {
method: "post",
headers: { Authorization: "Bearer " + token },
payload,
});
const result = JSON.parse(response.getContentText());
if (!result.ok) {
throw new Error(method + ": " + result.error);
}
return result;
}
For example, if you already have the image in imageBlob, call the function as follows.
uploadImageToSlack(imageBlob, "C0123456789", "scores.png", "Monthly scores");
The completion call uses the file ID returned in the first step. Supply the channel ID there to share the uploaded image.