Skip to main content

POST /api/likes

Toggles upvote or downvote status for a question or answer. The endpoint uses atomic operations to ensure vote consistency.

Authentication

Authorization
string
required
Valid NextAuth session token required

Request Body

targetId
string
required
ID of the question or message to vote on
targetType
string
required
Type of target. Options:
  • question - Vote on a question
  • message - Vote on an answer/message
voteType
string
default:"up"
Type of vote. Options:
  • up - Upvote (like)
  • down - Downvote

Response

action
string
required
Action performed. Possible values:
  • liked - Item was upvoted
  • unliked - Upvote was removed
  • downvoted - Item was downvoted
  • undownvoted - Downvote was removed
upvotes
integer
required
Total number of upvotes after the operation
downvotes
integer
required
Total number of downvotes after the operation
liked
boolean
required
Whether the current user has liked this item
downvoted
boolean
required
Whether the current user has downvoted this item

Vote Behavior

The API implements intelligent vote toggling:

Example Request - Upvote

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

Example Response - Upvoted

{
  "action": "liked",
  "upvotes": 16,
  "downvotes": 2,
  "liked": true,
  "downvoted": false
}

Example Request - Downvote

{
  "targetId": "msg_456",
  "targetType": "message",
  "voteType": "down"
}

Example Response - Downvoted

{
  "action": "downvoted",
  "upvotes": 8,
  "downvotes": 3,
  "liked": false,
  "downvoted": true
}

Example Response - Switched Vote

{
  "action": "liked",
  "upvotes": 9,
  "downvotes": 2,
  "liked": true,
  "downvoted": false
}
When switching from downvote to upvote (or vice versa), both counters are updated atomically in a single database operation.

Error Responses

Atomic Operations

All vote operations are performed atomically using MongoDB’s updateOne with conditional queries. This ensures:
  • No race conditions between concurrent votes
  • Vote counts remain consistent
  • User can only have one type of vote (upvote OR downvote) at a time

Usage Example

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

# Downvote an answer
curl -X POST "https://api.example.com/api/likes" \
  -H "Content-Type: application/json" \
  -H "Cookie: next-auth.session-token=..." \
  -d '{
    "targetId": "msg_456",
    "targetType": "message",
    "voteType": "down"
  }'

# Remove upvote (call again with same parameters)
curl -X POST "https://api.example.com/api/likes" \
  -H "Content-Type: application/json" \
  -H "Cookie: next-auth.session-token=..." \
  -d '{
    "targetId": "q_123",
    "targetType": "question",
    "voteType": "up"
  }'

Database Storage

Vote data is stored directly on question and message documents:
  • upvotes (integer) - Total upvote count
  • downvotes (integer) - Total downvote count
  • likedBy (array of strings) - User IDs who upvoted
  • dislikedBy (array of strings) - User IDs who downvoted

Build docs developers (and LLMs) love