DEV Community

Cover image for Setting up Node.js Integration to Slack using OAuth Slack
Tyler Steck
Tyler Steck

Posted on

Setting up Node.js Integration to Slack using OAuth Slack

Slack is a popular messaging platform used by many teams to communicate and collaborate. As a developer, you can use Slack's APIs to build integrations that enable your applications to interact with Slack channels, messages, and users. In this article, we will walk through how to set up a Node.js integration to Slack using OAuth.

Overview

The OAuth flow allows you to authenticate a user's Slack account and obtain an access token that can be used to interact with Slack on their behalf. Here are the steps involved in setting up a Node.js integration to Slack using OAuth:

  1. Create a Slack app
  2. Set up OAuth scopes
  3. Install the app to your workspace
  4. Obtain an access token
  5. Use the access token to interact with Slack's API

We'll go through each of these steps in detail below.

Step 1: Create a Slack App

The first step is to create a Slack app. To do this, follow these steps:

  1. Go to the Slack API website
  2. Click on "Create an App"
  3. Give your app a name and select the workspace where you want to install it
  4. Click "Create App"

Once you've created your app, you'll be taken to the app's settings page.

Step 2: Set up OAuth Scopes

OAuth scopes define the permissions that your app needs to interact with Slack's API. To set up OAuth scopes, follow these steps:

  1. Click on "OAuth & Permissions" in the left-hand menu
  2. Under "Scopes", click "Add an OAuth Scope"
  3. Select the scopes you want to add to your app. For example, if you want your app to read and write messages in a user's Slack channel, you'll need to select the channels:read and chat:write scopes.
  4. Click "Save Changes"

Step 3: Install the App to Your Workspace

To install your app to your workspace, follow these steps:

  1. Click on "Install App" in the left-hand menu
  2. Click "Install App to Workspace"
  3. Review the OAuth scopes that your app is requesting
  4. Click "Allow"

Once you've installed your app to your workspace, you'll be provided with an OAuth access token that you can use to interact with Slack's API on behalf of the user.

Step 4: Obtain an Access Token

To obtain an access token, you'll need to use Slack's OAuth 2.0 flow. Here are the steps:

Redirect the user to Slack's OAuth authorization page. The URL should look something like this: https://slack.com/oauth/v2/authorize?client_id=<your_client_id>&scope=<your_scopes>&redirect_uri=<your_redirect_uri>
The user will be prompted to authorize your app to access their Slack account. Once they accept, Slack will redirect them back to your app with a temporary code.
Exchange the temporary code for an access token by making a POST request to https://slack.com/api/oauth.v2.access. You'll need to include your app's client ID, client secret, and the temporary code in the request body. For example:

const response = await fetch('https://slack.com/api/oauth.v2.access', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer <your_bot_token>`
  },
  body: JSON.stringify({
    client_id: '<your_client_id>',
    client_secret: '<your_client_secret>',
    code: '<temporary_code>',
    redirect_uri: '<your_redirect
Enter fullscreen mode Exit fullscreen mode

Top comments (0)