Core concepts
Auto-pagination
Every list endpoint in the Meta Graph API is cursor-paginated. wapi-cloud simplifies pagination with structured PageInfo and effortless for-await async iterators.
The list() method
Calling list() on any module fetches a single page of items and normalizes Graph API's pagination object into a clean PaginatedResult<T>:
pagination.ts
const { data, error } = await whatsapp.templates.list({
limit: 25,
after: "cursor_string_here",
});
if (data) {
console.log("Current page items:", data.items);
console.log("Next cursor:", data.pageInfo.nextCursor);
console.log("Has more pages?", data.pageInfo.hasNext);
}PaginatedResult shape
interface PaginatedResult<T> {
items: T[];
pageInfo: PageInfo;
}
interface PageInfo {
nextCursor?: string;
previousCursor?: string;
hasNext: boolean;
}The listAll() async iterator
Modules that support full listing (such as whatsapp.templates.listAll()) expose an async generator that automatically fetches subsequent pages lazily as you iterate:
list-all.ts
// Iterates over ALL templates across all pages seamlessly
for await (const template of whatsapp.templates.listAll()) {
console.log(template.id, template.name, template.status);
}Because the iterator is lazy, breaking out of the loop immediately halts further network requests:
for await (const template of whatsapp.templates.listAll()) {
if (template.name === "welcome_discount" && template.status === "APPROVED") {
console.log("Found template ID:", template.id);
break; // No further pages are fetched from Meta
}
}Collecting all items into an array
To gather all items across all pages into memory:
const allTemplates = [];
for await (const item of whatsapp.templates.listAll()) {
allTemplates.push(item);
}
console.log("Total templates loaded:", allTemplates.length);If an unexpected error occurs during a
listAll() iteration, the generator cleanly terminates iteration. For critical pipelines that require inspecting mid-pagination errors, use list({ after }) with manual cursor walking.