The Messages API provides two endpoints that cover the full messaging lifecycle: fetching the history of an existing conversation and sending a new message to another user. Every action is scoped to the authenticated user — the server derives the sender’s identity from the JWT cookie, so the client never needs to pass a sender ID explicitly. Messages are persisted to MongoDB and, when the recipient is currently connected, delivered in real time over Socket.io.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/khushboodaryani/Chat-App/llms.txt
Use this file to discover all available pages before exploring further.
Both endpoints are protected by the
protectRoute middleware. You must have a valid jwt cookie (set by signup or login) before calling either route. Requests without a cookie receive a 401 Unauthorized response.GET /api/messages/:id
Retrieves the full conversation history between the authenticated user and another user identified by:id. The server looks up a Conversation document whose participants array contains both user IDs and returns the populated messages array. If no conversation has been started between the two users yet, the endpoint returns an empty array rather than a 404.
Path parameters
The MongoDB ObjectId (24-character hex string) of the other user in the conversation — i.e., the person you want to read messages with. This is not the conversation ID.
Response — 200 OK
Returns an array of Message objects in chronological order. Returns[] when no conversation exists yet — this is not an error condition.
The MongoDB ObjectId of the individual message document.
The MongoDB ObjectId of the user who sent this message.
The MongoDB ObjectId of the user who received this message.
The plain-text content of the message.
ISO 8601 timestamp of when the message was created, added automatically by Mongoose’s
timestamps option.ISO 8601 timestamp of the last update to this document, added automatically by Mongoose’s
timestamps option.200 Response — conversation with one message
200 Response — no conversation yet
Error responses
| HTTP Status | error value | Cause |
|---|---|---|
401 | "Unauthorized - No Token Provided" | JWT cookie is absent |
401 | "Unauthorized - Invalid Token" | JWT signature verification failed |
404 | "User not found" | Decoded JWT refers to a deleted user |
500 | "Internal server error" | Unexpected server-side exception |
POST /api/messages/send/:id
Sends a new message to the user identified by:id. The server resolves the sender from the JWT cookie, locates or creates a Conversation document for the pair, persists the new Message document, and then checks whether the receiver has an active Socket.io connection. If they do, the message is emitted immediately as a newMessage event so the recipient’s UI updates in real time without polling.
Path parameters
The MongoDB ObjectId of the receiver — the user you are sending this message to.
Request body
The plain-text content of the message to send. Must be a non-empty string.
Request body example
Response — 201 Created
Returns the newly created Message document.The MongoDB ObjectId of the newly created message.
The MongoDB ObjectId of the authenticated user who sent the message.
The MongoDB ObjectId of the user the message was sent to (matches
:id in the path).The plain-text content that was sent.
ISO 8601 timestamp of when the message document was created.
ISO 8601 timestamp of the last update to the message document.
201 Response
Error responses
| HTTP Status | error value | Cause |
|---|---|---|
401 | "Unauthorized - No Token Provided" | JWT cookie is absent |
401 | "Unauthorized - Invalid Token" | JWT signature verification failed |
404 | "User not found" | Decoded JWT refers to a deleted user |
500 | "Internal server error" | Unexpected server-side exception |
Socket.io delivery is best-effort. If the receiver is offline at send time, the
newMessage event is simply not emitted — but the message is still saved to MongoDB. The next time the receiver opens or refreshes the conversation, GET /api/messages/:id will return the full history including any messages they missed.