Reputation: 41
I'm trying to integrate hubspot and piepdrive. I'm trying to sync both data lists. The problem is it syncs but doesn't update in pipedrive.
In pipedrive I have a client list that I want to update from hubspot, so I'm asing node to automate this process. I want to integrate the hubspot with piepdrive using node.js code.
Here is my code:
const axios = require('axios');
require('dotenv').config(); // Load environment variables
// Pipedrive API credentials
const pipedriveApiKey = process.env.PIPEDRIVE_API_TOKEN; // Use dotenv for secret management
const pipedriveApiUrl = 'https://Rian-Sandbox.pipedrive.com/v1';
// HubSpot API credentials
const hubspotApiKey = process.env.HUBSPOT_API_KEY; // Use dotenv for secret management
const hubspotApiUrl = 'https://api.hubapi.com/crm/v3/objects/contacts';
// Axios configurations for headers
const pipedriveConfig = {
headers: {
'Authorization': `Bearer ${pipedriveApiKey}`,
},
};
const hubspotConfig = {
headers: {
'Authorization': `Bearer ${hubspotApiKey}`,
},
};
// Function to get contacts from HubSpot
async function getHubSpotContacts() {
try {
const response = await axios.get(`${hubspotApiUrl}`, {
params: {
limit: 100, // Adjust the limit as needed
},
headers: hubspotConfig.headers,
});
const hubSpotContacts = response.data.results; // Adjust the property based on the actual response structure
if (!Array.isArray(hubSpotContacts)) {
console.error('HubSpot contacts response is not an array:', hubSpotContacts);
return; // Exit the function or handle the error as needed
}
return hubSpotContacts;
} catch (error) {
console.error('Error fetching HubSpot contacts:', error.message);
throw error;
}
}
// Function to create or update a contact in Pipedrive
async function createOrUpdatePipedriveContact(contact) {
try {
// Check if the contact already exists in Pipedrive based on email
const response = await axios.get(`${pipedriveApiUrl}/persons/find`, {
params: {
term: contact.email,
},
headers: pipedriveConfig.headers,
});
const existingPerson = response.data.data[0];
if (existingPerson) {
// Update the existing person
const updatedResponse = await axios.put(`${pipedriveApiUrl}/persons/${existingPerson.id}`, contact, {
headers: pipedriveConfig.headers,
});
console.log('Contact updated in Pipedrive:', contact.firstname, contact.lastname);
return updatedResponse.data.data;
} else {
// Create a new person in Pipedrive
const createdResponse = await axios.post(`${pipedriveApiUrl}/persons`, contact, {
headers: pipedriveConfig.headers,
});
console.log('Contact created in Pipedrive:', contact.firstname, contact.lastname);
return createdResponse.data.data;
}
} catch (error) {
console.error('Error creating/updating Pipedrive contact:', error.message);
throw error;
}
}
// Function to sync contacts from HubSpot to Pipedrive
async function syncHubSpotToPipedrive() {
try {
const hubSpotContacts = await getHubSpotContacts();
if (!Array.isArray(hubSpotContacts)) {
console.error('HubSpot contacts response is not an array:', hubSpotContacts);
return; // Exit the function or handle the error as needed
}
for (const contact of hubSpotContacts) {
if (
contact.properties &&
contact.properties.firstname &&
contact.properties.lastname &&
contact.properties.email
) {
// Customize the contact mapping according to your needs
const pipedriveContact = {
name: contact.properties.firstname.value + ' ' + contact.properties.lastname.value,
email: contact.properties.email.value,
phone: contact.properties.phone ? contact.properties.phone.value : '',
};
await createOrUpdatePipedriveContact(pipedriveContact);
} else {
console.error('Contact data is incomplete or missing:', contact);
}
}
console.log('HubSpot to Pipedrive sync completed.');
} catch (error) {
console.error('Error during HubSpot to Pipedrive sync:', error.message);
}
}
// Function to start the synchronization
async function startSync() {
await syncHubSpotToPipedrive();
}
// Call the function to start the synchronization
startSync();
Terminal output:
HubSpot to Pipedrive sync completed.
rianc@Rians-MacBook-Pro hubspot % node synchro
Contact data is incomplete or missing: {
id: '1',
properties: {
createdate: '2020-11-29T18:27:35.318Z',
email: '[email protected]',
firstname: 'Maria',
hs_object_id: '1',
lastmodifieddate: '2022-04-26T19:31:14.645Z',
lastname: 'Johnson (Sample Contact)'
},
createdAt: '2020-11-29T18:27:35.318Z',
updatedAt: '2022-04-26T19:31:14.645Z',
archived: false
}
Upvotes: 0
Views: 139
Reputation: 1
I am also trying the same issue. You are matching Hubspot contacts with Pipedrive using email, which is not really a unique identifier as email adress can change. To fix this I created a property in Pipedrive called hubspotID, Already I I have a script that creates contacts in PipeDrive from Hubspot. SO Pipedrive receives the hubSpot ID and piuts it into that property. How do you know that your script is syncing both CRMs?
Upvotes: 0