Upload Files to Slack
Overview
This reference file describes the uploadFileToSlack() function from Upload File to Slack, which allows you to upload files of any type to Slack channels using Slack's three-step upload process. The function supports optional metadata like titles and comments, and handles malware scanning by Slack before making files available in the workspace.
Supported File Types
The function automatically detects and properly handles the following file types:
Images
- JPEG (
.jpg,.jpeg) - PNG (
.png) - GIF (
.gif) - SVG (
.svg) - WebP (
.webp) - BMP (
.bmp) - TIFF (
.tiff) - ICO (
.ico)
Documents
- PDF (
.pdf) - Microsoft Word (
.doc,.docx) - Microsoft Excel (
.xls,.xlsx) - Microsoft PowerPoint (
.ppt,.pptx)
Text/Code Files
- Plain text (
.txt) - Markdown (
.md) - CSV (
.csv) - JSON (
.json) - XML (
.xml) - HTML (
.html) - CSS (
.css) - JavaScript (
.js) - TypeScript (
.ts) - Python (
.py) - Java (
.java) - Ruby (
.rb) - Go (
.go) - PHP (
.php) - C (
.c) - C++ (
.cpp) - Shell scripts (
.sh)
Archives
- ZIP (
.zip) - RAR (
.rar) - TAR (
.tar) - GZIP (
.gz) - 7-Zip (
.7z)
Audio
- MP3 (
.mp3) - WAV (
.wav) - OGG (
.ogg) - M4A (
.m4a)
Video
- MP4 (
.mp4) - QuickTime (
.mov) - AVI (
.avi) - WMV (
.wmv) - WebM (
.webm) - Matroska (
.mkv)
The function supports all file types. Slack will automatically detect the file type and scan for malware before making it available in the workspace.
Required Parameters
The following parameters are required to use the function:
channelId(string)- The Slack channel ID where the file will be uploaded
- Format: Typically starts with 'C' followed by alphanumeric characters (e.g.,
'C09R5QUC527') - Can be found in the channel URL or via the Slack API
filePath(string)- The local filesystem path to the file you want to upload
- Can be absolute or relative path
- Example:
'session/attachments/photo.jpeg'or'/Users/name/Documents/report.pdf'
slackToken(string)- Your Slack API authentication token
- Must have
files:writescope permission
Optional Parameters
The function accepts an optional options object with the following properties:
title(string, optional)- A title for the uploaded file
- Will be displayed in Slack above the file
- Example:
'Q4 Sales Report'
initialComment(string, optional)- A comment/message to post along with the file
- Appears as a message in the channel with the file attached
- Example:
'Here is the report we discussed'
How to Run the Code
Basic Usage
IMPORTANT: Do NOT generate or define the uploadFileToSlack function. The function already exists in Upload File to Slack and must be imported.
To execute this script, use the run_script tool with the following code:
import { uploadFileToSlack } from '
Upload File to Slack';
const result = await uploadFileToSlack(
'C09R5QUC527', // Channel ID
'session/attachments/photo.jpeg', // File path
process.env.SLACK_TOKEN // Slack token
);
console.log('File uploaded:', result);
// Output: { success: true, fileId: 'F123...', fileName: 'photo.jpeg', permalink: 'https://...' }With Optional Parameters
Remember: Always import the function from the existing script file. Do not generate it.
Use the run_script tool to execute this script with optional parameters:
import { uploadFileToSlack } from '
Upload File to Slack';
const result = await uploadFileToSlack(
'C09R5QUC527',
'documents/report.pdf',
process.env.SLACK_TOKEN,
{
title: 'Q4 Financial Report',
initialComment: 'Here is the final Q4 report for review. Please provide feedback by EOD.'
}
);
console.log('Upload successful!');
console.log(JSON.stringify(result, null, 2));Return Value
The function returns a Promise that resolves to an object with the following structure:
{
success: true, // Boolean - indicates successful upload
fileId: 'F123ABC456', // String - Slack's unique file identifier
fileName: 'photo.jpeg', // String - name of the uploaded file
permalink: 'https://...' // String - permanent URL to view the file
}How It Works
The function uses Slack's three-step upload process to handle file uploads securely with malware scanning:
Step 1: Get Upload URL
- Calls the
files.getUploadURLExternalAPI method - Provides the filename and file size (in bytes)
- Receives an upload URL and file ID from Slack
Step 2: Upload File Contents
- POSTs the file contents to the URL from Step 1
- Uses
application/octet-streamcontent type - Verifies successful upload via HTTP status code
Step 3: Complete Upload
- Calls the
files.completeUploadExternalAPI method - Provides the file ID, optional title, channel ID, and optional initial comment
- Slack scans the file for malware (larger files take longer to scan)
- File becomes available in the specified channel once scanning completes
Error Handling
The function throws descriptive errors at each step if any part of the upload process fails, making it easy to diagnose issues.
Return Value
Extracts and returns key information about the uploaded file including the file ID, filename, and permanent link.