'REST API multiple endpoints design

I am currently in the process of building a .NET Web API which is gonna be consumed in another project for a dashboard. Currently, first endpoint return total number of parcels and an array of parcels sent for a given date range.

An example of this API endpoint response looks something like this

{
    "totalCount": 3,
    "data": [
        {
            "parcelId": 1,
            "parcelSentDate": "2022-04-25",
            "parcelCount": 1
        },
        {
            "parcelId": 2,
            "parcelSentDate": "2022-04-26",
            "parcelCount": 1
        },
        {
            "parcelId": 3,
            "parcelSentDate": "2022-04-27",
            "parcelCount": 1
        }
    ],
    "success": true
}

Second endpoint returns a total number of parcels on whether they have been delivered either early, late or on time and has an array of parcels which contains the delivered date and the estimated delivery date.

An example of this API endpoint response looks like the below

{
    "early": 1,
    "late": 2,
    "onTime": 0,
    "data": [
        {
            "parcelId": 1,
            "deliveredDate": "2022-05-04",
            "estimatedDeliveryDate": "2022-05-02",
        },
        {
            "parcelId": 2,
            "deliveredDate": "2022-05-01",
            "estimatedDeliveryDate": "2022-04-30",
        },
        {
            "parcelId": 3,
            "deliveredDate": "2021-05-26",
            "estimatedDeliveryDate": "2022-05-09",
        }
    ],
    "success": true
}

The total number of parcels sent can be calculated using the second endpoint from the parcels array. What I am wondering about is, is it still okay to keep the first endpoint that returns the total number of parcels sent and keep things separated? or is it better to keep only the second endpoint and derive the total parcels count from it since it will contain all the parcels information?

Does it all come down to the situation and there isn't a right or wrong answer here?



Solution 1:[1]

You can keep only one endpoint but separate with any specific parameters like early, late or on time that depends on your business requirements. If these parameters passed then return the second endpoint functionality or just return the first endpoint functionality. If these parameters are not required put just one parameter like with_specifications = true and then return second endpoint functionality otherwise if it false return the first endpoint functionality.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1