POST /api/chat
Streams a Gemini chat response as Server-Sent Events (SSE). When RAG is enabled and auserId is provided, the server embeds the latest user message, searches the Qdrant vector store for the five most relevant document chunks (score threshold 0.4), prepends that context to the prompt, and emits a sources event before the first text chunk.
The route has a maximum execution time of 60 seconds.
Request body
Conversation history. Each item must have a
role and content.The Appwrite user ID. Required for RAG context retrieval. If omitted, the
model answers without document context.
Set to
false to skip vector search and answer from the model’s general
knowledge only.Response — SSE stream
The response hasContent-Type: text/event-stream. Each event is a line starting with data: followed by a JSON payload, terminated by \n\n.
Event order:
sources— emitted once at the start, only when relevant document chunks are found.chunk— emitted repeatedly as the model generates text.[DONE]— signals the end of the stream.
Array of document chunks used as context.
A partial text fragment from the model output. Concatenate all chunks to
reconstruct the full response.
Example
Use
--no-buffer with curl to see SSE events as they arrive instead of
waiting for the full stream to complete.POST /api/chat/generate-title
Generates a short title (maximum 5 words) for a chat session by passing the first few user messages to Gemini. Falls back to"New Chat" if generation fails.
Request body
The conversation messages. The endpoint reads only the first three user-role
messages. Uses the same
{ role, content } shape as /api/chat.Response
Generated session title, up to 50 characters, with quotes stripped.
Errors
| Status | Condition |
|---|---|
400 | messages is missing or empty |
500 | Gemini API error |
Example
GET /api/chat/history
Returns up to 100 chat sessions for a user, ordered by most recently updated.Query parameters
The Appwrite user ID whose sessions to fetch.
Response
List of chat session documents from Appwrite.
Errors
| Status | Condition |
|---|---|
400 | userId query parameter is missing |
500 | Appwrite database error |
Example
POST /api/chat/history
Creates a new chat session document in Appwrite.Request body
The Appwrite user ID to associate with the session.
Initial message array. Stored as a JSON string in Appwrite.
Display title for the session.
Appwrite file ID of a document to pin to this session.
Response
The newly created Appwrite document. Includes all session fields listed under
GET /api/chat/history.Errors
| Status | Condition |
|---|---|
400 | userId or messages is missing |
500 | Appwrite database error |
Example
PUT /api/chat/history
Updates an existing chat session. Only the fields you include in the body are changed; omitted fields are left unchanged.Request body
Appwrite document ID of the session to update.
New display title.
Replacement message array. Overwrites the stored messages entirely.
New pinned document file ID, or
null to clear.Response
Errors
| Status | Condition |
|---|---|
400 | sessionId is missing |
500 | Appwrite database error |
Example
DELETE /api/chat/history
Permanently deletes a chat session from Appwrite.Query parameters
Appwrite document ID of the session to delete.
Response
Errors
| Status | Condition |
|---|---|
400 | sessionId query parameter is missing |
500 | Appwrite database error |