JAVASCRIPT

Fetching Paginated Data from REST APIs

Learn how to efficiently fetch all pages of data from a paginated REST API endpoint using async/await and recursion in JavaScript, handling common pagination patterns.

async function fetchAllPaginatedData(url, params = {}) {
  let allResults = [];
  let currentPage = 1;
  let hasMorePages = true;

  while (hasMorePages) {
    try {
      const response = await fetch(`${url}?page=${currentPage}&${new URLSearchParams(params).toString()}`);
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`);
      }
      const data = await response.json();

      // Assuming API returns data in 'items' and pagination info like 'next_page_url' or 'last_page'
      // Adjust according to your API's pagination structure
      allResults = allResults.concat(data.items || data.data);

      // Example: API returns 'last_page' or 'next_page_url'
      if (data.last_page && currentPage < data.last_page) {
        currentPage++;
      } else if (data.next_page_url) {
        const nextPageUrl = new URL(data.next_page_url);
        const pageParam = nextPageUrl.searchParams.get('page');
        if (pageParam) {
          currentPage = parseInt(pageParam);
        } else {
          hasMorePages = false; // No explicit page param in next_page_url, assume it's the last.
        }
      }
      else {
        hasMorePages = false;
      }

      // If the API doesn't provide explicit pagination links/info,
      // you might rely on the absence of new data or a specific limit.
      if (!data.items?.length && !data.data?.length) {
          hasMorePages = false;
      }

    } catch (error) {
      console.error("Error fetching paginated data:", error);
      hasMorePages = false; // Stop on error
    }
  }
  return allResults;
}

// Example Usage:
// fetchAllPaginatedData('https://api.example.com/products', { limit: 10 }).then(products => {
//   console.log('All products:', products.length);
// }).catch(err => console.error(err));
How it works: This JavaScript snippet demonstrates how to fetch all data from an API that uses cursor-based or page-number-based pagination. It iteratively calls the API, appending results until no more pages are indicated by the API response (e.g., `next_page_url`, `last_page`, or an empty data array). This ensures all available records are retrieved.

Need help integrating this into your project?

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

Hire DigitalCodeLabs