Scripts
Retrieve trending scripts

GET /api/trending

This endpoint retrieves the top 12 trending scripts based on user engagements within the last 48 hours, using a special algorithm that leverages our analytics backend.

Description

The /api/trending endpoint is a unique algorithm-driven route that analyzes user engagement data from our analytics backend to identify the most popular and trending scripts over the past 48 hours. This endpoint employs an advanced MongoDB aggregation pipeline to filter, sort, and project the necessary data for generating the trending scripts list.

The algorithm takes into account various factors, such as the number of views, user interactions, and script popularity, to determine the top 12 trending scripts.

Response

The response is a JSON object with the following structure:

{
 "success": [
   {
     "_id": "script_id",
     "views": 1234,
     "script": {
       "title": "Trending Script",
       "description": "This is a trending script.",
       // ... (other script fields)
     },
     "user": {
       "username": "UserName",
       "verified": true
       // ... (other user fields)
     }
   },
   // ... (up to 12 trending scripts)
 ]
}
⚠️

Please read about possible API Response Edge cases and handle them accordingly.

Reponse fields explained

The response includes an array of trending scripts, each containing the following fields:

FieldDescription
_idThe unique identifier of the script.
viewsThe number of unique views the script has received within the last 48 hours.
scriptAn object containing the script details.
userAn object containing the user information associated with the script.

Error Handling

If an error occurs during the retrieval of trending scripts, the endpoint will respond with a JSON object containing the error message:

{
  "error": "Error message"
}

The error response will have a status code of 500 (Internal Server Error).

Example Usage

  .then(response => response.json())
  .then(data => {
    if (data.success) {
      console.log('Trending Scripts:', data.success);
    } else {
      console.error('Error:', data.error);
    }
  })
  .catch(error => {
    console.error('Error fetching trending scripts:', error);
  });

This example demonstrates how to fetch the trending scripts using the /api/trending endpoint. The response data contains an array of trending scripts under the success key, or an error message under the error key in case of an error.