Chat SDK
Run Chat SDK bots on Supabase with a Postgres state adapter, an inbox adapter for the in-app widget, and helpers that record Slack, WhatsApp and SMS messages in the inbox.
better-supabase/chat-sdk connects Chat SDK to
Supabase. It adds no tables of its own: the state lives in the
chat-sdk-state SQL module, and conversations live in
the inbox.
pnpm add chat
pnpm better-supabase add chat-sdk-state inboxchat is an optional peer (>=4.45.0 <5), so apps that don't run a bot
never install it. Every platform adapter, such as @chat-adapter/slack, is
the app's own dependency.
| Page | Covers |
|---|---|
| State | createSupabaseState, the StateAdapter on Postgres |
| Inbox adapter | inboxAdapter, a bot that answers in the in-app widget |
| Channels | webhook, inboundHandler, deliver, maintain and installations |
| Identities | botJobs, chatActions and approval prompts for linked chat users |
How a message flows
- A platform webhook reaches the route from
webhook(), which verifies it, stores it in the inbox's event table and answers at once. inboundHandler().drain()replays the stored event into Chat SDK. Your handlers callhandler.mirror(thread, message), which records the message in the inbox, so staff see it next to widget conversations.- A staff reply queues an
inbox_outboundjob;deliver()posts it with the platform adapter and records the delivery. - In
botmode, a contact's message queues aninbox_botjob instead, andinboxAdapter().dispatch()runs your bot's handlers on it. maintain()on a cron reopens snoozed conversations, sends the scheduled replies whose time came, replays events a crashed request left, purges old events and deletes expired state.
A minimal bot on the widget:
import "server-only";
import { Chat } from "chat";
import { createInbox, sqlTransport } from "better-supabase/blocks/inbox";
import { createSupabaseState, inboxAdapter } from "better-supabase/chat-sdk";
const transport = sqlTransport(postgres.asService());
export const inbox = createInbox({ transport });
export const inboxBot = inboxAdapter({ inbox, userName: "Acme bot" });
export const bot = new Chat({
userName: "Acme bot",
adapters: { inbox: inboxBot },
state: createSupabaseState({ transport }),
});
bot.onNewMention(async (thread, message) => {
await thread.post(`You wrote: ${message.text}`);
});Run the bot jobs from a jobs drain route. Declare
inbox_bot in your queues with the InboxBotJob shape
(conversation_id, message_id), then:
import "server-only";
import { inboxBot } from "@/lib/bot";
import { jobs } from "@/lib/jobs";
export const GET = jobs.drainRoute({
secret: process.env.CRON_SECRET,
handlers: { inbox_bot: (payload) => inboxBot.dispatch(payload) },
});Other storage and adapter features
inboundHandler, deliver and maintain accept a ChatInboundStore
instead of an Inbox. inboxStore(inbox) is the default. drain and
importFiles need the Inbox; with your own store, replay events through
handler.process.
To keep raw webhooks in the queue block's webhook inbox,
pass it as webhook({ sink, adapter }). A worker replays each stored message
with handler.process(chatEventOf(message)).
capabilitiesOf(name) returns what a platform adapter supports. It needs the
optional peer @chat-adapter/catalog and returns a CHAT_CATALOG_MISSING
error when it is not installed.
inboxAdapter handles actions, reactions and stop actions (through
abortTurn), uploads outgoing files, and marks bot messages with
ai_generated metadata. An empty stream returns an empty message instead of
posting twice. deliver takes a download function for platform files.
maxConcurrent limits turns per process, not across instances.
Credentials
Platform tokens never sit in the inbox tables. An installation stores a
credential_ref, and channelAdapter asks the
credential provider for the token each time
it builds an adapter, so a rotated or revoked token takes effect on the
next message.
Last updated on