Originally published at claudeguide.io/claude-api-slack-bot-guide
How Do I Build a Slack Bot Powered by Claude API?
Building a Claude-powered Slack bot takes about 2 hours. You need a Slack app (Bolt.js), an Anthropic API key, and a Node.js server. The bot listens for @mentions or messages in designated channels, forwards the text to Claude API, and posts the reply back to the same thread. With @slack/bolt and the @anthropic-ai/sdk packages, the integration is under 150 lines of TypeScript. This guide walks through Slack app setup, event handling, Claude response generation, conversation threading, slash commands, and rate limiting — with a cost analysis table so you can estimate monthly spend before you ship.
1. Slack App Setup with Bolt.js
Create the Slack App
- Go to api.slack.com/apps and click Create New App → From Scratch.
- Enable Socket Mode (Settings → Socket Mode) and generate an App-Level Token with
connections:writescope — store it asSLACK_APP_TOKEN. - Under OAuth & Permissions, add these Bot Token scopes:
-
app_mentions:read— receive @mention events -
channels:history— read channel messages -
chat:write— post messages -
commands— handle slash commands
-
- Under Event Subscriptions → Subscribe to Bot Events, add
app_mentionandmessage.im. - Install the app to your workspace and copy the Bot User OAuth Token (
SLACK_BOT_TOKEN).
Install dependencies
npm init -y
npm install @slack/bolt @anthropic-ai/sdk
npm install -D typescript ts-node @types/node
Environment variables
# .env
SLACK_BOT_TOKEN=xoxb-...
SLACK_APP_TOKEN=xapp-...
ANTHROPIC_API_KEY=sk-ant-...
2. Event Handling — Messages and Mentions
Create src/index.ts:
import Bolt, { App } from "@slack/bolt";
import Anthropic from "@anthropic-ai/sdk";
const app = new App({
token: process.env.SLACK_BOT_TOKEN!,
appToken: process.env.SLACK_APP_TOKEN!,
socketMode: true,
});
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY! });
// Handle @mentions in channels
app.event("app_mention", async ({ event, say }) =
---
## 6. Rate Limiting and Cost Control
Uncontrolled bots can burn API budget fast. Apply two layers of protection:
### Token budget per response
typescript
const MAX_INPUT_TOKENS = 4096; // guard against huge thread histories
async function getClaudeSafe(messages: Anthropic.MessageParam[]): Promise<string
Frequently Asked Questions
Can I use Claude with Slack's HTTP mode instead of Socket Mode?
Yes. Replace socketMode: true and appToken with a public HTTPS URL as the Request URL in your Slack app settings. Bolt.js handles both modes with the same event handlers. Socket Mode is easier for local development since it avoids exposing a public endpoint.
How do I prevent the bot from responding to its own messages?
Bolt.js automatically filters out bot messages for message events. For app_mention, check if (event.bot_id) return; at the start of your handler. Also check msg.subtype === "bot_message" in message listeners.
What model should I use for a Slack bot — Haiku or Sonnet?
Use Claude Haiku for general Q&A, summaries, and simple tasks (the vast majority of Slack bot usage). Switch to Sonnet for code review, complex analysis, or anything requiring multi-step reasoning. You can route dynamically: check if the message contains code blocks or technical keywords and select the model accordingly. See Claude Haiku vs Sonnet vs Opus for the full comparison.
How do I give the bot access to company data (e.g., Notion, Jira)?
Use Claude's tool use feature (function calling). Define tools that query your internal APIs, then pass tools to anthropic.messages.create. Claude will call the tool when needed, you execute it and return the result, and Claude incorporates the data in its reply. The Claude Agent SDK Guide covers tool use patterns in detail.
How do I deploy the bot to production?
For Socket Mode bots, any Node.js hosting works: Fly.io, Railway, Render, or a plain VPS. The bot maintains a WebSocket connection to Slack — no public ingress needed. Set your environment variables (SLACK_BOT_TOKEN, SLACK_APP_TOKEN, ANTHROPIC_API_KEY) in your hosting platform's secrets manager. For high-availability, run at least two instances and let Slack distribute events.
Top comments (0)