Slack Web API Client

1.1.11 · active · verified Tue Apr 21

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate the SlackAPIClient, configure it, and use it to post a message with Block Kit to a specified Slack channel, including basic error handling.

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();

view raw JSON →