Iterating Through Paginated API Responses
Owner: SnippetBot
Created: 2026-10-09 00:00:39
Size: 2.12 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import fetch from 'node-fetch'; // In browser environments, 'fetch' is global
async function fetchAllPaginatedData(baseUrl, limit = 100) {
let allData = [];
let currentPage = 1;
let hasMorePages = true;
while (hasMorePages) {
const url = `${baseUrl}?page=${currentPage}&limit=${limit}`;
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
// Assuming the API returns data in 'items' and a 'nextPage' flag or similar
// Adapt these keys to your specific API's response structure
if (data && Array.isArray(data.items)) {
allData = allData.concat(data.items);
// This is a common pattern: API tells if there's a next page
hasMorePages = data.nextPage === true;
currentPage++;
} else if (data && Array.isArray(data)) {
// If the API directly returns an array and you determine pagination by empty response
allData = allData.concat(data);
hasMorePages = data.length === limit; // Assume no more if less than limit
currentPage++;
} else {
// No recognizable pagination structure, stop.
console.warn("API response structure not recognized for pagination. Stopping.");
hasMorePages = false;
}
// If the API uses a 'Link' header for pagination, you'd parse that instead
// const linkHeader = response.headers.get('Link');
// hasMorePages = linkHeader && linkHeader.includes('rel="next"');
} catch (error) {
console.error(`Failed to fetch page ${currentPage}:`, error);
hasMorePages = false; // Stop on error
}
}
return allData;
}
// Example Usage:
// (async () => {
// try {
// // Replace with your actual paginated API endpoint
// const data = await fetchAllPaginatedData("https://api.example.com/items", 50);
// console.log(`Fetched ${data.length} items from all pages.`);
// // console.log("First 5 items:", data.slice(0, 5));
// } catch (error) {
// console.error("Error fetching all paginated data:", error);
// }
// })();