Testing the Slack Web API and bot tokens
If you build a Slack app or bot, you'll call the Slack Web API a lot: post a message, update it, look up a user, open a modal. It has one quirk that catches everyone: almost every call returns 200 OK, even when it fails. The real result is in the ok field of the body.
So a test that only checks the status code passes on every broken call. Here's how to set it up properly.
Step 1: A bot token
Create an app at api.slack.com, add the bot scopes you need under OAuth & Permissions (for this guide: chat:write and channels:read), install it to a test workspace and copy the Bot User OAuth Token. It starts with xoxb-.
| Variable | Value |
|---|---|
baseUrl | https://slack.com/api |
slackToken | xoxb-…, marked as secret |
channelId | the ID of a test channel, such as C0123456789 |
On the collection's Authorization tab, choose Bearer token with {{slackToken}}.
Step 2: Check the token
POST {{baseUrl}}/auth.test{"ok": true, "url": "https://your-team.slack.com/", "team": "Your Team", "user": "testbot", "team_id": "T…", "user_id": "U…", "bot_id": "B…"}Step 3: Test ok, everywhere
Put this in the collection's post-response script, so every request checks it:
pm.test('Slack call succeeded', () => {
const body = pm.response.json();
// On failure, log Slack's reason: missing_scope, channel_not_found…
if (!body.ok) console.log(`Slack error: ${body.error}`);
pm.expect(body.ok).to.equal(true);
});Step 4: Post a message
POST {{baseUrl}}/chat.postMessage
Content-Type: application/json; charset=utf-8
{
"channel": "{{channelId}}",
"text": "Deploy of orders-api finished",
"blocks": [
{"type": "section", "text": {"type": "mrkdwn", "text": "*Deploy finished* :white_check_mark:\norders-api `v1.4.2` is live on staging."}}
]
}pm.environment.set('messageTs', pm.response.json().ts);Slack identifies a message by its channel and its ts (timestamp). Store it to update or delete the message later:
POST {{baseUrl}}/chat.update
Content-Type: application/json; charset=utf-8
{"channel": "{{channelId}}", "ts": "{{messageTs}}", "text": "Deploy of orders-api finished (updated)"}Step 5: The errors you'll meet
Duplicate requests to see each error once, so you know what your code has to handle:
error | Cause |
|---|---|
invalid_auth, not_authed | Wrong or missing token |
missing_scope | The token lacks a scope; the response lists needed and provided |
channel_not_found | Wrong channel ID, or a private channel the bot can't see |
not_in_channel | The bot isn't a member; invite it with /invite @yourbot |
invalid_blocks | A malformed Block Kit payload |
// "chat.postMessage (no scope)": expected failure
pm.test('reports the missing scope', () => {
const body = pm.response.json();
pm.expect(body.ok).to.equal(false);
pm.expect(body.error).to.equal('missing_scope');
});For deliberate-failure requests like this one, skip the collection-level ok check: put the shared check in a folder's post-response script instead, and keep the expected failures in a separate folder.
Step 6: Rate limits
Slack limits each method separately, and chat.postMessage allows roughly one message per second per channel. Over the limit, you get a real 429 with a Retry-After header in seconds. In a test run that posts many messages, add a delay:
requestarium run-collection "Slack" --env SlackTest --delay 1100 --env-var slackToken=SLACK_TEST_TOKEN