Last active
May 31, 2019 15:38
-
-
Save cbarley10/b2800eba6e7811a5cf804a4eb4e6335e to your computer and use it in GitHub Desktop.
HubSpot Recursion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("dotenv").config() | |
const axios = require("axios"); | |
const API_KEY = process.env.API_KEY; | |
const count = 100; | |
const mainUrl = `https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=${API_KEY}&count=${count}&property=email`; | |
const createCsvWriter = require("csv-writer").createArrayCsvWriter; | |
const csvWriter = createCsvWriter({ | |
path: "recursion.csv", | |
header: ["EMAIL", "VID"] | |
}); | |
const getObjects = (url, arr) => { | |
return axios.get(url).then(({ data }) => { | |
if (data["has-more"]) { | |
const newOffset = data["vid-offset"]; | |
const newUrl = `${mainUrl}&vidOffset=${newOffset}`; | |
const newArr = arr.concat(data.contacts); | |
return getObjects(newUrl, newArr); | |
} | |
arr = arr.concat(data.contacts); | |
return arr; | |
}); | |
}; | |
getObjects(mainUrl, []).then(res => { | |
const allData = res.map(item => { | |
const { properties, vid } = item; | |
const newArr = [properties.email.value, vid]; | |
return newArr; | |
}); | |
csvWriter.writeRecords(allData).then(() => { | |
console.log("...Done"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment