Skip to main content

List Product Reviews

Retrieve all reviews for a specific product.
GET /api/v1/products/{product_id}/reviews/

Authentication

This endpoint does not require authentication. It is publicly accessible.

Path Parameters

product_id
integer
required
The unique identifier of the product

Response

Returns an array of review objects.
id
integer
Unique review identifier
user
string
Username of the reviewer
review_text
string
The review content/comments
rating
integer
Rating value (1-5):
  • 1: Very Bad
  • 2: Unsatisfied
  • 3: Just There
  • 4: Satisfied
  • 5: Very Satisfied
sentiment
string
Analyzed sentiment of the review (can be null):
  • POSITIVE - Positive sentiment
  • Neutral - Neutral sentiment
  • Negative - Negative sentiment
sentiment_score
float
Numerical sentiment score between -1 and 1 (can be null)
  • Positive values indicate positive sentiment
  • Negative values indicate negative sentiment
is_active
boolean
Whether the review is active and visible
created
string
ISO 8601 timestamp of review creation
updated
string
ISO 8601 timestamp of last update
reviewimage_set
array
Array of images attached to the review

Request Example

curl -X GET "https://api.example.com/api/v1/products/1/reviews/" \
  -H "Accept: application/json"

Response Example

200 OK
[
  {
    "id": 15,
    "user": "john_doe",
    "review_text": "Excellent sound quality and comfortable for long listening sessions. The noise cancellation is impressive!",
    "rating": 5,
    "sentiment": "POSITIVE",
    "sentiment_score": 0.92,
    "is_active": true,
    "created": "2024-02-20T15:45:00Z",
    "updated": "2024-02-20T15:45:00Z",
    "reviewimage_set": [
      {
        "id": 1,
        "image": "https://cdn.example.com/reviews/headphones-review-1.jpg",
        "is_primary": true
      }
    ]
  },
  {
    "id": 16,
    "user": "jane_smith",
    "review_text": "Good product overall, but the battery life could be better. Works great for daily use.",
    "rating": 4,
    "sentiment": "POSITIVE",
    "sentiment_score": 0.65,
    "is_active": true,
    "created": "2024-02-18T10:30:00Z",
    "updated": "2024-02-18T10:30:00Z",
    "reviewimage_set": []
  }
]

Error Responses

404
Not Found
Product with the specified ID does not exist
{
  "detail": "Not found."
}

Create Product Review

Add a new review for a product. Only authenticated customers who have purchased the product can post reviews.
POST /api/v1/products/{product_id}/reviews/

Authentication

Required: User must be authenticated with a valid JWT token.
Authorization: Bearer <your_jwt_token>

Path Parameters

product_id
integer
required
The unique identifier of the product

Request Body

review_text
string
required
The review content/comments
rating
integer
required
Rating value (1-5):
  • 1: Very Bad
  • 2: Unsatisfied
  • 3: Just There
  • 4: Satisfied
  • 5: Very Satisfied

Request Example

curl -X POST "https://api.example.com/api/v1/products/1/reviews/" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "review_text": "Amazing product! Exceeded my expectations.",
    "rating": 5
  }'

Response Example

201 Created
{
  "success": "Review posted"
}

Error Responses

400
Bad Request
Invalid request data or user hasn’t purchased the product
{
  "error": "You can't add a review for a product you didn't purchase."
}
Or with validation errors:
{
  "rating": ["A valid integer is required."],
  "review_text": ["This field may not be blank."]
}
401
Unauthorized
Authentication credentials were not provided or are invalid
{
  "detail": "Authentication credentials were not provided."
}
404
Not Found
Product with the specified ID does not exist
{
  "detail": "Not found."
}

Get Specific Review

Retrieve a single review by its ID.
GET /api/v1/products/{product_id}/reviews/{review_id}/

Authentication

This endpoint does not require authentication. It is publicly accessible.

Path Parameters

product_id
integer
required
The unique identifier of the product
review_id
integer
required
The unique identifier of the review

Request Example

curl -X GET "https://api.example.com/api/v1/products/1/reviews/15/" \
  -H "Accept: application/json"

Response Example

200 OK
{
  "id": 15,
  "user": "john_doe",
  "review_text": "Excellent sound quality and comfortable for long listening sessions.",
  "rating": 5,
  "sentiment": "POSITIVE",
  "sentiment_score": 0.92,
  "is_active": true,
  "created": "2024-02-20T15:45:00Z",
  "updated": "2024-02-20T15:45:00Z",
  "reviewimage_set": []
}

