@saulo.martins/chat.api
v1.0.0
Published
Express chat API with Supabase, OpenAI and rate limiting
Downloads
40
Maintainers
Readme
chat-api
Express chat API with Supabase (conversations + messages + system prompt table), OpenAI, and rate limiting. Mount in your app or run standalone. Use with chat on the frontend.
Install
npm install chat-api
# or from Git
npm install git+ssh://[email protected]:saulommartins/chat.api.gitDatabase (Supabase)
Run the migrations in your Supabase SQL Editor (in order):
migrations/01-conversations-messages.sql— createsconversationsandmessagesmigrations/02-chat_system_prompt.sql— createschat_system_prompt(single row, id=1). Edit the row content to set your assistant behavior and optional WhatsApp link.
Mount in your Express app
const express = require('express');
const { createClient } = require('@supabase/supabase-js');
const { createChatRouter } = require('chat-api');
const supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_SERVICE_KEY
);
const chatRouter = createChatRouter({
supabase,
openaiApiKey: process.env.OPENAI_API_KEY,
rateLimitWindowMs: Number(process.env.RATE_LIMIT_CHAT_WINDOW_MS) || 60000,
rateLimitMax: Number(process.env.RATE_LIMIT_CHAT_MAX) || 10,
// optional: rateLimitMessage, conversationsTable, messagesTable, systemPromptTable, openaiModel, openaiMaxTokens, openaiTemperature
});
const app = express();
app.use(express.json());
app.use('/api/chat', chatRouter);Options (ChatApiOptions)
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| supabase | SupabaseClient | required | Supabase client (service role). |
| openaiApiKey | string | required | OpenAI API key. |
| rateLimitWindowMs | number | 60000 | Rate limit window (ms). |
| rateLimitMax | number | 10 | Max POST requests per window per IP. |
| rateLimitMessage | string | (en) | JSON message when limit exceeded. |
| conversationsTable | string | conversations | Table name. |
| messagesTable | string | messages | Table name. |
| systemPromptTable | string | chat_system_prompt | Table name (single row, id=1). |
| openaiModel | string | gpt-3.5-turbo | OpenAI model. |
| openaiMaxTokens | number | 500 | Max tokens per reply. |
| openaiTemperature | number | 0.7 | Temperature. |
API contract
- POST /api/chat — Body:
{ message: string, conversationId?: string }. Returns{ conversationId, reply, message }. Rate limited. - GET /api/chat/:conversationId — Returns
{ conversationId, messages }.
