Overview
Chat Sessions endpoints allow you to retrieve, list, and delete AI chatbot conversation history. Each session represents a conversation thread between a student and the SkillRise AI Assistant. All sessions are user-specific and secured by user authentication to ensure privacy.GET /api/user/chat/:sessionId
Retrieve the complete message history for a specific chat session.Authentication
Requires JWT authentication token in the Authorization header.URL Parameters
The unique session identifier (UUID v4 format).
Response
MongoDB document ID for the chat session.
Array of all messages in the conversation.
Response Example
Error Responses
Implementation Notes
- Uses MongoDB aggregation pipeline for efficient retrieval
- Matches both
sessionIdanduserIdto ensure security - Returns only
_idandmessagesfields (excludes metadata) - System messages (from context building) are included in the response
DELETE /api/user/chat/:sessionId
Delete a specific chat session and all its messages.Authentication
Requires JWT authentication token in the Authorization header.URL Parameters
The unique session identifier to delete.
Response
Indicates whether the deletion was successful.
Confirmation message.
Response Example
Error Responses
Implementation Notes
- Uses
findOneAndDeletewith bothsessionIdanduserIdfor security - Permanently removes the session and all associated messages
- Returns 404 if the session doesn’t exist or doesn’t belong to the user
- Cannot be undone - ensure user confirmation before deletion
POST /api/user/previous-chats
Retrieve a list of all chat sessions for the authenticated user.Authentication
Requires JWT authentication token in the Authorization header.Request Body
No request body required.Response
Array of chat session summaries.
Response Example
Error Responses
Implementation Notes
- Retrieves all sessions for the authenticated user
- Selects only
sessionId,messages, andupdatedAtfields - Returns chats in reverse order (most recent first)
- Uses the first message content as a preview/summary
- Date is formatted using JavaScript’s
toDateString()method - Empty array is returned if user has no chat sessions
Data Model
ChatSession Schema
Chat sessions are stored in MongoDB with the following structure:Indexes
- Composite index on
(userId, sessionId)for efficient lookups - Sessions are user-scoped for security and privacy
Usage Patterns
Building a Chat History UI
-
List View: Use
POST /api/user/previous-chatsto display all conversations- Show the first message as the conversation title
- Display the
updatedAttimestamp - Use
sessionIdas the navigation key
-
Detail View: Use
GET /api/user/chat/:sessionIdto show full conversation- Render all messages in chronological order
- Filter out
systemrole messages if desired (they’re internal context)
-
Deletion: Use
DELETE /api/user/chat/:sessionIdwith user confirmation- Show a confirmation dialog before deletion
- Redirect to chat list after successful deletion
Continuing a Conversation
To continue an existing conversation:- Get the
sessionIdfrom the chat list or URL - Make a new
POST /api/user/ai-chatrequest with thesessionIdin the body - The AI will load the last 20 messages from the session for context
- The response includes the updated conversation history
Session Lifecycle
- Creation: Sessions are created automatically when a user sends their first message without a
sessionId - Updates: Sessions are updated each time a message is sent (user) or received (assistant)
- Persistence: Sessions persist indefinitely until explicitly deleted
- Security: Sessions are always filtered by
userIdto prevent cross-user access
Best Practices
- Pagination: For users with many chats, implement client-side pagination
- Caching: Cache the chat list and individual sessions to reduce API calls
- Optimistic UI: Update the UI immediately when sending messages, then sync with the server response
- Error Handling: Always handle 404 errors gracefully (session may have been deleted)
- Confirmation: Require explicit user confirmation before deleting sessions
Security
- All endpoints require authentication via JWT token
- Sessions are strictly user-scoped (userId is always matched)
- Attempting to access another user’s session returns 404 (not 403) to avoid information leakage
- Session IDs are UUIDs, making them difficult to guess
Performance Considerations
- The chat list endpoint fetches all user sessions - consider pagination for power users
- Individual session retrieval uses MongoDB aggregation for efficiency
- Messages are not paginated within a session - very long sessions may cause performance issues
- Consider implementing a message limit per session (e.g., 1000 messages)
Related Endpoints
- AI Chatbot - Send messages and create new sessions
- Learning Roadmaps - AI-generated learning paths