# Using the api client merge endpoint in custom code

**URL:** <https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [June 28, 2022, 9:52am UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924 "2022-06-28T09:52:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![JHowardAdapt](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JHowardAdapt](https://community.hubspot.com/u/JHowardAdapt)\
**Post date:** [June 28, 2022, 9:52am UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/1 "2022-06-28T09:52:53Z")

</div>

Hi,

I am trying to use the new merge endpoint from the api client within a custom code block of a workflow but can’t seem to figure out how to do it, if it is even possible.

I have called vairous other endpoints from the client like so:

```lia-code-sample
await hubspotClient.crm.companies.basicApi.getById(event.object.objectId, ["domain"]).then(results => {
})

```

But I am not sure what the path or the parameters are for the merge endpoint. The merge endpoint can be found in the github repo [https://github.com/HubSpot/hubspot-api-nodejs/blob/master/codegen/crm/companies/apis/PublicObjectApi.ts](https://github.com/HubSpot/hubspot-api-nodejs/blob/master/codegen/crm/companies/apis/PublicObjectApi.ts)

Can someone please help me out with how to use this?

Thanks

J Howard

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [June 28, 2022, 7:21pm UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/2 "2022-06-28T19:21:44Z")

</div>

Hi, @JHowardAdapt 👋 Thanks for reaching out. Let’s see if our community has any experience here — @JBeatty @LMeert, do you have any insight into how @JHowardAdapt can accomplish their goal?

Thank you! — Jaycee

---

<div class="post-metadata">

**Author:** ![LMeert](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/lmeert/32/103847_2.png) [@LMeert](https://community.hubspot.com/u/LMeert)\
**Post date:** [June 29, 2022, 8:48am UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/3 "2022-06-29T08:48:32Z")

</div>

Hi @JHowardAdapt, [@Jaycee\_Lewis](https://community.hubspot.com/t5/user/viewprofilepage/user-id/127074),

You can find here the [documentation for merging two companies.](https://developers.hubspot.com/docs/api/crm/companies#endpoint?spec=POST-/crm/v3/objects/companies/merge)

There’s the nodeJs example that matches what you’ve done so far.  
You’re basically passing two variables : the primary Id as well as the object Id to merge with.

Hope this helps !  
If it does, please consider marking this answer as a solution 🙂

Best,

Ludwig

---

<div class="post-metadata">

**Author:** ![JHowardAdapt](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JHowardAdapt](https://community.hubspot.com/u/JHowardAdapt)\
**Post date:** [June 29, 2022, 9:55am UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/4 "2022-06-29T09:55:17Z")

</div>

Hi Ludwig,

Thanks for this, it seems I was being stupid and didnt notice the node tab on the code example 😣

---

<div class="post-metadata">

**Author:** ![JHowardAdapt](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JHowardAdapt](https://community.hubspot.com/u/JHowardAdapt)\
**Post date:** [June 29, 2022, 2:11pm UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/5 "2022-06-29T14:11:13Z")

</div>

Hi @LMeert Ludwig,

I actually end up getting an undefined error for that endpoint when using the exact example from the documentation you suggest.

“TypeError: Cannot read property ‘merge’ of undefined”

It looks like the publicObjectApi isn’t accessible in the hubspot client?

I am wondering if this is maybe because it’s a relatively new endpoint?

---

<div class="post-metadata">

**Author:** ![AbdelMOULIDA](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/abdelmoulida/32/76793_2.png) [@AbdelMOULIDA](https://community.hubspot.com/u/AbdelMOULIDA)\
**Post date:** [July 25, 2022, 2:18pm UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/6 "2022-07-25T14:18:44Z")

</div>

Hello @JHowardAdapt !

Were you able to merge your companies by domain name?  
I’m trying to do the same thing on my side but I can’t.

Please share with use your code if you were able to make the merge, thank you.

Abdelilah

---

<div class="post-metadata">

**Author:** ![JHowardAdapt](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@JHowardAdapt](https://community.hubspot.com/u/JHowardAdapt)\
**Post date:** [August 2, 2022, 9:53am UTC](https://community.hubspot.com/t/using-the-api-client-merge-endpoint-in-custom-code/59924/7 "2022-08-02T09:53:04Z")

</div>

I had to loop through each company in my account and use this endpoint for each company to see if it’s domain existed multiple times in the system:

```lia-code-sample
 const filter = { propertyName: 'domain', operator: 'EQ', value: companyDomain }
 const filterGroup = { filters: [filter] }
 const sort = JSON.stringify({ propertyName: 'createdate', direction: 'DESCENDING' })
 const properties = propertiesArray
 const limit = 100
 const after = 0

 const publicObjectSearchRequest = {
 filterGroups: [filterGroup],
 sorts: [sort],
 properties,
 limit,
 after,
 }

const result : any = await hubspotRequest("POST", "/crm/v3/objects/companies/search", publicObjectSearchRequest)

```

If this search returned more than 1 company I would merge all of the companies into the one that I identified as being the main company (this is based on our own company logic, so you will need to decide your own). For each company that is returned and is not idenfied as the main one I used this endpoint to merge them into the main company:

```lia-code-sample
 const body = {
 "primaryObjectId": primaryId,
 "objectIdToMerge": toMergeId
 }

 return await hubspotRequest("POST", "/crm/v3/objects/companies/merge", body)

```

Depending on how many companies you have in your Hubpot this could take a long time, I used a stand alone script and ran it on my machine for a day. But I had 100k duplicates.

Also please be aware that the code samples above include methods like hubspotRequest that are my own methods that call my Hubspot api.
