Pagination with the Vonage Client SDK

When getting a list of objects with the Vonage Client SDK, the response will be paginated. This means that rather than returning all the objects at once, which would increase latency and the size of the response, you will get a chunk of the response or a page. For example, calling getConversations will return a ConversationsPage.

Functions that return a page, will have parameters for a page size, and a cursor. By supplying a cursor you can traverse the pages until you get all the results. Here is an example with getting Conversations.

let conversations, nextCursor, previousCursor
const params = {
    order: "asc", // "desc"
    pageSize: 5,
    cursor: null,
    includeCustomData: true,
    orderBy: null // "CUSTOM_SORT_KEY"     
};

client.getConversations(params)
    .then(conversationsPage => {
        ({conversations, nextCursor, previousCursor} = conversationsPage);
        console.log("Array of Conversations: ", conversations);
        console.log("Cursor for next set of results, if any. Could be null: ", nextCursor);
        console.log("Cursor for previous set of results, if any. Could be null: ", previousCursor);
    }).catch(error => {
        console.error("Error getting Conversations: ", error);
    });

On the ConversationsPage there is a nextCursor and previousCursor parameter. To get the next page of Conversations, call getConversations again, but this time provide the value from the current ConversationsPage.nextCursor for the cursor:

const params = {
    order: "asc", // "desc"
    pageSize: 5,
    cursor: nextCursor, // previousCursor
    includeCustomData: true,
    orderBy: null // "CUSTOM_SORT_KEY"     
};

client.getConversations(params)
    ...

When you reach the end of the results, nextCursor will be nil/null. Inversely, when you reach the start of the results previousCursor will be nil/null.