How to set up and use the Slack API to extract, automate, and enhance your Slack workspace using Slack API?
Hi, hope you are doing well. Thank you so much for stopping by. It’s another day to learn something fruitful. To give you an idea of what we will be learning here. The guide will help you get familiar with the Slack App and Slack API. The guide will explain to you how to create a Slack app, ways to set up a Slack app to use Slack API, and generate API tokens (access token and refresh token, etc)—lastly, test the Slack API access token using a Python code.
What is Slack?
Slack is a messaging app developed by Slack Technologies. Slack is used for professional and organizational communications. That is why Slack is mostly for business, to bring people together to work as one unified team.
I have been a user of Slack for the last 3 years, and I just love it. Slack makes things easy and makes me feel connected with my team which is distributed across different time zones. Over the past 3 years, slack has come a long way, and has introduced several highly usable and practical features like a huddle, short video messages, etc.
You can learn more about Slack from Slack (Software) – Wikipedia and Slack – official website.
What is Slack API?
Slack API enables you to reduce or eliminate repetitive and manual processes like onboarding, collecting feedback, sending messages to multiple groups, etc. It helps you bring data and tasks from external services and tools to teams, groups, and workspaces.
Slack API provides several options like reading, writing, and updating many kinds of data in and around the Slack workspace. The two major API provided by Slack is Web API and Events API.
What is the Slack Web API?
The Web API of Slack is not a REST API. The web API of Slack is an HTTP RPC method. Slack provides HTTP methods to enable several Slack app functionalities.
What is HTTP-RPC?
In brief, HTTP-RPC is a Remote Procedure Call over HTTP (RPC over HTTP) is a Microsoft protocol that enables Microsoft Outlook clients to access Microsoft Exchange servers over HTTP. It is used to communicate RPC traffic over an HTTP connection.
HTTP-RPC is an open-source framework for simplifying the development of REST-based applications. It allows developers to create and access HTTP-based web services using a convenient, RPC-like metaphor while preserving fundamental REST principles such as statelessness and uniform resource access.
You can learn more about it from Link1 and Link2.
What is the Slack Event API?
Event API is like a webhook, getting a response triggered based on activities of the slack workspace. Event-based API works similarly to subscription modal for almost all platforms used nowadays, especially YouTube. You subscribe to a channel, and you get a notification whenever a new video gets posted.
Similarly, you can create a Slack app, subscribe to an event from a range of possibilities, and configure the Slack app to react to those events according to your requirements.
Okay, so now let’s dive directly into the main topic!
1. Create a Slack App
To create a Slack app go to create a Slack app. You need to log in to your Slack account before you can even start creating a Slack app.
After you are logged in to Slack. Now click on “Create New App”. It will look like the below image.
Next, select the “From scratch” options type in the App name, and select a workspace. In my case, I will name my app “setup-demo”.
The last step of creating a Slack app is clicking on “Create App”. Don’t forget to do that!
2. Generate Slack app credentials – especially access tokens
There are two ways to generate credentials: the first is a manual process and the other is using Python code.
In this, I am going to describe the manual process.
To generate credentials, you need to install the app in your workspace. To do so first you need to add the permission scope you will need. Choose the permission scope wisely from the list of scopes. Since the app’s tokens will be able to extract data based on that.
Note that there are mainly two types of scope: bot scope and user scope. That said there are several kinds of tokens too. You can know about the token from the token type.
This guide will focus on user tokens and user scope. You can contact me to figure out solutions for other token types and scope types.
If you have any questions, need further guidance, or seek consulting, feel free to get in touch with me. You can reach out to me through this Contact Form to discuss your specific needs and requirements. I can automate this process and make it more scalable and easier.
Let’s come back to the topic
2.1: Adding permission scope to your Slack app
To add permission scope to a Slack app you need to click on “permission scope” or you can click on OAuth & Permissions from the left-hand side menu. You don’t need to do this step if you are going through Python code to generate a token.
Next, scroll down, and in the Scopes section you will see “Bot Token Scopes” and “User Token Scopes”. As I told you previously there are two types of scopes you can add to your Slack app and in this post, we will be covering only a bunch of Slack user token scopes.
2.2: Adding User Tokens Scopes
To add a user token scope to your Slack app click on “Add an OAuth Scope‘. Search and add profile, email, and identity.basic scope.
2.3: Installing the Slack app to your workspace to generate an access token and refresh token
To install the app to your workspace click on “Install to Workspace”. You find the button on the same page where you were adding the above scope.
After clicking on Install to the workspace you redirect to the user consent step where you need to confirm the scope and click on the “Allow” button. You can confirm if you are going right from the image below.
Soon after clicking on the allow button on the above screen, you will be redirected to your Slack app page and you will see a token in OAuth Tokens for Your Workspace section. Same as the image below.
Note: since we are using user permission scope the token generated starts with xoxp. If we were using bot permission scope we would have got xoxb. That doesn’t mean you can’t use both, you can use both the permission scope at the same time.
Another thing to note is that you don’t see a refresh token, you will get a refresh token if you have installed and generated tokens using Python code.
Let’s continue with the next step. Copy the OAuth token and paste it into the below Python code to test your Slack App and API setup and its scope working.
3. Testing the Slack App token and making the first Slack API call using Python code
Understand the below code and create a test code. I named the Python code file below -> “slack_first_api-call.py”.
#!/usr/local/bin/python3 # command to run this code $ python3 ./slack_first_api_call.py import sys import json import requests def first_api_call(access_token): try: url = "https://slack.com/api/users.identity" headers = {"Authorization": "Bearer "+access_token} r = requests.get(url = url, headers = headers) response_dict = json.loads(r.text) user_dt = response_dict["user"] print("\n Function first_api_call successfully\n") return user_dt except: print("\n Function first_api_call Failed : ",sys.exc_info()) if __name__ == '__main__': try: print("\n Slack API first call") access_token = "replace with your slack app token" user_details = first_api_call(access_token) print(user_details) print("\n name : ", user_details["name"]) print(" id : ", user_details["id"]) print(" email : ", user_details["email"]) print("\n Slack API first call \n") except: print("\n Slack API first call ", sys.exc_info())
By running the above file by using the command python3 ./slack_first_api_call.py. You should see your Slack User data printed in the console.
Congratulation! you have successfully created Slack App and set it up for Slack API. Hope I have made the entire process of generating Slack App (slack API) Tokens and using them to extract automate and enhance your Slack workspace and channels.
If you like this article and think it was easy to understand and might help someone you know, do share it with them. If you want my help, you can reach out to me through this Contact Form to discuss your specific needs and requirements. Thank You! See you soon.
For any suggestions or doubts ~ Get In Touch
Checkout out my other API Integration and Setup Guide