API Reference

Cursor Pagination

Learn how we use cursor pagination

Some endpoints use cursor based pagination. This means you will use the cursor from the API response in order to fetch the next "page" of data.

If you are looking to get all of the responses for a particular survey you will want to utilize this pattern.

See below for an example:


Get all responses

const axios = require('axios');

const headers = {
  'Authorization': 'YOUR_API_KEY',
  'Content-Type': 'application/json',
};

const accountId = 'YOUR_ACCOUNT_ID';
const endpoint = `https://v1.zigpoll.com/responses`;

function fetchAllResponses() {
  function fetchPage(responses = [], startCursor = '') {
    const url = `${endpoint}?accountId=${accountId}&limit=100&startCursor=${startCursor}`;
    return axios.get(url, { headers })
      .then((response) => {
        const { data, hasNextPage, endCursor } = response.data;
        responses = [ ...data, ...responses ];
        if (hasNextPage) {
          return fetchPage(responses, endCursor);
        }
        return responses;
      })
      .catch((err) => {
        console.log(err)
      })
  }
  
  return fetchPage();
}

fetchAllResponses().then((responses) => {
  // Do something with all the response objects...
});

And here's another example to get all documents up until a certain timestamp:

Get all responses up until a certain timestamp

And here's another example to get all documents up until a certain timstamp:

const axios = require('axios');

const headers = {
  'Authorization': 'YOUR_API_KEY',
  'Content-Type': 'application/json',
};

const accountId = 'YOUR_ACCOUNT_ID';
const endpoint = `https://v1.zigpoll.com/responses`;

function getStartOfLastWeekTimestamp() {
  const now = new Date();
  const startOfThisWeek = new Date(now);
  startOfThisWeek.setDate(now.getDate() - now.getDay()); // Set to Sunday of current week
  const startOfLastWeek = new Date(startOfThisWeek);
  startOfLastWeek.setDate(startOfThisWeek.getDate() - 7) // Set to Sunday of last week
  startOfLastWeek.setHours(0, 0, 0, 0); // Set to midnight
  return startOfLastWeek.getTime();
}

const lastWeekTimestamp = getStartOfLastWeekTimestamp();

function fetchAllResponses() {
  function fetchPage(responses = [], startCursor = '') {
    const url = `${endpoint}?accountId=${accountId}&limit=100&startCursor=${startCursor}`;
    return axios.get(url, { headers })
      .then((response) => {
        const { data, hasNextPage, endCursor } = response.data;
        let getNextPage = hasNextPage;

        const filteredData = [];
        data.forEach((response) => {
          const timestamp = new Date(response.createdAt).getTime();
          if (lastWeekTimestamp > timestamp) {
            getNextPage = false;
          } else {
            filteredData.push(response);
          }
        });

        responses = [ ...filteredData, ...responses ];

        if (getNextPage) {
          return fetchPage(responses, endCursor);
        }
        return responses;
      })
      .catch((err) => {
        console.log(err)
      })
  }
  
  return fetchPage();
}

fetchAllResponses().then((responses) => {
  // Do something with all the response objects...
});