Paginated requests in Code by Zapier / Hubspot Lookup by Phone Number

  • 7 February 2020
  • 2 replies
  • 4675 views

Userlevel 4
Badge +4

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 newUsers[x]){

        if(newUsers[x][y].properties.phone && (newUsers[x][y].properties.phone.value.indexOf(inputData.phone) >  -1)){

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

                lastName = newUsers[x][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][y].properties.email.value : 'no email';

            obj.phone = newUsers[x][y].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 = json['vid-offset'];

        page++

        if (json['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;



2 replies

Userlevel 4
Badge +4

@druppe Thanks that worked slick!


Userlevel 1

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


Reply