Pagination, Sorting and Filtering

Pagination

When working with collections of resources retrieved from an API call, you can utilize query parameters to filter the results according to your specific criteria. The following query parameters are available for filtering:

  • limit: This parameter allows you to specify the maximum number of records to be returned by the API call. It helps in controlling the size of the response payload by limiting the number of items retrieved.
    Example:
    GET /customer?limit=10
    
    This request would return a maximum of 10 records from the customers collection.

  • offset: The offset parameter indicates the starting point from which the API should begin fetching records. It defines the position within the collection to start retrieving records from.
    Example:
    GET /customer?offset=20
    
    This request would start retrieving records from the 21st position in the customers collection.

Sorting

Sorting allows you to arrange the results of an API call in a specific order based on a chosen attribute. To sort the data, you can use the sort query parameter with the following format:

sort=field_name:sort_method

Where,

  • field_name: Is the name of the field by which you want to sort the data. It specifies the attribute or field based on which the sorting will be performed.
  • sort_method: Indicates the order in which the sorting will be applied. It can be either 'asc' for ascending order or 'desc' for descending order.

Example Usage:

Suppose you want to sort a list of customers based on their email addresses in descending order:

GET /customer?sort=email:desc

This request would return the list of customers sorted by their email addresses in descending order.

By utilizing the sort parameter, you can easily organize the retrieved data according to your preferences, making it easier to analyze and work with the results.

Additional Information:

Not all field names can be sorted. To see a complete documentation of all the sortable fields, go to ...


Filtering


Metadata

Additional information that comes mostly with response data that are collections. It usually comes in a JSON object format with three properties:

PropertyTypeDescription
posnumberThe offset the paginated data was taken from
countnumberThe total record count from the database
deltanumberNumber of records per page

Example metadata:

"pagination": {  
  "pos": number,
  "count": number,
  "delta": number
}

Example request and response with metadata:

Request:

GET /customer

Response:

{
  "customers": [
    {
      "id": 1,
    	"firstname": "Examplefirstname",
    	"lastname": "Examplelastname",
    	"email": "[email protected]",
    	"birthDay": 1,
    	"birthMonth": "JANUARY",
    	"phoneNumber": "12345678",
    	"address": "1 Example Street, Example State",
    	"company": "Example Company Co.",
    	"accruedLoyaltyPoint": 0,
    	"accruedLoyaltyAmount": 0,
    	"createdAt": "2024-01-01T00:00:00.000Z",
    	"updatedAt": "2024-01-01T00:00:00.000Z",
    	"deletedAt": null,
    	"totalTransactionCount": 0,
    	"totalPaidAmount": 0,
    	"totalBalance": 0
    },
    ...4 more customers...
  ],
  "pagination": {
  	"count": 5,
  	"pos": null,
  	"delta": 100
  }
}