Skip to content

Pagination

Canteen's Admin API supports page-based pagination for endpoints that return lists of data. This ensures efficient data retrieval and prevents performance issues when handling large datasets.

Requesting Paginated Data

To paginate results, use the following query parameters:

ParameterTypeDescription
pageintegerThe page number to retrieve (default: 1).
per_pageintegerThe number of items per page (default: 25, max: 100).

Example Request:

bash
curl -X GET "https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=2&per_page=25" \
  -H "Authorization: Bearer your-access-token"

Pagination Response Structure

Paginated responses include metadata to help navigate through the dataset:

json
{
  "success": true,
  "message": "Orders retrieved successfully.",
  "data": [
    {
      "id": 1,
      "amount": 500,
      "status": "completed",
      "created_at": "2024-02-12T10:00:00Z"
    }
  ],
  "meta": {
    "current_page": 2,
    "last_page": 5,
    "per_page": 25,
    "total": 120
  },
  "links": {
    "first": "https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=1",
    "last": "https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=5",
    "prev": "https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=1",
    "next": "https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=3"
  }
}

Pagination Metadata

FieldTypeDescription
current_pageintegerThe current page number.
last_pageintegerThe total number of pages available.
per_pageintegerThe number of items returned per page.
totalintegerThe total number of records available.
FieldDescription
firstURL of the first page.
lastURL of the last page.
prevURL of the previous page (null if on the first page).
nextURL of the next page (null if on the last page).

Handling Pagination in Code

To navigate through paginated results, use the links section in the response.

Example in JavaScript:

js
async function fetchorders(page = 1) {
  const response = await fetch(`https://your-org-uuid.canteenweb.com/api/v1/admin/orders?page=${page}`, {
    headers: { 'Authorization': 'Bearer your-access-token' }
  });

  const data = await response.json();

  console.log('orders:', data.data);

  if (data.links.next) {
    console.log('Next Page URL:', data.links.next);
  }
}

Best Practices

  • Always check the last_page value to prevent unnecessary requests.
  • Use per_page wisely to balance performance and data retrieval needs.
  • Handle the links.next value dynamically to fetch additional pages as needed.

This is a private API. All rights reserved.