Skip to main content

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

  • February 7, 2020
  • 2 replies
  • 4830 views

BowTieBots
Forum|alt.badge.img+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;


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

2 replies

  • New
  • February 11, 2020

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



BowTieBots
Forum|alt.badge.img+4
  • Author
  • Zapier Solution Partner
  • February 11, 2020

@druppe Thanks that worked slick!