Error Responses

404
Not Found
Product or review with the specified ID does not exist
{
  "error": "Review doesn't exist"
}

Update Review

Update an existing review. Only the review author can update their own review.
PUT /api/v1/products/{product_id}/reviews/{review_id}/

Authentication

Required: User must be authenticated and must be the author of the review.
Authorization: Bearer <your_jwt_token>

Path Parameters

product_id
integer
required
The unique identifier of the product
review_id
integer
required
The unique identifier of the review

Request Body

review_text
string
required
The updated review content/comments
rating
integer
required
Updated rating value (1-5)

Request Example

curl -X PUT "https://api.example.com/api/v1/products/1/reviews/15/" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "review_text": "Updated: Still an amazing product after 3 months of use!",
    "rating": 5
  }'

Response Example

200 OK
{
  "id": 15,
  "user": "john_doe",
  "review_text": "Updated: Still an amazing product after 3 months of use!",
  "rating": 5,
  "sentiment": "POSITIVE",
  "sentiment_score": 0.92,
  "is_active": true,
  "created": "2024-02-20T15:45:00Z",
  "updated": "2024-03-03T09:30:00Z",
  "reviewimage_set": []
}

Error Responses

400
Bad Request
Invalid request data
{
  "rating": ["Ensure this value is less than or equal to 5."]
}
401
Unauthorized
Authentication credentials were not provided
{
  "detail": "Authentication credentials were not provided."
}
403
Forbidden
User is not the author of the review
{
  "detail": "You do not have permission to perform this action."
}
404
Not Found
Product or review with the specified ID does not exist
{
  "error": "Review doesn't exist"
}

Delete Review

Delete a review. Only the review author can delete their own review.
DELETE /api/v1/products/{product_id}/reviews/{review_id}/

Authentication

Required: User must be authenticated and must be the author of the review.
Authorization: Bearer <your_jwt_token>

Path Parameters

product_id
integer
required
The unique identifier of the product
review_id
integer
required
The unique identifier of the review

Request Example

curl -X DELETE "https://api.example.com/api/v1/products/1/reviews/15/" \
  -H "Authorization: Bearer YOUR_JWT_TOKEN"

Response Example

204 No Content
(Empty response body)

Error Responses

401
Unauthorized
Authentication credentials were not provided
{
  "detail": "Authentication credentials were not provided."
}
403
Forbidden
User is not the author of the review
{
  "detail": "You do not have permission to perform this action."
}
404
Not Found
Product or review with the specified ID does not exist
{
  "error": "Review doesn't exist"
}

Review Rules and Validation

Rating Scale

Reviews use a 5-point rating scale:
  1. Very Bad (1) - Extremely dissatisfied
  2. Unsatisfied (2) - Below expectations
  3. Just There (3) - Meets basic expectations
  4. Satisfied (4) - Meets or slightly exceeds expectations
  5. Very Satisfied (5) - Exceeds expectations significantly

Review Requirements

  1. Purchase Verification: Users can only review products they have purchased
  2. Authentication: Creating, updating, and deleting reviews requires authentication
  3. Ownership: Users can only update or delete their own reviews
  4. One Review Per User: Each user can only post one review per product (enforced at application level)

Automatic Features

  1. Product Rating Update: Product’s average rating is automatically recalculated when reviews are added, updated, or deleted
  2. Sentiment Analysis: Reviews may undergo automatic sentiment analysis to populate sentiment and sentiment_score fields
  3. Timestamp Tracking: Creation and update timestamps are automatically maintained

Content Guidelines

  • Review Text: Should be descriptive and provide useful information to other customers
  • Minimum Length: While not enforced at API level, reviews should be substantive
  • Prohibited Content: Reviews containing inappropriate content may be marked as inactive by administrators

Notes

  • Reviews are sorted by creation date (most recent first) by default
  • The is_active flag allows administrators to hide inappropriate reviews without deletion
  • Sentiment analysis is performed automatically but may be null for older reviews
  • Product ratings are calculated as the average of all active review ratings
  • Review images are optional and managed separately from the review text
  • Deleting a review permanently removes it from the database
  • The product’s rating field is updated asynchronously after review operations

Build docs developers (and LLMs) love