19/09/2023
Here's a quick article to show you how you can use HubSpot Operations Hub with a Custom Coded Action to automatically query the People Data Labs API to get more information about a company from its domain name.
Concept
See how it works / and implement it
PeopleDataLabs API key
You need to get an API key on the PeopleDataLabs website’s.
You need to set the peopleDataLabsAPI
key in the secret, the API key has to be set in the secret section of the Custom Coded Action. Use the name peopleDataLabsAPI
Set the domain name variable
Set the variable name domainName
in the property to include in code.
Code
Paste the following code :
const axios = require('axios');
exports.main = async (event, callback) => {
if (!process.env.peopleDataLabsAPI) throw new Error('The peopleDataLabs API key has to be set in the secret section');
const domainName = event.inputFields.domainName;
if (!domainName) throw new Error('domainName is not set, are you sure you put domainName in the "properties to include in code" ? ');
const websiteInfos = await getWebsiteInfos(domainName).catch(axiosErrorHandler)
if (!websiteInfos.data) throw new Error(`We couldn't grab your websiteInfos infos`);
const {
name,
size,
employee_count,
id,
founded,
industry,
linkedin_id,
linkedin_url,
facebook_url,
twitter_url,
summary
} = websiteInfos.data;
callback({
outputFields: {
name,
size,
employee_count,
id,
founded,
industry,
linkedin_id,
linkedin_url,
facebook_url,
twitter_url,
summary
}
});
}
/**
* Retrieves information about a website's associated company using the PeopleDataLabs API.
*
* @async
* @function
* @param {string} domainName - The domain name of the website for which you want to fetch information.
* @throws {Error} Throws an error if domainName is not a string or if it is empty.
* @returns {Promise<Object>} A Promise that resolves to an object containing information about the company.
* @see {@link https://docs.peopledatalabs.com/|PeopleDataLabs API Documentation}
*/
const getWebsiteInfos = async (domainName) => {
if (typeof domainName !== 'string' || domainName.trim() === '') throw new Error('Invalid domainName parameter. It must be a non-empty string.');
// Construct the API endpoint URL with the provided domainName and API key.
const endpoint = `https://api.peopledatalabs.com/v5/company/enrich?api_key=${process.env.peopleDataLabsAPI}&pretty=True&website=${domainName}`;
// Make a GET request to the API and return the result.
return axios.get(endpoint);
}
/**
* Handles errors thrown by axios requests and logs relevant information.
*
* @param {Error} e - The error object thrown by axios.
*/
const axiosErrorHandler = e => {
console.log(e);
//console.log("The : ", e.config.method, " call on", e.config.url, "failed");
console.log("error code retuned ", e.code);
console.log("error data returned ", e.response.data);
}
Set the output / to use the data in the WorkFlow
name,
size,
employee_count,
id,
founded,
industry,
linkedin_id,
linkedin_url,
facebook_url,
twitter_url,
summary
Like so :
Then don’t forget to finish the logic with copy property values blocks to update the company record.