Pagination

Some API endpoints will return paginated results. When results are paginated, the server will return 3 headers related to pagination:

  • X-Per-Page: number of results return per page

  • X-Page: Id of the current page

  • X-Total: total number of results available

  • Link: URLs helpers for navigation

The Link header may contain up to 4 different URLs:

<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=1>; rel="first",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=12>; rel="last",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=6>; rel="next",
<https://api.tability.app/workspaces/:workspaceID/search/outcomes?page=4>; rel="prev"
  • rel="first": first page of the results

  • rel="last": last page of the results

  • rel="next": next page of results

  • rel="prev": previous page of results

To iterate on results, you simply need to parse the Link header to find the rel="next" link. If the link doesn't exist, then it means that there are no more pages to display.

Example

Using JS

import parse from 'parse-link-header';

const getNextPage = (response) => {
  if (response && response.headers && response.headers.link) {
    const links = response.headers.link;
    const parsed = parse(links);
    if (parsed && parsed.next) {
      return parsed.next.page;
    }
  }
  return null;
};

Last updated