Skip to content

Instantly share code, notes, and snippets.

@jackcoldrick90
Created February 10, 2022 10:41
Show Gist options
  • Select an option

  • Save jackcoldrick90/e44d7c56696d8610bdb9a444ceae0632 to your computer and use it in GitHub Desktop.

Select an option

Save jackcoldrick90/e44d7c56696d8610bdb9a444ceae0632 to your computer and use it in GitHub Desktop.
OPERATIONS HUB WORKSHOP #2: Email Validation using Kickbox - Collection of code snippets from the first workshop.
OPERATIONS HUB WORKSHOP #2: EMAIL VALIDATION USING KICKBOX
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 Contact 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
/* Example 1: Make a HTTP Request to Kickbox Verification API using NodeJS request. */
//1. Import libraries, in this instance "request" to make HTTP requests
const request = require('request')
exports.main = async (event, callback) => {
//2. Define variables to store data
var role, free, disposable, sendex, result, reason; // We will use these later on when data is returned from Kickbox
const email = event.inputFields['email']; // We store the contact email in a variable and will query Kickbox
//3. Configure our HTTP Request options
var options = {
"method": "GET",
"url": "https://api.kickbox.com/v2/verify?email=" + email + "&apikey=" + process.env.KICKBOXAPI
}
//4. Make our HTTP Request
request(options, function (error, response, body) {
//5. Store the returned data in the variables we created earlier
role = JSON.parse(body).role;
free = JSON.parse(body).free;
disposable = JSON.parse(body).disposable;
sendex = JSON.parse(body).sendex;
result = JSON.parse(body).result;
reason = JSON.parse(body).reason;
//6. Pass data back to worklfow using outputFields - make sure to also define data output types
callback({
outputFields: {
"role": role,
"free": free,
"disposable": disposable,
"sendex": sendex,
"result": result,
"reason": reason
}
});
})
}
/* Example 2: Make a HTTP Request to Kickbox Email Verification API using NodeJS axios */
//1. Import libraries, in this instance "axios" to make HTTP requests
const axios = require('axios');
exports.main = async (event, callback) => {
//2. Store contact email in variable
var email = event.inputFields['email'];
//3. Configure and Make our HTTP request to Kickbox Email Verification API
axios.get("https://api.kickbox.com/v2/verify", {
"params": {
"email": email, // include contact email
"apikey": process.env.KICKBOXAPI // include API key
}
})
.then((response) => { // Run code when we get response
//4. Pass the data back to workflow using outputFields - make sure to also define data output types
callback({
outputFields: {
"role": response.data.role,
"free": response.data.free,
"disposable": response.data.disposable,
"sendex": response.data.sendex,
"result": response.data.result,
"reason": response.data.reason
}
})
})
.catch((error) => {
console.log(error); // Output any errors
});
}
# Example 3: Make a HTTP request to Kickbox Email Verification using Python requests package
# 1. Import required libraries
import os
import requests #for http requests
import json #to decode JSON returned
def main(event):
# 2. Configure and make our HTTP request to Kickbox Email Verification API
email = event.get('inputFields').get('email') # Store contact emai
url = 'https://api.kickbox.com/v2/verify?email=' + email + '&apikey=' + os.getenv('KICKBOXAPI')
r = requests.get(url) # store results in variable r
#3. Convert results to Python Dictionary
data = json.loads(r.text) #Dictionary
#4. Store the returned data in variables
result = data["result"]
reason = data["reason"]
sendex = data["sendex"]
role = data["role"]
free = data["free"]
disposable = data["disposable"]
#OPTIONAL: we can interate through the entire dictionary if we wanted like this:
#for key, value in data.items():
#print(key)
#print(value)
# 5. Pass data back to workflow using outputFields - make sure to also define data output types
return {
"outputFields": {
"result": result,
"reason": reason,
"sendex": sendex,
"role": role,
"free": free,
"disposable": disposable
}
}
/* Example 4: Make a HTTP request to Kickbox Email Verification API using NodeJS Axios, then use the HubSpot NodeJS Client to update the contact properties with returned data */
//1. Import required libraries, in this instance the HubSpot API client and axios
const hubspot = require('@hubspot/api-client'); // to use Hubspot API
const axios = require('axios'); // for http requests
exports.main = async (event, callback) => {
//2. Create a new HubSpot API Client
const hubSpotClient = new hubspot.Client({
apiKey: process.env.HAPIKEY // use our HubSpot API key
})
//3. Store the contact email in a variable
var email = event.inputFields['email'];
//4. Configure and Make a request to Kickbox Email Verification API
axios.get("https://api.kickbox.com/v2/verify", {
"params": {
"email": email, // include contact email
"apikey": process.env.KICKBOXAPI // include Kickbox API key
}
})
.then((response) => { // Run code when we get response
//5. Update data using HubSpot CRM Contact API - event.object.objectId is the ID of the contact currently enrolled in workflow
hubSpotClient.crm.contacts.basicApi.update(event.object.objectId, {
"properties": {
// "hubspot property name": "response.data.property" - format for any additional fields you want to include
"role_address": response.data.role,
"free_address": response.data.free,
"disposable_address": response.data.disposable,
"sendex": response.data.sendex,
"reason": response.data.reason,
"result": response.data.result
}
})
})
.catch((error) => {
console.log(error); // Output any errors
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment