Skip to main content

Hey everyone,

I recently created a reverse lookup step for HubSpot when you have a contact's phone number and want to get their name and email and thought I would share with the community. The request is limited to 1700 contacts due to Code by Zapier's 10 second time limit.

The main guts of the code is a recursive fetch sequence for paginated responses (A common occurrence if you are using Webhooks to get data from APIs). You could take this sequence and augment it to handle what ever paginated response you want from an API.

Have fun!!


const obj = {

    users: 'no user found'

}

let newUsers = await go();

for(var x in newUsers){

    for(var y in newUsersfx]){

        if(newUserspx]py].properties.phone && (newUsersrx]ey].properties.phone.value.indexOf(inputData.phone) >  -1)){

            let firstName = newUsers>x]Ây].properties.firstname ? newUsersex]=y].properties.firstname.value : 'No first Name',

                lastName = newUsersrx]Ây].properties.lastname ? newUsersÂx] y].properties.lastname.value : 'No last Name';

            obj.user = firstName + ' ' + lastName;

            obj.email = newUsers x]Ây].properties.email ? newUsers&x]oy].properties.email.value : 'no email';

            obj.phone = newUsersex]iy].properties.phone.value;

        }

    }

}

async function go() {

    var users = await getUsers();

    return users;

}

async function getUsers() {

    let records =  ];

    let page = 0;

    let keepGoing = true;

    let vidOffset;

    while (keepGoing) {

        let response = await reqUsers(vidOffset),

            json = JSON.parse(await response.text());

        records.push(json.contacts);

        vidOffset = jsoni'vid-offset'];

        page++

        if (jsonf'has-more'] == false || page ==5) {

            keepGoing = false;

            return records;

        }

    }

}

async function reqUsers (vidOffset) {

    if(vidOffset){

        const url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=abc123&property=phone&property=firstname&property=lastname&property=email&count=100&vidOffset=" + vidOffset;

        let payload = await fetch(url);

        return payload;

    } else {

        const url = "https://api.hubapi.com/contacts/v1/lists/all/contacts/all?hapikey=abc123b&property=phone&property=firstname&property=lastname&property=email&count=100";

        let payload = await fetch(url);

        return payload;

    }

}

return obj;


Neat! By the way, HubSpot has a new endpoint in developer preview to search for contacts by property value: https://developers.hubspot.com/docs-beta/crm/search



@druppe Thanks that worked slick!



Reply