DEV Community

APIVAI
APIVAI

Posted on • Originally published at apivai.com

How to Build a Slack AI Bot with APIVAI (Cheap Claude & GPT)

Build a Slack AI bot with APIVAI

A Slack AI bot lets your workspace ask Claude or GPT from any channel. Create a Slack app, subscribe
to message events, and forward them to an OpenAI-compatible model. APIVAI provides the model cheaply.

1. Create the Slack app

  • api.slack.com/apps → Create New App → enable Bot Token Scopes (app_mentions:read, chat:write) → install to your workspace → copy the Bot Token (xoxb-...).
  • Enable Event Subscriptions and subscribe to app_mention.

2. The server (Node)

import express from "express";
import OpenAI from "openai";
const app = express(); app.use(express.json());
const ai = new OpenAI({ apiKey: process.env.APIVAI_API_KEY, baseURL: "https://api.apivai.com/v1" });

app.post("/slack/events", async (req, res) => {
  if (req.body.type === "url_verification") return res.send(req.body.challenge); // Slack handshake
  res.sendStatus(200);
  const e = req.body.event;
  if (e?.type === "app_mention") {
    const prompt = e.text.replace(/<@[^>]+>/g, "").trim();
    const r = await ai.chat.completions.create({ model: "gpt-5.5", messages: [{ role: "user", content: prompt }], max_tokens: 500 });
    await fetch("https://slack.com/api/chat.postMessage", {
      method: "POST",
      headers: { Authorization: `Bearer ${process.env.SLACK_BOT_TOKEN}`, "Content-Type": "application/json" },
      body: JSON.stringify({ channel: e.channel, text: r.choices[0].message.content }),
    });
  }
});
app.listen(3000);
Enter fullscreen mode Exit fullscreen mode

Set SLACK_BOT_TOKEN + APIVAI_API_KEY, expose the URL to Slack, and @mention the bot.

Polish

  • Reply in a thread (thread_ts) to keep channels tidy.
  • Keep short per-thread history for context; trim to control cost.
  • GPT-5.5 for general Q&A; Claude Sonnet for code.

FAQ

Can a Slack bot use APIVAI? Yes — it's an OpenAI chat call with base URL
https://api.apivai.com/v1; the bot forwards the mention and posts the reply.

Which model? GPT-5.5 for general/multilingual; Claude Sonnet for code-heavy help.

How do I control cost? Trim history and cap max_tokens; APIVAI's per-token price is low.

Get started

Create the Slack app, run the server with your APIVAI key, and @mention the bot. Examples:
APIVAI examples repo.

Top comments (0)