HubSpot: Find Contact

Template
Function
1
Template
Function
by
Voiceflow Community

This function finds a contact in HubSpot. The function returns a list contacts filtered by the provided email.

Created:

Heading

Voiceflow APIs used:

Channels
No items found.
Created By
Voiceflow Community
This is some text inside of a div block.
Overview
This is some text inside of a div block.
by
This is some text inside of a div block.

Heading 1

Heading 2

Heading 3

Heading 4

Heading 5
Heading 6

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.

Block quote

Ordered list

  1. Item 1
  2. Item 2
  3. Item 3

Unordered list

  • Item A
  • Item B
  • Item C

Text link

Bold text

Emphasis

Superscript

Subscript

Heading
Based in
This is some text inside of a div block.
Heading

Function Code Snippet


export default async function main(args) {
// Validate input variables from args - find the "validateInputs" function below
const invalidFieldTrace = validateInputs(args)

if (invalidFieldTrace) return invalidFieldTrace;

// Extract input variables from args (destructuring)
const {
userEmail,
hubspotApiToken,
} = args.inputVars;

/**
* Build the payload for batch reading contacts in HubSpot
*
* Check HubSpot documentation to learn what properties are available
*
* - https://developers.hubspot.com/docs/api/crm/contacts
*/
const payload = {
"properties": [
"email",
"firstname",
"lastname",
"lifecyclestage",
],
"idProperty": "email",
"inputs": [
{
"id": userEmail
}
]
}

/**
* Define the URL for HubSpot REST operations
*/
const endpointUrl = `https://api.hubapi.com/crm/v3/objects/contacts/batch/read`;

/**
* Configure the fetch request
*
* Get your HubSpot API Token from the settings in Integration > Private apps
*
* https://developers.hubspot.com/docs/api/private-apps
*/
const config = {
method: 'POST',
headers: {
'Authorization': `Bearer ${hubspotApiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(payload),
};


try {
// Make the API call
const response = await fetch(endpointUrl, config);

// Check if the response status is OK
if (!response.ok) {
throw new Error(`HTTP status code ${response.status}`);
}

// Extract the JSON body from the response
const responseBody = response.json;

// Validate the responseBody structure as expected
if (!responseBody || typeof responseBody !== 'object') {
throw new Error("Invalid or missing response body");
}

// Extract data from the response
const exist = !!responseBody.results.length
const contact = exist ? responseBody.results[0] : null;
const contactFirstName = contact?.properties?.firstname
const contactId = contact?.id

// Create the success return trace with extracted data
return {
outputVars: {
contact: JSON.stringify(contact),
contactFirstName,
contactId,
exist,
},
next: {
path: 'success'
},
trace: [
{
type: "debug",
payload: {
message: 'HubSpot API call successful'
}
},
{
type: "debug",
payload: {
message: `Contact exist: ${exist}`
}
},
{
type: "debug",
payload: {
message: JSON.stringify(contact)
}
},
]
};
} catch (error) {
return {
next: {
path: 'error'
},
trace: [{
type: "debug",
payload: {
message: `HubSpot API call error: ${error.message}`
}
}]
};
}

/**
* Here we validate all the inputs of the Voiceflow function
*
* The function can be used in the first lines of the code
* because of a Javascript concept called hoisting
*
* Read more: https://developer.mozilla.org/en-US/docs/Glossary/Hoisting
*/
function validateInputs(args) {
// Extract input variables from args (destructuring)
const {
userEmail,
hubspotApiToken,
} = args.inputVars;

// Validate that userEmail variable is provided
if (!userEmail) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: userEmail" } }]
};
}

// Validate that hubspotApiToken variable is provided
if (!hubspotApiToken) {
return {
next: { path: 'error' },
trace: [{ type: "debug", payload: { message: "Missing required input variable: hubspotApiToken" } }]
};
}

return null
}
}
copy-icon

Explore More Templates

Build and submit a Template to have it featured in the community.

ghraphic
No items found.
No items found.