APIs & Integrations

KPerera
Member

How to search contacts using keywords

SOLVE

I want to search contacts based on keywords through the api.

 

lets say i want to search all contacts where the job title is 'software engineer' and lastname 'Peterson'.

 

how to get the selected group of contacts only?

0 Upvotes
1 Accepted solution
erindigsmusic
Solution
Member | Elite Partner
Member | Elite Partner

How to search contacts using keywords

SOLVE

The following code should do the trick! You can edit the filters and properties to return in the search options. You can also sort the results, but that is not demonstrated here. Check out the documentation for more on that.

 

const axios = require('axios');
 
const headers = {
  'Authorization': `Bearer ${process.env.PRIVATE_APP_KEY}`,
  'Content-Type': 'application/json'
};
 
// SEARCH OPTIONS //
// Define properties to return
let contact_props_to_get = ["hs_object_id", "email"];
// Return only contacts with certain properties
let contact_filters = {
  "filters": [{
    "propertyName": "job_title",
    "operator": "EQ",
    "value": "Software Engineer"
  },
  {
    "propertyName": "lastname",
    "operator": "EQ",
    "value": "Peterson"
  }]
};
 
// FUNCTIONS //
// Search Function - https://developers.hubspot.com/docs/api/crm/search
async function searchObjects(type, filters, sorts, props) {
  let limit = 100;
  let start = 0;
  let search_endpoint = `https://api.hubapi.com/crm/v3/objects/${type}/search`;
  let results_object = [];
  let searchOptions = {
    filterGroups: [filters],
    properties: props,
    sorts: sorts,
    limit: limit
  };
  try {
    searchOptions = JSON.stringify(searchOptions);
    let search_response = await axios.post(search_endpoint, searchOptions, { headers });
    let response_results = search_response.data.results;
    for (let i=0; i < response_results.length; i++) {
      let this_object = response_results[i];
      results_object.push(this_object);
    }
    if (search_response.data.paging) {
      start = start + limit;
      let nextSearchOptions = {
        filterGroups: filters,
        properties: props,
        limit: limit,
        sorts: sorts,
        after: start
      };
      let next_search_response = await axios.post(search_endpoint, nextSearchOptions, { headers });
    }
    existing_contacts = results_object;
  } catch(error) {
    console.log(error);
    throw error
  }
}
 
// Get the HubSpot search results
let hs_contacts = await searchObjects("0-1", contact_filters, [], contact_props_to_get);

 

Hope that helps! Let me know if you have any questions.

View solution in original post

1 Reply 1
erindigsmusic
Solution
Member | Elite Partner
Member | Elite Partner

How to search contacts using keywords

SOLVE

The following code should do the trick! You can edit the filters and properties to return in the search options. You can also sort the results, but that is not demonstrated here. Check out the documentation for more on that.

 

const axios = require('axios');
 
const headers = {
  'Authorization': `Bearer ${process.env.PRIVATE_APP_KEY}`,
  'Content-Type': 'application/json'
};
 
// SEARCH OPTIONS //
// Define properties to return
let contact_props_to_get = ["hs_object_id", "email"];
// Return only contacts with certain properties
let contact_filters = {
  "filters": [{
    "propertyName": "job_title",
    "operator": "EQ",
    "value": "Software Engineer"
  },
  {
    "propertyName": "lastname",
    "operator": "EQ",
    "value": "Peterson"
  }]
};
 
// FUNCTIONS //
// Search Function - https://developers.hubspot.com/docs/api/crm/search
async function searchObjects(type, filters, sorts, props) {
  let limit = 100;
  let start = 0;
  let search_endpoint = `https://api.hubapi.com/crm/v3/objects/${type}/search`;
  let results_object = [];
  let searchOptions = {
    filterGroups: [filters],
    properties: props,
    sorts: sorts,
    limit: limit
  };
  try {
    searchOptions = JSON.stringify(searchOptions);
    let search_response = await axios.post(search_endpoint, searchOptions, { headers });
    let response_results = search_response.data.results;
    for (let i=0; i < response_results.length; i++) {
      let this_object = response_results[i];
      results_object.push(this_object);
    }
    if (search_response.data.paging) {
      start = start + limit;
      let nextSearchOptions = {
        filterGroups: filters,
        properties: props,
        limit: limit,
        sorts: sorts,
        after: start
      };
      let next_search_response = await axios.post(search_endpoint, nextSearchOptions, { headers });
    }
    existing_contacts = results_object;
  } catch(error) {
    console.log(error);
    throw error
  }
}
 
// Get the HubSpot search results
let hs_contacts = await searchObjects("0-1", contact_filters, [], contact_props_to_get);

 

Hope that helps! Let me know if you have any questions.