Start tracking your progress
Trailhead Home
Trailhead Home

Get to Know the Slack APIs

Learning Objectives

After completing this module, you’ll be able to:

  • Explain the differences between the Slack Web API and the Slack Events API.
  • Identify use cases for the Slack Web API.
  • Identify use cases for the Slack Events API.

Introduction to Slack APIs

No adventurer can actually start without knowing which road to take. In the case of building Slack Apps, Slack provides a great map in the form of APIs.

Slack APIs are the means by which you send data to and from Slack.

There are many Slack APIs available to use. For now, we focus on the core APIs needed for most common use cases—the Slack Web API and the Slack Events API

The Web API

With the Web API, you can query Slack to both get information and to enact change within Slack itself.

Some examples include:

  • Sending and deleting messages
  • Marking users as active
  • Creating groups
  • Adding reactions
  • Renaming channels
  • Retrieving threads of messages
  • Retrieving URLs for messages

And more.

The way an API user actually accomplishes these is to use the Web API methods. A method on the Slack API is something that gives you a means by which to talk to the Slack API and tell it to do things. These methods usually come in the form of a URL you can send data to.

The following example uses curl, a command line–based tool whose purpose is to transfer data on the Internet.

curl -X POST -H 'Authorization: Bearer xoxb-1234-56789abcdefghijklmnop' \
-H 'Content-type: application/json' \
--data '{"channel":"C12345678","text":"Hello, Trailhead!"}' \
https://slack.com/api/chat.postMessage

Let’s break this down.

  1. You’re presenting Slack with an authorization token in order to let the Web API know that you’ve got permission to send a message. This is the first line of code above.
  2. Then, you’re letting Slack know that your request is in JSON format, that’s the Content-type:.
  3. With the data field on the next line, you’re telling Slack’s Web API to send a message to channel C12345678 that says “Hello, Trailhead!”.
  4. Finally, you’re choosing the correct API method to actually send this message off—chat.postMessage. The method family is chat, which is the collection of methods that have to do with posting messages. The method you’re using is postMessage, which tells the API to send a new message.
Note

Note

To help adventurers, there is a large collection of documented methods available. Make sure to check out this documentation when you decide to venture out and build a Slack App on your own!

To sum it up, if you want to get some relatively static data from Slack, or you want to send to or change something in Slack, use the Web API.

The Events API

Slack is a where conversations take place. Sometimes, you need your app to listen to these conversations before taking action. The Events API is there to help you listen to conversations in real time and act accordingly.

A good example is an app that tracks when a new user has joined. Here’s what happens on a high level when you code an app to do this with the Events API. 

  1. You set up a server to listen for when a user joins the team—the ‘team_join’ event.
  2. On the Slack website, you set up your app(s) to respond to that event.
  3. Once a user actually joins your team, Slack sends your server information about the new user, including their user ID, their user name, and their locale, and so on.
  4. Now that your server has the data about the new user, it can respond however you like with this data.

With the Events API, you can track and act upon things like:

  • When a user removes a star from a message
  • When a file gets shared
  • When messages with specific phrases have been posted
  • When your app or bot has been mentioned
Note

Note

Like with the Web API, Slack has a good amount of documentation related to events you can track.

In general, use the Events API when you want to track things going on in Slack and offer help or useful content to users in real time.

A Word on the APIs

You can certainly use just the Web API if you want to periodically send and receive data without the real-time aspect.

And you can certainly just use the Events API if you want to track real-time data coming into your instance of Slack.

But it doesn’t have to just be exclusive—most great apps use both! You can use the Events API to track data coming in, and you can follow up by using the Web API to respond on Slack in some way.

Conclusion

Congratulations! You know now what Slack Apps can do for you and how they can help you cultivate a productive and collaborative workplace.

The world of Slack Apps can still seem like a daunting place, but now you know the essentials of the craft.

The next step is to see your newfound knowledge in action. In the next module on this trail, you see how to actually build an app step by step.

Resources

retargeting