JAVASCRIPT

Fetching All Pages from a Paginated API in JavaScript

Learn how to programmatically fetch all available data from a paginated API endpoint using JavaScript's Fetch API, iterating through pages until complete.

async function fetchAllPaginatedData(baseUrl, pageSize = 10, initialPage = 1) {
  let allData = [];
  let currentPage = initialPage;
  let hasMore = true;

  while (hasMore) {
    try {
      const url = `${baseUrl}?page=${currentPage}&pageSize=${pageSize}`;
      const response = await fetch(url);

      if (!response.ok) {
        throw new Error(`HTTP error! Status: ${response.status}`);
      }

      const pageData = await response.json();

      // Assuming the API returns data in 'items' array and 'totalPages' or 'nextPage' info
      allData = allData.concat(pageData.items || pageData.data);

      // Logic to determine if there are more pages
      // This often depends on API specific response structure (e.g., 'next' link, 'total_pages', 'has_more')
      hasMore = pageData.nextPage !== null && pageData.nextPage > currentPage;
      // Or if API provides total pages:
      // hasMore = currentPage < pageData.totalPages;

      if (hasMore) {
        currentPage++;
      } else {
        hasMore = false;
      }

    } catch (error) {
      console.error('Error fetching paginated data:', error.message);
      hasMore = false; // Stop on error
    }
  }
  console.log('All paginated data fetched successfully.');
  return allData;
}

// Example Usage:
// const paginatedApiUrl = 'https://api.example.com/products';
// fetchAllPaginatedData(paginatedApiUrl, 20)
//   .then(data => console.log('Combined Data:', data.length, 'items'))
//   .catch(err => console.error('Failed to fetch all data:', err));
How it works: This snippet provides a robust solution for fetching all available data from an API that uses pagination. It iteratively makes `fetch` requests, incrementing the page number until no more pages are indicated by the API response. The snippet assumes a common API pattern where `page` and `pageSize` query parameters control pagination and the response contains an array of `items` or `data`, along with information like `nextPage` or `totalPages` to determine if more data exists.

Need help integrating this into your project?

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

Hire DigitalCodeLabs