Local Files

Learn to use Slack API calls to manipulate files in the workspace.

We can regularly upload and share local files on our system using Slack API calls.

Let’s look at the following endpoints in this lesson:

  1. files.upload: This endpoint uploads or creates a file.
  2. files.list: This endpoint list files for a team, in a channel, or from a user with applied filters.
  3. files.info: This endpoint gets information about a file.
  4. files.delete: This endpoint deletes a file.

Upload a file

To upload a file, we’ll access the https://slack.com/api/files.upload endpoint. First, we’ll go over how to upload a file using the files.upload endpoint. We’ll use the POST request to call this endpoint.

Request parameters

Some important query parameters for the files.upload endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

channels

string

optional

This is a comma-separated list of channel names or IDs where the file will be shared.

content

string

optional

These are the contents of the file we send via a POST variable. If you are omitting this parameter, you must provide a file.

file

string

optional

These are files we send via multipart/form-data (for posting entire files as part of a form submission when using the POST request). If you are omitting this parameter, you must submit content.

filename

string

optional

This is the file name.

filetype

string

optional

A file type identifier, such as auto or python.

initial_comment

string

optional

This is the message text introducing the file in specified channels.

thread_ts

string

optional

This provides another message's ts value to upload this file as a reply. We can not use a reply's ts value. Instead, we use its parent instead.

title

string

optional

This is the title of the file.

Upload the file and check for it in the Slack channel where the application has been added.

Let’s call the files.upload endpoint. Click the “Run” button to upload the mytext.txt file to Slack.

Press + to interact
index.js
mytext.txt
import fetch from "node-fetch";
import { promises as fs } from "fs";
// Defining a function to read a local file
async function readLocalFile(filename) {
try {
const data = await fs.readFile(filename, { encoding: "utf8" });
return data;
} catch (err) {
console.log(err);
}
}
const url = new URL("https://slack.com/api/files.upload");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/x-www-form-urlencoded",
};
async function uploadFile() {
try {
const filename = "mytext.txt";
const fileContent = await readLocalFile(filename);
const bodyParameters = new URLSearchParams({
channels: "{{CHANNEL_ID}}",
content: fileContent,
filename: filename,
filetype: "txt",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
uploadFile();

Let’s look at the highlighted lines in the code widget shown above:

  • Line 2: We import the JavaScript fs module.
  • Lines 5–12: We define a function readLocalFile to read and return the contents of a local file.
  • Lines 23–24: We read the contents of the mytext.txt file using the readLocalFile function.
  • Lines 26–31: We specify the necessary parameters: channels, content, filename, and filetype.

Response fields

The response from this endpoint contains the details of the file that was uploaded.

Note: Visit this lesson to view the details of the file object.

Get a list of all files

To get a list of all files, we access the https://slack.com/api/files.list endpoint. This endpoint will respond with a list of all the uploaded files in a channel.

Request parameters

Some important query parameters for the files.list endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

channel

string

optional

This parameter filters files that appear in a specific channel, indicated by their ID.

count

integer

optional

This is the number of items to return per page.

Let’s call the files.list endpoint. Click the “Run” button to get a list of all the files in a channel.

Press + to interact
import fetch from "node-fetch";
const url = new URL("https://slack.com/api/files.list");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const queryParameters = new URLSearchParams({
channel: "{{CHANNEL_ID}}",
});
const options = {
method: "GET",
headers: headerParameters,
};
async function getFiles() {
try {
url.search = queryParameters;
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
getFiles();

Let’s look at the highlighted lines in the code widget shown above:

  • Line 3: We specify the files.list endpoint.
  • Line 11: We specify the channel to filter only the files uploaded to that channel.

Response fields

The response includes the details of all the files uploaded to a certain channel.

Note: Visit this lesson to view the details of each element within the files list.

Get file information

To get the file information, we access the https://slack.com/api/files.info endpoint. Once we have a file ID, we can get the file information using the files.info endpoint.

Request parameters

Some important query parameters for the files.info endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

file

string

required

This specifies a file by providing its ID.

count

string

optional

This is the number of items to return per page.

cursor

string

optional

This parameter is a string of characters used to "point" to the next "page" of data. We paginate through collections of data by setting the cursor parameter to a next_cursor attribute returned by a previous request's response_metadata.

limit

integer

optional

This indicates the maximum number of items to return. Fewer than the requested number of items may be returned, even if the end of the list hasn't been reached.

page

integer

optional

This is the page number of results to return.

Let’s call the files.info endpoint. Click the “Run” button to get the information on the already uploaded file.

Press + to interact
import fetch from "node-fetch";
const url = new URL("https://slack.com/api/files.info");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const queryParameters = new URLSearchParams({
file: "{{FILE_ID}}",
});
const options = {
method: "GET",
headers: headerParameters,
};
async function getFileInfo() {
try {
url.search = queryParameters;
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
getFileInfo();

Let’s look at the highlighted lines in the code widget shown above:

  • Line 11: We specify the file parameter.
  • Lines 21–22: We provide the query parameters and call the file.info endpoint.

Response fields

The response includes the full details of the file.

Note: Visit this lesson to view the details of the file object.

Delete a file

To delete a file, we access the https://slack.com/api/files.delete endpoint. We use the same file ID to delete the file from the workspace.

Request parameters

Some important query parameters for the files.delete endpoint are as follows:

Parameter

Type

Category

Description

token

token

required

Authentication tokens carry the required scopes that govern the usage of different Slack applications and APIs. We usually pass these tokens as an HTTP Authorization header or as a POST parameter.

file

string

required

This specifies a file by providing its ID.

Let’s call the files.delete endpoint. Click the “Run” button to delete the file we uploaded at the beginning of the lesson.

Press + to interact
import fetch from "node-fetch";
const url = new URL("https://slack.com/api/files.delete");
const headerParameters = {
Authorization: "Bearer {{TOKEN}}",
"Content-Type": "application/json; charset=UTF-8",
};
const bodyParameters = JSON.stringify({
channel: "{{CHANNEL_ID}}",
file: "{{FILE_ID}}",
});
const options = {
method: "POST",
headers: headerParameters,
body: bodyParameters,
};
async function deleteFile() {
try {
const response = await fetch(url, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
deleteFile();

Let’s look at the highlighted lines in the code widget shown above:

  • Lines 10–13: We specify the channel and file body parameters.
  • Line 23: We call the files.delete endpoint.

Response fields

The response from this endpoint includes only the ok parameter indicating whether the request was successful or not.