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.
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.
- macOS
- Linux
- Windows
brew install superfaceai/cli/superface
npm install -g @superfaceai/cli@latest
npm install -g @superfaceai/cli@latest
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:
{
"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"
Turn the Comlink into code
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.
- Node.js
- Python
superface map slack slack-channels/fetch-channels
superface map slack slack-channels/fetch-channels python
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.
- Node.js
- Python
{
exclude_archived: true,
types: 'public_channel',
limit: 5
},
{
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.
- Node.js
- Python
superface execute slack slack slack-channels/fetch-channels
superface execute slack slack slack-channels/fetch-channels python
If everything is successful you will see a JSON response listing 5 public channels in your Slack workspace.