Skip to main content

Slack

In this example, we will show you how you can get started working with the Slack API to produce a working use case that allows you to list 5 public channels in your Slack workspace. Here we are using an endpoint from Slack's Web API that requires a more minimal level of authentication.

Before you start

You'll need a Slack API token that is valid for the workspace you want to access. Find out how to set that up in their Getting a token guide.

Install & Authorize Superface

The Superface CLI provides all the tooling needed to author the Comlinks for your integration.

Install with Homebrew
brew install superfaceai/cli/superface

If you don't have a Superface account already, you can sign up here. You can use your account to authenticate the CLI.

superface login

Prepare the documentation

The Slack documentation is available in OAS format. You can find the Open API Specs for their APIs on the Slack GitHub. Here we are using the OAS for their Web API.

superface prepare https://raw.githubusercontent.com/slackapi/slack-api-specs/master/web-api/slack_web_openapi_v2.json slack

Next, you will need to edit the slack.provider.json that has been created to ensure that the correct API endpoint and security policy are used. The exact provider definition you need to use is below:

slack.provider.json
{
"name": "slack",
"defaultService": "default",
"services": [
{
"id": "default",
"baseUrl": "https://slack.com/api"
}
],
"securitySchemes": [
{
"id": "slackBearerToken",
"type": "http",
"scheme": "bearer"
}
],
"parameters": []
}

Define your use case

superface new slack "list all channels"

To turn the Comlink profile into runnable code in either Node.js or Python, use the following command and include the name of the provider and a use case profile you want to work with.

superface map slack slack-channels/fetch-channels

Set up your inputs

In order to process this request correctly, you will need to modify the boilerplate code from the Slack documentation to remove some values. From the superface folder, open slack-channels.fetch-channels.slack.mjs (or .py if you created Python files) and modify the object in the usecase.perform() function.

slack-channels.fetch-channels.slack.mjs
{
exclude_archived: true,
types: 'public_channel',
limit: 5
},

Add your API key

If you don't already have your Slack API token in your environment, you can add it to your .env file as SLACK_TOKEN=<your-slack-api-token>".

Test it

To test this use case you can use the execute command.

superface execute slack slack-channels/fetch-channels

If everything is successful you will see a JSON response listing 5 public channels in your Slack workspace.