JAVASCRIPT

Consuming Paginated APIs with Link Headers (or Body)

Master fetching large datasets from paginated APIs using JavaScript. Navigate `next` and `previous` links (from response body or headers) for efficient and complete data retrieval.

async function fetchPaginatedData(initialUrl) {
  let currentPageUrl = initialUrl;
  const allData = [];

  while (currentPageUrl) {
    try {
      const response = await fetch(currentPageUrl);

      if (!response.ok) {
        const errorData = await response.json().catch(() => ({ message: response.statusText }));
        throw new Error(`HTTP error! Status: ${response.status}, Details: ${JSON.stringify(errorData)}`);
      }

      const pageData = await response.json();
      allData.push(...pageData.results); // Assuming 'results' array and 'next' property

      // --- API-specific pagination parsing ---
      // Option 1: Links in response body (common in many APIs like GitHub, Stripe)
      currentPageUrl = pageData.next || null;

      // Option 2: Links in 'Link' header (common in REST APIs)
      // const linkHeader = response.headers.get('Link');
      // if (linkHeader) {
      //   const links = parseLinkHeader(linkHeader); // Helper function needed
      //   currentPageUrl = links.next ? links.next.url : null;
      // } else {
      //   currentPageUrl = null;
      // }

    } catch (error) {
      console.error('Error fetching paginated data:', error);
      throw error;
    }
  }
  return allData;
}

// Helper to parse Link header (if using Option 2 above)
// function parseLinkHeader(header) {
//   const links = {};
//   header.replace(/<([^>]+)>;\s*rel="([^\"]+)"/g, (match, url, rel) => {
//     links[rel] = { url: url };
//     return match;
//   });
//   return links;
// }


// Example Usage:
// fetchPaginatedData('https://api.example.com/items?page=1')
//   .then(data => console.log('All paginated data:', data))
//   .catch(error => console.error('Failed to fetch all data:', error));
How it works: This snippet shows how to consume data from an API that provides paginated results. It iteratively fetches data pages until no `next` link is provided, accumulating all results. The example assumes the API provides `next` and `previous` links directly within the JSON response body (e.g., `pageData.next`), but also includes commented-out logic for parsing pagination links from the HTTP `Link` header, a common pattern in RESTful APIs. This pattern is essential for retrieving large datasets efficiently without overloading the server or client.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs