google-apps-scripthubspothubspot-crm

Unable to retrieve contact address data from Hubspot Contacts API


I'm using Google Apps scripting to access the Hubspot Contacts API and everything works fantastically:

 var firstName = (item.properties.hasOwnProperty('firstname')) ? item.properties.firstname.value : "NA";
 var lastName = (item.properties.hasOwnProperty('lastname')) ? item.properties.lastname.value : "NA";
 var fullName = firstName + " " + lastName; 
 var companyName = (item.properties.hasOwnProperty('company')) ? item.properties.company.value : "NA";

...but I can't seem to query anything related to the contact ADDRESS.

 var streetAddress = (item.properties.hasOwnProperty('address')) ? item.properties.address.value : "NA";
 var cityName = (item.properties.hasOwnProperty('city')) ? item.properties.city.value : "NA";
 var stateName = (item.properties.hasOwnProperty('state')) ? item.properties.state.value : "NA";
 var postalCode = (item.properties.hasOwnProperty('zip')) ? item.properties.zip.value : "NA";

streetAddress, cityName, stateName, postalCode don't seem to return anything from my API queries. But firstName, lastName, companyName all work fine. I had some trouble retrieving email before and found a solution online.

Any ideas why my code for looking up and assigning Hubspot address information isn't working?

Here is my current URL query (both URLs have the same effect, but the top one is newer and I thought if I was more specific with the API, I could get the right properties):

var url_query = API_URL + "/contacts/v1/lists/all/contacts/all?properties=address&properties=firstname&properties=lastname&properties=company&properties=city&properties=state&properties=zip";
//var url_query = API_URL + "/contacts/v1/lists/all/contacts/all";

 response.contacts.forEach(function(item) {
 var vid = item.vid;
  
 var firstName = (item.properties.hasOwnProperty('firstname')) ? item.properties.firstname.value : "NA";
 var lastName = (item.properties.hasOwnProperty('lastname')) ? item.properties.lastname.value : "NA";
 var fullName = firstName + " " + lastName; 
 var companyName = (item.properties.hasOwnProperty('company')) ? item.properties.company.value : "NA";
 var streetAddress = (item.properties.hasOwnProperty('address')) ? item.properties.address.value : "NA";
 var cityName = (item.properties.hasOwnProperty('city')) ? item.properties.city.value : "NA";
 var stateName = (item.properties.hasOwnProperty('state')) ? item.properties.state.value : "NA";
 var postalCode = (item.properties.hasOwnProperty('zip')) ? item.properties.zip.value : "NA";
 Logger.log(fullName,streetAddress,cityName,stateName,postalCode);
 
 var email = "NA";     
  // Not sure why, but a contact might have multiple identity-profiles, we take the firstone
  item['identity-profiles'][0].identities.forEach(function(identity) {
    if (identity.type == "EMAIL") {
      email = identity.value;
    }
  });

Solution

  • This looks like a simple mistake of not including the desired properties in the URL. According to the documentation:

    By default, only a few standard properties will be included in the response data. If you include the 'property' parameter, then you will instead get the specified property in the response. This parameter may be included multiple times to specify multiple properties.

    It would seem you used an invalid "properties" parameter in your URL by mistake. Here's how I would adjust the code:

    var url_query = API_URL + "/contacts/v1/lists/all/contacts/all?property=address&property=firstname&property=lastname&property=company&property=city&property=state&property=zip";
    
    response.contacts.forEach(function(item) {
        var p = item.properties;
    
        var firstName = (p.hasOwnProperty('firstname')) ? p.firstname.value : "NA";
        var lastName = (p.hasOwnProperty('lastname')) ? p.lastname.value : "NA";
        var fullName = firstName + " " + lastName;
        var companyName = (p.hasOwnProperty('company')) ? p.company.value : "NA";
        var streetAddress = (p.hasOwnProperty('address')) ? p.address.value : "NA";
        var cityName = (p.hasOwnProperty('city')) ? p.city.value : "NA";
        var stateName = (p.hasOwnProperty('state')) ? p.state.value : "NA";
        var postalCode = (p.hasOwnProperty('zip')) ? p.zip.value : "NA";
    
        var email = "NA";
        // Not sure why, but a contact might have multiple identity-profiles, we take the firstone
        item['identity-profiles'][0].identities.forEach(function(identity) {
            if (identity.type == "EMAIL") {
                email = identity.value;
            }
        });
    });
    

    Note that this API will be replaced soon with an updated CRM API, so if this is a new project you may not want to put too much work into code using the old API calls!