Working with files

Working with files can feel like a free-for-all, but fear no more. Files are just messages with extra information included.

Since files can represent complex information, an app that uses files well can communicate more succinctly and precisely. In this guide, we'll explore the two ways to work with files. Here are the distinct differences.

Uploading files to Slack allows you to:

  • Treat files like regular messages
  • Upload files directly to and host files in Slack
  • Easiest way to use files in Slack

Adding remote files allows you to:

  • Keep your files hosted wherever you please
  • Reference and unfurl files remotely
  • Harness the power of the Remote Files API

Uploading files to Slack

When you upload a file to Slack, Slack becomes the host for your file, taking care of its safe storage. Uploading files is by far the easiest way to use files in Slack.

App setup

In order to upload a file to Slack, there are a couple of scopes your app should have. There are also some events you may want your app to subscribe to.

Scopes

Under the Apps page of your app, choose the OAuth & Permissions sidebar to select scopes.

If you'd like to do your work with a bot instead, one scope will do: request the bot scope to gain dominion over the file arts.

Events

A bevy of events inform your app about file happenings in your workspace. To subscribe to any file events, use the Event Subscriptions tab under your Apps page. Here's a list of file events:

With your app set up, read on to learn how to upload files.

Uploading files

Your app can upload files using the files.upload method. Check out the method documentation for a deep dive on the details of available parameters.

Here's a sample curl command that uploads a file:

curl -F file=@cycling.jpeg -F "initial_comment=Hello, Leadville" -F channels=C0R7MFNJD -H "Authorization: Bearer xoxp-123456789" https://slack.com/api/files.upload

As always, life is easier with a Slack SDK. Use your programming language of choice, and get methods like files.upload as primitive building blocks that you don't have to think about.

If you've got the necessary scopes, your file should appear in the channels specified.

A picture of a teapot

Here's a potentially confusing bit when you use the channels parameter during your upload: the upload method actually combines "uploading" (creating a file hosted in Slack), and "sharing" the file to a channel.

If you don't use the channels parameter, the upload method only "uploads" the file, hosting the file in Slack, but doesn't "share" the file anywhere.

Either way, once your upload succeeds, you'll see an HTTP response from Slack containing an "ok": true field, plus a file object. For more detail on file objects and the fields contained inside, a look at the file object documentation is highly recommended.

Other methods for working with files

Slack provides some other methods for working with files:

Responding to files

Slack also sends events when files are uploaded or changed in your workspace. Here are some of the relevant events:

The best thing about working with files is that they usually work the way you'd expect. Gone are the days of file comments (don't ask) and strange behavior.

If, however, you wish to flex your file skills by sharing files hosted outside of Slack, providing custom unfurls, or fine-tuning the way your files appear in Slack searches: continue reading for guidance on remote files.


Adding remote files

Remote files are files not hosted inside Slack. Think of a remote file as a pointer, a forwarding address, or a reference to a file that lives elsewhere.

For most apps, uploading files directly to Slack is preferred over adding remote files. Adding remote files involves extra steps that may not be necessary for your use case.

Why use remote files?

There are a few benefits to adding and sharing remote files to Slack:

  • If your file can't be hosted in Slack for property rights reasons, remote files are here to save the day.
  • If you wish to provide a rich, custom unfurl experience for your files when their links are shared, remote files bring forth fantastical unfurls for the low low price of a JSON object.
  • If you want to control the way your file appears in Slack searches so that others can find it later, remote files allow for customized indexable content. That way, you can ensure your file appears when users search for certain terms.

Note on terminology
Remote files are "added" to Slack, not "uploaded." Adding a remote file to Slack makes Slack aware of its existence. Once that's done, "sharing" a remote file, like sharing a direct upload file, brings the file into a conversation. So a remote file must be "added" before it is "shared".

You can add, update, remove, share, and unfurl remote files in Slack.

App setup

In order to add a remote file to Slack, there are a couple of scopes your app should have. There are also some events you may want your app to subscribe to.

Scopes

Working with remote files requires a distinct set of scopes from those involved in direct upload. Again, under the Apps page of your app, choose the OAuth & Permissions sidebar to select scopes.

  • remote_files:read allows your app to read information about remote files visible to the user associated with your user token.
  • remote_files:share allows your app to share remote files visible to the user associated with your user token.
  • chat:write allows your app to post messages in approved channels & conversations. This is especially useful for an app that provides a custom unfurl for remote files.
  • links:read allows your app to view and subscribe to events about links that have been shared in conversation. Again, this is useful for knowing when a link to your remote file has been shared, so that you can unfurl it.
  • links:write allows your app to unfurl links (like to remote files) using the chat.unfurl method.
  • bot allows your app's bot user to add, share, delete, and generally manipulate remote files.

But wait a second. Where's the regular old remote_files:write scope? How does my app add a remote file to Slack so that it can be shared?

There is no write scope for remote files. Remote files exist across the whole workspace (or organization, for Enterprise Grid). Because of that, remote files must be added by bots with the bot scope, not by an individual user.

Once you've obtained the necessary scopes, you can add, update, remove, share, and unfurl remote files in Slack.

Events

To subscribe to any file events, use the Event Subscriptions tab under your Apps page. One event that can be especially useful to apps that work with remote files is the link_shared event, which notifies your app when a link has been shared so that you can unfurl it. Add any App Unfurl Domains that relate to your file service under that same tab in the Apps page.

Once you've obtained the necessary scopes and subscribed to any desired events, you can add, update, remove, share, and unfurl remote files in Slack.

Adding remote files

To add a remote file, use the files.remote.add method from the Web API.

Remote files exist across the whole workspace (or organization, for Enterprise Grid).

Here's a sample call:

curl -F token=<token> \
    -F external_id=ABCD1 \
    -F external_url=https://mydocuments.com/document/d/1TA9fIaph4eSz2fC_1JGMuYaYUc4IvieIop0WqfCXw5Y/edit?usp=sharing \
    -F title=LeadvilleAndBackAgain \
    -F preview_image=@cycling.jpg  \
    -F indexable_file_contents=search_terms.txt
    https://slack.com/api/files.remote.add

Pay particular attention to the preview_image parameter. preview_image should be a binary image file, and it will be stored in Slack. That's going to allow a more beautiful unfurl in the next step, when the app actually shares the remote file.

indexable_file_contents also deserves a mention. Use this parameter to specify a file containing the search terms that correspond to this remote file. When a user searches in Slack, their query will be compared against the contents of this text file for matching.

Think of this text file like the alt parameter on an HTML <img> tag — a textual representation of a non-textual object. The text file can contain a description of the remote file, or it can contain search keywords, or anything else text-based.

Once your upload succeeds, you'll see an HTTP response from Slack containing an "ok": true field, plus a file object. For more detail on file objects and the fields contained inside, a look at the file object documentation is highly recommended.

You'll see the external_id returned to you on the file object in the Slack response, as well as a file_id. Either of these ids can be stored and used to query the files.remote.info method to access other information about the file.

Sharing remote files

Adding is nice, but sharing is caring.

In other words, adding a remote file does not, by itself, share the file to a conversation. Without sharing the file, it remains an orphan inside Slack—you can view information on it via files.remote.info method, but it won't actually be visible anywhere. Let's make it visible with a call to the files.remote.share method.

curl -F token=<token> \
    -F external_id=ABCD1 \
    -F channels=C12345 \
    https://slack.com/api/files.remote.share

Now we get a lovely message in channel that shows off the remote file (in this case, a story about cycling) as it should be seen.

A remote file upload

Now we have a custom preview_image, which we specified during the add step, instead of the ugly raw URL and a small picture of text.

A word about tokens: the files.remote.share method may be called with either a bot or a user token. The bot token shares the file from your app, while the user token shares the file from the user associated with your user token. Use the bot token to share to channels that the bot has access to; use the user token to share to channels that the user has access to.

Unfurling remote files

We've already mentioned how to add a custom preview_image that makes our remote files appear nicer when they're shared. But what about when someone else shares our remote file as a link?

The first step to providing lush unfurls is to be armed with the link_shared event.

When that link_shared event fires, we receive its payload, including the message_ts of the message that triggered it, as well as the channel of that message. Now we can make an HTTP request to chat.unfurl. First, let's set up our unfurls object using a block:

unfurls: {
        'https://mydocuments.com/document/d/1TA9fIaph4eSz2fC_1JGMuYaYUc4IvieIop0WqfCXw5Y/edit?usp=sharing': {
          hide_color: true,
          blocks: [{
            type: 'file',
            external_id: 'ABCD1',
            source: 'remote',
          }]
        }
      }

For use specifically with a file unfurl, you can set the hide_color field to true to remove the color bar from a message. This property works only with a file block; if this property is included along with other blocks (for example, a section block), the chat.unfurl method will throw an error.

Next, we call chat.unfurl with the channel from the link_shared event, and set the ts equal to the message_ts from the event:

curl -F token=<user_token> \
    -F ts=123456789.9875 \
    -F unfurls=<unfurls json from above> \
    -F channel=C12345 \
    -F user_auth_required=false \
    https://slack.com/api/chat.unfurl

Check out the chat.unfurl method documentation for more detail on how to use that method to its fullest potential.

Updating remote files

Your remote file's contents may change. When that happens, call the files.remote.update method to update your remote file:

curl -F token=<token> \
    -F tile=ACyclistTale \
    -F external_id=ABCD1 \
    https://slack.com/api/files.remote.update

You cannot update the external_id or file_id of a remote file. If you need to change those fields, your best bet is to remove and then add the file.

One other piece of pleasant news: adding a remote file is actually an "upsert" operation, meaning that if you add a file that has been added before, the existing file will be updated.

Removing remote files

Removing a remote file follows the same pattern as adding and updating. Use the files.remote.remove method to remove a remote file from Slack.

This method does not delete the remote file from where it's externally hosted; it only removes the remote file from Slack.

    curl -F token=<bot_token> \
    -F external_id=ABCD1 \
    https://slack.com/api/files.remote.remove

After removal, any place that used to display the file will show a tombstone message containing the text "This file was deleted." instead.

Recommended reading