You can automatically trigger a CloudTalk VoiceAgent call using HubSpot Workflows. This guide shows you how to set up the trigger, pass contact data into CloudTalk, and view enrollment results in HubSpot.
User level: Agent
Before you start
You will need:
A CloudTalk VoiceAgent already created
CloudTalk API credentials (API Key and Secret)
A HubSpot account with access to Workflows
Contact properties that include at least a phone number
Create a HubSpot Workflow Trigger
Open HubSpot > Workflows.
Create a new Contact-based workflow.
Choose a trigger type, for example:
Optional: Enable Re-enrollment if you want the same contact to be called multiple times whenever the conditions are met.
Add the Custom Code Action
After setting your trigger:
Click
+ Add actionSelect Data ops > Custom code
Configure the action:
Language: Node.js 20.x
Secrets: Leave empty
Properties to include in code:
phone(required)Additional fields as needed, such as
nameorcontactId
These properties become event.inputFields[...] inside the script.
Paste the Code Snippet
const axios = require('axios');
exports.main = async (event, callback) => {
const phone = event.inputFields['phone'];
const name = event.inputFields['name'];
const contactId = event.inputFields['contactId'];
try {
// Convert hs_object_id to integer
const contactIDint = parseInt(contactId, 10);
// Check if conversion was successful
if (isNaN(contactIDint)) {
throw new Error('Invalid hs_object_id: not a number');
}
// Prepare request body
const data = {
call_number: phone,
voice_agent_id: "67eba86558ba995b89e05e08",
call_properties: {
system_prompt: {
variables: {
name: name
}
},
output: {
variables: {
contactID: contactIDint
}
}
}
};
// Send POST request
const response = await axios.post('https://api.cloudtalk.io/v1/voice-agent/calls', data, {
headers: {
'Authorization': 'Basic {{ Auth Header}}',
'Content-Type': 'application/json'
}
});
// Log success and return
console.log('Call triggered successfully:', response.data);
callback({
outputFields: {
status: 'Call triggered successfully'
}
});
} catch (error) {
console.error('Error triggering call:', error.message);
callback({
outputFields: {
status: `Error: ${error.message}`
}
});
}
};
This script:
Sends a POST request to the CloudTalk VoiceAgent API
Passes the contact’s phone number
Optionally passes variables such as first name or Record ID
Receives the VoiceAgent output back to your selected endpoint in CloudTalk
Generate the Authorization Header
CloudTalk uses Basic Authentication for API requests.
To generate your encoded value:
Enter your CloudTalk API Key and CloudTalk API Secret
(found in your CloudTalk Company Settings).Copy the generated header. It will look similar to:
Basic XXXXXXXXX
Replace {{ Auth Header }} in the script with this value.
Publish and Test the Workflow
Save the custom code step.
Click Review and publish.
Decide whether the workflow should:
Only run for new contacts matching conditions, or
Also run for contacts that already match when the workflow is activated.
Trigger the workflow by updating a property or list membership for a test contact.
Check Enrollment History
To confirm your VoiceAgent call was triggered:
You should see the contact enrolled and the action Call triggered successfully if everything worked.
You're all set!
Your HubSpot workflow will now automatically trigger a CloudTalk VoiceAgent call whenever a contact meets your chosen conditions.
If you have any further questions, please do not hesitate to reach out to our Support team, we are always happy to help!
