GET /api/favorites
Retrieves favorite status for multiple questions or answers in a single request.Authentication
Valid NextAuth session token required
Query Parameters
Type of target to check. Options:
question- Check favorite status for questionsmessage- Check favorite status for answers/messages
Comma-separated list of target IDs to check (max 200 IDs)Example:
q_123,q_456,q_789Response
Object mapping each target ID to its favorite status (boolean)
Example Request
Example Response
If no target IDs are provided, an empty statuses object is returned:
{"statuses": {}}Error Responses
POST /api/favorites
Toggles favorite status for a question or answer. If the item is already favorited, it will be unfavorited, and vice versa.Authentication
Valid NextAuth session token required
Request Body
ID of the question or message to favorite/unfavorite
Type of target. Options:
question- Favorite a questionmessage- Favorite an answer/message
Response
Current favorite status after the operation
Action performed:
favorited or unfavoritedExample Request - Add to Favorites
Example Response - Favorited
Example Response - Unfavorited
Error Responses
Implementation Details
The API automatically handles the toggle behavior:
- If a favorite record exists, it will be deleted (unfavorited)
- If no favorite record exists, one will be created (favorited)
- Duplicate favorite attempts are handled gracefully and return success
For messages (answers), the API automatically looks up and stores the associated
questionId for efficient querying.Database Schema
Favorite records include:userId- ID of the user who favoritedtargetType- Type of target (‘question’ or ‘message’)targetId- ID of the favorited itemquestionId- Associated question ID (for messages, this is looked up; for questions, it’s the same as targetId)createdAt- Timestamp when favoritedupdatedAt- Last update timestamp