Slack Web API Client
The `slack-web-api-client` is a fetch-based, type-safe client library for interacting with the Slack Web API, designed for broad runtime compatibility across environments like Node.js, Deno, and Cloudflare Workers. Currently stable at version 1.1.11, it is part of the `slack-edge` ecosystem and is utilized internally by the `slack-edge` library itself. A key differentiator is its "zero additional dependencies" approach, minimizing install footprint and potential conflicts, which simplifies integration and reduces supply chain risks. It provides strong TypeScript types for all API responses and Block Kit elements, significantly enhancing the developer experience with predictive coding and robust error checking. The project appears to be actively maintained, with updates available via `npm i slack-web-api-client@latest`. Its fetch-based implementation makes it highly portable, a significant advantage over clients relying on Node.js-specific HTTP modules.
Common errors
-
SlackAPIError: The request to the Slack API failed. (response: { ok: false, error: 'invalid_auth' })cause The Slack API token provided to the client is missing, invalid, or has insufficient scopes.fixVerify that your `SLACK_BOT_TOKEN` environment variable (or hardcoded token) is correctly set and starts with `xoxb-` or `xoxp-`. Ensure your Slack app has the necessary OAuth scopes for the API method being called (e.g., `chat:write` for `chat.postMessage`). -
TypeError: client.chat is not a function
cause This error typically indicates that the `SlackAPIClient` instance was not properly initialized or the method chain is incorrect.fixDouble-check that `new SlackAPIClient(token)` was called with a valid token. Confirm that the API method path (e.g., `chat.postMessage`) matches the Slack Web API documentation and the client's exposed methods.
Warnings
- gotcha By default, the `SlackAPIClient` is configured to throw a `SlackAPIError` when a Slack API call returns `ok: false` in its response. Developers accustomed to checking the `ok` property directly or receiving a structured error object without an exception might find this behavior unexpected.
- gotcha The client requires a valid Slack Bot Token (e.g., `xoxb-YOUR_TOKEN`) or User Token (e.g., `xoxp-YOUR_TOKEN`) for authentication. Using an invalid, expired, or improperly scoped token will result in API errors.
- gotcha While the library is fetch-based and designed for various runtimes, specific runtime environments (like Cloudflare Workers or Deno) may require slightly different setup or polyfills for `process.env` access, depending on how environment variables are exposed.
Install
-
npm install slack-web-api-client -
yarn add slack-web-api-client -
pnpm add slack-web-api-client
Imports
- SlackAPIClient
const SlackAPIClient = require('slack-web-api-client').SlackAPIClient;import { SlackAPIClient } from 'slack-web-api-client'; - SlackAPIError
import SlackAPIError from 'slack-web-api-client/errors';
import { SlackAPIError } from 'slack-web-api-client'; - WebClientOptions
import { Options } from 'slack-web-api-client';import { WebClientOptions } from 'slack-web-api-client';
Quickstart
import { SlackAPIClient } from "slack-web-api-client";
// Ensure SLACK_BOT_TOKEN is set in your environment
const token = process.env.SLACK_BOT_TOKEN ?? '';
const client = new SlackAPIClient(token, {
// Log level can be set to 'DEBUG', 'INFO' (default), 'WARN', 'ERROR'
logLevel: "INFO",
// By default, the client throws SlackAPIError for API failures (ok: false)
throwSlackAPIError: true
});
async function postGreeting() {
if (!token) {
console.error('SLACK_BOT_TOKEN environment variable is not set.');
return;
}
try {
const response = await client.chat.postMessage({
channel: "#general", // Replace with your desired channel ID or name
text: ":wave: Hello from slack-web-api-client!",
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Hey there! I just posted this message using the `slack-web-api-client` library. :rocket:"
}
},
{
"type": "actions",
"elements": [
{
"type": "button",
"text": {
"type": "plain_text",
"text": "View GitHub"
},
"url": "https://github.com/slack-edge/slack-web-api-client"
}
]
}
]
});
console.log('Message posted:', response.ts);
} catch (error) {
if (error instanceof SlackAPIClient) {
console.error('Slack API Error:', error.code, error.response);
} else {
console.error('An unexpected error occurred:', error);
}
}
}
postGreeting();