Skip to main content

Create an Intercom Access Token

Follow the instructions here to create an Intercom access token.

Add the key to a secret group

Copy the access token from the prior step. Then create a new secret group and paste the key into a new secret named intercom_api_key.

Example code

Once you’ve completed the prior steps, create a new workbook, add a new import script to a sheet and copy + paste the script below. Once pasted select your secret group in the toolbar (make sure the key in the secret group is called intercom_api_key). If you’d like to fetch users instead of leads, change role to user.
const equals = require("equals");
const axios = require("axios");
const luxon = require("luxon");

const apiKey = equals.getSecret("intercom_api_key");
const role = "lead";

const unixToDateTime = (unixTimestamp) =>
  luxon.DateTime.fromSeconds(unixTimestamp).toFormat('yyyy-LL-dd HH:mm:ss');

const getLeadsPage = async (starting_after = undefined) => {
  const response = await axios({
    method: "post",
    url: "https://api.intercom.io/contacts/search",
    data: {
      query: {
        field: "role",
        operator: "=",
        value: role
      },
      pagination: { per_page: 150, starting_after },
      sort: { field: "created_at", order: "descending" }, 
    },
    headers: {
      Authorization: `Bearer ${apiKey}`,
      Accept: "application/json"
    }
  });
  return response.data;
}

const getAllLeads = async () => {
  let leads = [];
  let response;
  let startingAfter;
  do {
    response = await getLeadsPage(startingAfter);
    leads = leads.concat(response.data);
    startingAfter = response.pages.next && response.pages.next.starting_after;
  } while(startingAfter);
  return leads;
}

const leads = await getAllLeads();

equals.addHeaders(["email", "created_at"])
leads.forEach((lead) => {
  lead["created_at"] = unixToDateTime(lead["created_at"]);
  equals.addRow(lead);
})

Google Analytics Typeform