Skip to content
All posts

Build a Slack bot that answers with AnyRouter

A Slack slash command is a short walk from a message to a model answer. This build uses one sk-ar-v1- key against AnyRouter's OpenAI-compatible endpoint, so routing and failover are handled for you.

Why put a model behind a slash command

Teams already live in Slack, so a slash command is often the cheapest way to put a model where the questions are. The hard part is not the Slack wiring, it is the model access: one key that reaches every provider, retries when one upstream fails, and a bill you can read.

AnyRouter sits between Slack and the providers, so the bot sends one request to one base URL and the gateway handles the rest.

What you need

  • A Slack app with a bot token, a signing secret, and a slash command called /anyr.
  • An AnyRouter API key from the dashboard. It starts with sk-ar-v1-.
  • Node 20 or newer, and the Bolt framework.

Create the Slack app at https://api.slack.com/apps, add the slash command, install it to your workspace, and copy the two credentials.

The handler

Ack the command first, then call the OpenAI-compatible chat endpoint. The model defaults to anyrouter/free, which picks the best free model available:

The whole bot is one command handler around a chat request
import { App } from "@slack/bolt";

const app = new App({
  token: process.env.SLACK_BOT_TOKEN,
  signingSecret: process.env.SLACK_SIGNING_SECRET,
});

app.command("/anyr", async ({ command, ack, say }) => {
  await ack();
  const prompt = command.text?.trim();
  if (!prompt) {
    await say("Usage: /anyr <your question>");
    return;
  }
  const response = await fetch("https://anyrouter.dev/api/v1/chat/completions", {
    method: "POST",
    headers: {
      "content-type": "application/json",
      authorization: `Bearer ${process.env.ANYROUTER_API_KEY}`,
    },
    body: JSON.stringify({
      model: "anyrouter/free",
      messages: [{ role: "user", content: prompt }],
    }),
  });
  const body = await response.json();
  await say(body.choices[0].message.content);
});

await app.start(Number(process.env.PORT ?? 3000));

A working, tested version of this bot lives in the AnyRouterHQ/anyrouter-slack-bot repo.

Get the demo repo

Run it

Install, export the three values, and start the app:

Set the credentials and start Bolt
npm install @slack/bolt
export SLACK_BOT_TOKEN=xoxb-...
export SLACK_SIGNING_SECRET=...
export ANYROUTER_API_KEY=sk-ar-v1-...
npm start

For local development, point the slash command at a tunnel to your machine. In production, deploy the same process behind the public URL Slack expects.

Where to take it next

The same pattern works for a channel assistant, a support triage bot, or an on-call helper. Pin a model per command with the model field, add a usage log, and keep the key on the server so it never reaches a browser.

Free models are $0 per token on Go and up, with no daily cap, which makes a team bot cheap to run. Bring your own provider keys and the markup stays $0.

Pick a plan and start

Create an account, choose Go, Pro, Pro+, or Max, pay on the next screen, then call the model.