JAVASCRIPT
JavaScript Function to Fetch All Pages from a Paginated API
Learn how to iteratively fetch all available data from a REST API that uses cursor-based or offset-based pagination by repeatedly making requests until no more pages are found.
async function fetchAllPaginatedData(baseUrl, pageSize = 10, initialPage = 1) {
let allItems = [];
let currentPage = initialPage;
let hasMore = true;
while (hasMore) {
try {
// Adjust URL parameters based on your API's pagination scheme
// Common schemes: 'page' and 'per_page', 'offset' and 'limit', 'cursor'
const url = `${baseUrl}?page=${currentPage}&per_page=${pageSize}`;
console.log(`Fetching page ${currentPage} from ${url}`);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const pageData = await response.json();
// Assuming API returns an array of items directly,
// or an object like { data: [...], meta: { next_page: ... } }
let itemsOnPage = [];
if (Array.isArray(pageData)) {
itemsOnPage = pageData;
} else if (pageData && Array.isArray(pageData.data)) {
itemsOnPage = pageData.data;
} else {
// Handle other API response structures
console.warn("Unexpected pagination response structure:", pageData);
break; // Stop if structure is not as expected
}
if (itemsOnPage.length > 0) {
allItems = allItems.concat(itemsOnPage);
currentPage++;
} else {
hasMore = false; // No more items on this page
}
// You might need to check specific API response fields for 'hasMore'
// e.g., if (pageData.meta && pageData.meta.next_page === null) hasMore = false;
// or if (pageData.links && !pageData.links.next) hasMore = false;
// For simplicity, we assume an empty page means no more data.
if (itemsOnPage.length < pageSize && pageData.length !== undefined) { // Check for last page if API doesn't indicate
hasMore = false;
}
} catch (error)
{
console.error("Error fetching paginated data:", error);
hasMore = false; // Stop on error
throw error;
}
}
return allItems;
}
// Example Usage (replace with your actual paginated API endpoint)
// async function main() {
// try {
// // This example uses JSONPlaceholder, which does not truly paginate this way,
// // but you can simulate it by adjusting page sizes.
// // For a real paginated API, uncomment and test.
// const allPosts = await fetchAllPaginatedData('https://jsonplaceholder.typicode.com/posts', 10);
// // const allPosts = await fetchAllPaginatedData('https://api.example.com/items', 20);
// console.log('Total items fetched:', allPosts.length);
// // console.log('First 3 items:', allPosts.slice(0, 3));
// } catch (error) {
// console.error('Failed to retrieve all paginated data:', error);
// }
// }
// main();
How it works: This JavaScript function demonstrates how to systematically retrieve all data from a REST API that employs pagination. It uses an `async/await` loop to make successive `fetch` calls, incrementing the page number or cursor parameter with each request. The loop continues until an API response indicates no more items are available, accumulating all data into a single array. This robust snippet includes flexible handling for different API response structures and comprehensive error management, making it essential for integrating with large datasets.