Skip to main content

GET /api/favorites

Retrieves favorite status for multiple questions or answers in a single request.

Authentication

Authorization
string
required
Valid NextAuth session token required

Query Parameters

targetType
string
required
Type of target to check. Options:
  • question - Check favorite status for questions
  • message - Check favorite status for answers/messages
targetIds
string
required
Comma-separated list of target IDs to check (max 200 IDs)Example: q_123,q_456,q_789

Response

statuses
object
required
Object mapping each target ID to its favorite status (boolean)

Example Request

curl "https://api.example.com/api/favorites?targetType=question&targetIds=q_123,q_456,q_789" \
  -H "Cookie: next-auth.session-token=..."

Example Response

{
  "statuses": {
    "q_123": true,
    "q_456": false,
    "q_789": true
  }
}
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

Authorization
string
required
Valid NextAuth session token required

Request Body

targetId
string
required
ID of the question or message to favorite/unfavorite
targetType
string
required
Type of target. Options:
  • question - Favorite a question
  • message - Favorite an answer/message

Response

favorited
boolean
required
Current favorite status after the operation
action
string
required
Action performed: favorited or unfavorited

Example Request - Add to Favorites

{
  "targetId": "q_123",
  "targetType": "question"
}

Example Response - Favorited

{
  "favorited": true,
  "action": "favorited"
}

Example Response - Unfavorited

{
  "favorited": false,
  "action": "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 favorited
  • targetType - Type of target (‘question’ or ‘message’)
  • targetId - ID of the favorited item
  • questionId - Associated question ID (for messages, this is looked up; for questions, it’s the same as targetId)
  • createdAt - Timestamp when favorited
  • updatedAt - Last update timestamp

Usage Example

# Add to favorites
curl -X POST "https://api.example.com/api/favorites" \
  -H "Content-Type: application/json" \
  -H "Cookie: next-auth.session-token=..." \
  -d '{
    "targetId": "q_123",
    "targetType": "question"
  }'

# Call again to remove from favorites
curl -X POST "https://api.example.com/api/favorites" \
  -H "Content-Type: application/json" \
  -H "Cookie: next-auth.session-token=..." \
  -d '{
    "targetId": "q_123",
    "targetType": "question"
  }'

Build docs developers (and LLMs) love