Last active
March 15, 2023 19:02
-
-
Save jackcoldrick90/8c324ce04834c849a83c8a2f671530ae to your computer and use it in GitHub Desktop.
OPERATIONS HUB WORKSHOP #1: Data Enrichment using Clearbit - Collection of code snippets from the first workshop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OPERATIONS HUB WORKSHOP #1: DATA ENRICHMENT USING CLEARBIT | |
These code snippets can be used within a HubSpot custom coded workflow action (https://developers.hubspot.com/docs/api/workflows/custom-code-actions) to query the Clearbit Enrichment API (https://dashboard.clearbit.com/docs#enrichment-api-company-api). | |
The data returned can then be copied into Company properties using the "copy to property" workflow action or using the HubSpot CRM APIs (https://developers.hubspot.com/docs/api/crm/companies). | |
Please note that custom coded workflow actions are a feature of Operations Hub Professional. | |
You can setup a free HubSpot Developer Account by vising https://developers.hubspot.com/ | |
You can find much more code snippets by visting https://www.hubspot.com/programmable-automation-use-cases |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('request'); | |
exports.main = async (event, callback) => { | |
//1. Store the company domain in a variable | |
var companyDomain = event.inputFields['domain']; | |
var domainAlias, tech, subIndustry, legalName; | |
//2. Configure request to Clearbit Discovery API | |
var options = { | |
"method": "GET", | |
"url": "https://company.clearbit.com/v2/companies/find?domain=" + companyDomain, | |
"headers": { | |
"Authorization": "Bearer " + process.env.APIKEY | |
} | |
} | |
//3. Make request to Clearbit Discovery API | |
request(options, function (error, response, body){ | |
//4. Store the data returned in variables | |
domainAlias = JSON.parse(body).domainAliases.join(","); // Domain Aliases | |
tech = JSON.parse(body).tech.join(","); // Tech | |
subIndustry = JSON.parse(body).category.subIndustry; // Industry | |
legalName = JSON.parse(body).legalName; // Legal Name | |
//5. Pass data back to workflow for later use | |
callback({ | |
outputFields: { | |
domainAliases: domainAlias, | |
tech: tech, | |
subIndustry: subIndustry, | |
legalName: legalName | |
} | |
}); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Example 2: Using Node Axios Library, make a HTTP GET request to Clearbit Enrichment API. Data returned can be passed back to the workflow using the callback function and copied to properties using the "Copy to property" workflow action. */ | |
const axios = require('axios'); | |
exports.main = async (event, callback) => { | |
//1. Store the company domain in a variable | |
var companyDomain = event.inputFields['domain']; | |
//2. Make a request to Clearbit | |
axios.get("https://company.clearbit.com/v2/companies/find", { | |
"params": { | |
"domain": companyDomain | |
}, | |
"headers": { | |
"Authorization": "Bearer " + process.env.APIKEY | |
} | |
}) | |
.then((response) => { | |
//3. Pass data back to the workflow | |
callback({ | |
outputFields:{ | |
legalName: response.data.legalName, | |
domainAliases: response.data.domainAliases.join(","), | |
subIndustry: response.data.category.subIndustry, | |
tech: response.data.tech.join(",") | |
} | |
}) | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Example 2: Using Node Axios Library, make a GET request to Clearbit Enrichment API. Then us the HubSpot CRM API to update the Company properties */ | |
const hubspot = require('@hubspot/api-client'); | |
const axios = require('axios'); | |
exports.main = async (event, callback) => { | |
//1. Create a new HubSpot API Client | |
const hubSpotClient = new hubspot.Client({ | |
apiKey: process.env.HAPIKEY | |
}) | |
//2. Store the company domain in a variable | |
var companyDomain = event.inputFields['domain']; | |
//3. Make a request to Clearbit | |
axios.get("https://company.clearbit.com/v2/companies/find", { | |
"params": { | |
"domain": companyDomain | |
}, | |
"headers": { | |
"Authorization": "Bearer " + process.env.APIKEY | |
} | |
}) | |
.then((response) => { | |
//4. Update data using HubSpot CRM Companys API | |
hubSpotClient.crm.companies.basicApi.update(event.object.objectId, { | |
"properties": { | |
// "hubspot property name": "response.data.property" | |
"company_name": response.data.legalName, | |
"domain_aliases": response.data.domainAliases.join(","), | |
"tech": response.data.tech.join(","), | |
"sub_industry": response.data.category.subIndustry | |
} | |
}) | |
}) | |
.catch((error) => { | |
console.log(error); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import required libraries | |
import os | |
import requests | |
import json | |
def main(event): | |
#configure (and make) our request to third party service i.e Clearbits Enrichment API | |
domain = event.get('inputFields').get('domain') | |
url = 'https://company.clearbit.com/v2/companies/find?domain=' + domain | |
headers = {'Authorization': 'Bearer ' + os.getenv('APIKEY')} | |
r = requests.get(url, headers=headers) | |
#print the response, JSON is returned, use json.loads() to convert to Dictionary | |
data = json.loads(r.text) #Dictionary | |
#we can reference parts of the dictionary - "legalName", "domainAliases", "tech" and "subIndustry" | |
legalName = data["legalName"] | |
tech = data["tech"] #List | |
techString = ','.join([str(item) for item in tech]) # iterate through the list and join each item using a comma | |
domainAliases = data["domainAliases"] #List | |
domainAliasesString = ','.join([str(item) for item in domainAliases]) # iterate through the list and join each item using a comma | |
subIndustry = data["category"]["subIndustry"] | |
#we can interate through the entire dictionary if we wanted like this: | |
#for key, value in data.items(): | |
#print(key) | |
#print(value) | |
# return the relevant information | |
return { | |
"outputFields": { | |
"domainAliases": domainAliasesString, | |
"legalName": legalName, | |
"tech": techString, | |
"subIndustry": subIndustry | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment