Skip to content

Instantly share code, notes, and snippets.

@nikmartin
Last active November 23, 2022 02:10
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nikmartin/ba5e4efd5d96309fe2c5 to your computer and use it in GitHub Desktop.
Save nikmartin/ba5e4efd5d96309fe2c5 to your computer and use it in GitHub Desktop.
A short example of how to use the hubspot api in node.js to get a full CSV export of your contacts
/**
hubspot API FULL CSV Export
A short example of how to use the hubspot api in node.js to get a full CSV export of your Hubspot contacts
The MIT License (MIT)
Copyright (c) 2015 Nik Martin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var request = require('request');
var fs = require('fs');
var baseUrl = 'https://api.hubapi.com';
var hapikey = '?hapikey=YOUR HAPI KEY';
var contactsPath = '/contacts/v1/lists/all/contacts/all';
//The list of contact properties to get - you should edit these to match what you want in your CSV
var contactProps = ['associatedcompanyid', 'hubspot_owner_id', 'firstname', 'lastname', 'company',
'email', 'phone', 'phone_2', 'mobilephone', 'jobtitle', 'address', 'city', 'state', 'postal_code',
'hs_lead_status', 'lifecyclestage', 'website', 'message', 'contact_tag', 'lead_rating', 'time_zone'
];
var contactPropsURL = "";
contactProps.forEach(function(prop) {
contactPropsURL += '&property=' + prop;
});
var contactsURL = baseUrl + contactsPath + hapikey + '&formSubmissionMode=none&count=100' + contactPropsURL;
var contactList = [];
function getContactsList(url, callback) {
request(url, function(error, response, body) {
if (!error && response.statusCode == 200) {
var list = JSON.parse(body);
contactList.push.apply(contactList, list.contacts)
if (list["has-more"]) {
console.log(list["vid-offset"]);
setTimeout(function() {
getContactsList(contactsURL + '&vidOffset=' + list["vid-offset"], callback);
}, 100); //hubspot has a rate limit of 10 requests/sec
} else {
return callback(contactList);
}
}
});
}
var date = new Date();
var contactsFile = '/contacts-' + (date.getMonth() + 1) + '-' + date.getDate() + '.csv';
console.log('Writing to CSV contact file: %s', contactsFile);
getContactsList(contactsURL, function(contacts) {
console.log('Done, got: %s', contacts.length);
var header = 'contactid,portalid,crmlink,' + contactProps.join(',');
fs.appendFileSync(__dirname + contactsFile, header + '\n');
contacts.forEach(function(contact) {
console.log('contactid: %s:', contact.vid);
var contactRow =
contact.vid + "," +
contact["portal-id"] + ',' +
'"https://app.hubspot.com/sales/' + contact["portal-id"] + '/contact/' + contact.vid + '/",';
contactProps.forEach(function(prop) {
contactRow += contact.properties.hasOwnProperty(prop) ? '"' + contact.properties[prop].value
.replace('"', '""') + '",' : '"",';
});
fs.appendFileSync(__dirname + contactsFile, contactRow + '\n');
});
});
@kyletorti
Copy link

There's a problem though. If a field doesn't have a value then the row is shifted over thus messing up all the columns. Can that be fixed so that blank values will provide a blank cell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment