Slack Node.js SDK

0.3.2 · active · verified Sun Apr 19

slack-node is a Node.js library for interacting with the Slack API, supporting both incoming webhooks and the comprehensive Slack Web API. Currently at version 0.3.2, it maintains a policy of zero runtime dependencies, making it a lightweight choice for integrating Slack functionalities into Node.js applications. The library is designed for flexibility, offering both traditional callback-based asynchronous operations and modern Promise-based and async/await syntax, catering to various coding styles. While its version number suggests a pre-1.0 stability, the project claims continuous updates and full feature support. Its core differentiation lies in its minimalist approach to dependencies and its dual API consumption models.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates sending a message via an Incoming Webhook and calling a Slack Web API method (users.list and chat.postMessage) using `async/await` syntax, utilizing environment variables for credentials.

const Slack = require('slack-node');

async function main() {
  // Webhook usage
  const webhookUrl = process.env.SLACK_WEBHOOK_URL ?? '__your_webhook_url__';
  const slackWebhook = new Slack();
  slackWebhook.setWebhook(webhookUrl);

  if (webhookUrl === '__your_webhook_url__') {
    console.warn("Please provide a valid SLACK_WEBHOOK_URL environment variable or replace the placeholder.");
  } else {
    try {
      const webhookResponse = await slackWebhook.webhook({
        channel: "#general",
        username: "webhookbot",
        text: "Hello from async/await via webhook!",
        icon_emoji: ":ghost:"
      });
      console.log("Webhook message sent:", webhookResponse.data);
    } catch (err) {
      console.error("Webhook error:", err);
    }
  }

  // API usage
  const apiToken = process.env.SLACK_API_TOKEN ?? '__your_api_token__';
  if (apiToken === '__your_api_token__') {
    console.warn("Please provide a valid SLACK_API_TOKEN environment variable or replace the placeholder.");
  } else {
    const slackApi = new Slack(apiToken);
    try {
      const usersList = await slackApi.api("users.list");
      console.log("Users list:", usersList.members.map(u => u.real_name));

      const chatPostMessage = await slackApi.api("chat.postMessage", {
        text: "hello from async/await via API",
        channel: "#general"
      });
      console.log("API message sent (timestamp):", chatPostMessage.ts);
    } catch (err) {
      console.error("API error:", err);
    }
  }
}

main();

view raw JSON →