# Company AssociationsAPI

**URL:** <https://community.hubspot.com/t/company-associationsapi/49112>\
**Category:** APIs & Integrations\
**Tags:** workflows, developer-tools\
**Created:** [December 31, 2021, 2:46pm UTC](https://community.hubspot.com/t/company-associationsapi/49112 "2021-12-31T14:46:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![James\_Swallow](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/james_swallow/32/97852_2.png) [@James\_Swallow](https://community.hubspot.com/u/James_Swallow)\
**Post date:** [December 31, 2021, 2:46pm UTC](https://community.hubspot.com/t/company-associationsapi/49112/1 "2021-12-31T14:46:28Z")

</div>

I’m trying to buid a workflow with custom code that detects when a certain company property has changed and then gives me the individial contacts associted with that company. I’m really struggling with the correct path for the API call, and am unsure which of the three below are the correct route:

1. await hubspotClient.crm.companies.associationsApi.getById(event.object.objectId)
2. await hubspotClient.crm.objects.companies.{objectId}.associations.{toObjectType}
3. await hubspotClient.crm.objects.associationsApi.

Any help would be much appreciated. I simply want it to spit out the ID’s of the associated contacts.

Kind regards,

James

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [January 3, 2022, 7:53am UTC](https://community.hubspot.com/t/company-associationsapi/49112/2 "2022-01-03T07:53:09Z")

</div>

Hi [@James\_Swallow](https://community.hubspot.com/t5/user/viewprofilepage/user-id/117432) ,

You have two simple options here, use the endpoint to list all associations for a specific company:

```lia-code-sample
const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"apiKey":""});

const companyId = "<companyId>";
const toObjectType = "contacts";
const after = undefined;
const limit = 500;

try {
 const apiResponse = await hubspotClient.crm.companies.associationsApi.getAll(companyId, toObjectType, after, limit);
 console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

```

Where the response is:

```lia-code-sample
HTTP 200

{
 "results": [
 {
 "id": "<contactId>",
 "type": "company_to_contact"
 }
 ]
}

```

Or read the company :

```lia-code-sample
const hubspot = require('@hubspot/api-client');

const hubspotClient = new hubspot.Client({"apiKey":""});

const companyId = "<companyId>";
const properties = ["name"];
const associations = [
 "contacts"
];
const archived = false;
const idProperty = undefined;

try {
 const apiResponse = await hubspotClient.crm.companies.basicApi.getById(companyId, properties, associations, archived, idProperty);
 console.log(JSON.stringify(apiResponse.body, null, 2));
} catch (e) {
 e.message === 'HTTP request failed'
 ? console.error(JSON.stringify(e.response, null, 2))
 : console.error(e)
}

```

Where the response is:

```lia-code-sample
HTTP 200

{
 "id": "<companyId>",
 "properties": {
 "createdate": "",
 "hs_lastmodifieddate": "",
 "hs_object_id": "<companyId>"
 },
 "createdAt": "",
 "updatedAt": "",
 "archived": false,
 "associations": {
 "contacts": {
 "results": [
 {
 "id": "<contactId>",
 "type": "company_to_contact"
 }
 ]
 }
 }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [January 4, 2022, 8:21am UTC](https://community.hubspot.com/t/company-associationsapi/49112/3 "2022-01-04T08:21:39Z")

</div>

Hi [@James\_Swallow](https://community.hubspot.com/t5/user/viewprofilepage/user-id/117432) ,  
please use this code for custom code workflow  
const hubspot = require(‘@hubspot/api-client’);  
const request = require(“request”);  
exports.main = async (event, callback) =\> {  
const hubspotClient = new hubspot.Client({  
// apiKey: process.env.HAPIKEY  
apiKey: ‘apikey’  
});  
let hubspot\_owner\_id;  
try {  
const ApiResponse = await hubspotClient.crm.contacts.basicApi.getById(event.object.objectId, [“hubspot\_owner\_id”]);  
hubspot\_owner\_id = ApiResponse.body.properties.hubspot\_owner\_id;  
var options = { method: ‘POST’,  
url: ‘[https://api.hubapi.com/crm/v3/objects/company/search?hapikey=apikey](https://api.hubapi.com/crm/v3/objects/company/search?hapikey=apikey)’,  
headers:  
{  
‘Content-Type’: ‘application/json’ },  
body: {  
“limit”: 100,  
“properties”: [“pipeline”],  
“filters”:[  
{  
“propertyName”: “”,  
“operator”: “”,  
“value”: “”  
},  
{  
“propertyName”: “associations.contact”,  
“operator”: “EQ”,  
“value”: event.object.objectId  
}  
]  
},  
json: true };  
request(options, function (error, response, body) {  
if (error) throw new Error(error);  
if(body.total == 0)  
// No  
callback({  
outputFields: {  
dataStatus: body  
}  
});  
else  
// Yes  
});  
} catch (err) {  
console.error(err);  
// We will automatically retry when the code fails because of a rate limiting error from the HubSpot API.  
throw err;  
}  
// this is for testing  
// we need only hubspot\_owner\_id  
callback({  
outputFields: {  
hubspot\_owner\_id: hubspot\_owner\_id,  
}  
});  
}  
Hope this helps!  
If we were able to answer your query, kindly help the community by marking it as a solution.  
Thanks and Regard.

---

<div class="post-metadata">

**Author:** ![James\_Swallow](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/james_swallow/32/97852_2.png) [@James\_Swallow](https://community.hubspot.com/u/James_Swallow)\
**Post date:** [January 5, 2022, 4:09pm UTC](https://community.hubspot.com/t/company-associationsapi/49112/4 "2022-01-05T16:09:05Z")

</div>

Thanks everyone. I got it working thanks to your replies!
