Local Files
Learn to use Slack API calls to manipulate files in the workspace.
We'll cover the following
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:
files.upload
: This endpoint uploads or creates a file.files.list
: This endpoint list files for a team, in a channel, or from a user with applied filters.files.info
: This endpoint gets information about a file.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 | 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. |
| string | optional | This is a comma-separated list of channel names or IDs where the file will be shared. |
| 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 |
| string | optional | These are files we send via |
| string | optional | This is the file name. |
| string | optional | A file type identifier, such as |
| string | optional | This is the message text introducing the file in specified |
| string | optional | This provides another message's |
| 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.
import fetch from "node-fetch";import { promises as fs } from "fs";// Defining a function to read a local fileasync 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 thereadLocalFile
function. - Lines 26–31: We specify the necessary parameters:
channels
,content
,filename
, andfiletype
.
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 | 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. |
| string | optional | This parameter filters files that appear in a specific channel, indicated by their ID. |
| 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.
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 | 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. |
| string | required | This specifies a file by providing its ID. |
| string | optional | This is the number of items to return per page. |
| 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 |
| 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. |
| 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.
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 | 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. |
| 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.
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
andfile
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.