# Creating associations in hubspot-api-nodejs new version

**URL:** <https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703>\
**Category:** APIs & Integrations\
**Tags:** sdk\
**Created:** [June 6, 2023, 1:27am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703 "2023-06-06T01:27:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gangelo](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gangelo](https://community.hubspot.com/u/gangelo)\
**Post date:** [June 6, 2023, 1:27am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/1 "2023-06-06T01:27:49Z")

</div>

Hi everyone! I’m using the nodejs-api for the first time, but I was reading the documentation, and the readme still needed to be updated. This readme ([GitHub - HubSpot/hubspot-api-nodejs: HubSpot API NodeJS Client Libraries for V3 version of the API · GitHub](https://github.com/HubSpot/hubspot-api-nodejs#example-create-contact-company-and-associate-created-objects)) it’s using _ **crm.associations.v4.basicApi.create** _ to create an association, but the current node API has: _ **crm.associations.v4.definitionsApi.create** _, which has different parameters. Could someone help me create an association with this API version? The idea it’s to create in the exact same way from the readme example

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [June 6, 2023, 8:16am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/2 "2023-06-06T08:16:06Z")

</div>

Hello @gangelo  
create an association with the crm.associations.v4.definitionsApi.create method

```lia-code-sample
import { Client } from "@hubspot/api-client";

const hubspotClient = new Client({ accessToken: YOUR_ACCESS_TOKEN });

const associationRequest = {
 fromObjectType: "contact",
 fromObjectId: "1234567890",
 toObjectType: "company",
 toObjectId: "9876543210",
 associationType: "default",
};

await hubspotClient.crm.associations.v4.definitionsApi.create(associationRequest);

```

The associationRequest object contains the following properties:

fromObjectType: The type of the object you’re associating (e.g. contact).  
fromObjectId: The ID of the record to associate.  
toObjectType: The type of the object you’re associating the record to (e.g. company).  
toObjectId: The ID of the record to associate to.  
associationType: The type of association you want to create. The default value is “default”.

---

<div class="post-metadata">

**Author:** ![gangelo](https://avatars.discourse-cdn.com/v4/letter/g/b9e5f3/32.png) [@gangelo](https://community.hubspot.com/u/gangelo)\
**Post date:** [June 6, 2023, 12:43pm UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/3 "2023-06-06T12:43:48Z")

</div>

Hey himanshurauthan! Thank you for the quick answer!

So, I tried your code here, and it’s giving me this warning/error:

 ![SnippingTool_IAupqQupIw.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/9/c99a1b2aaf032a14e36294763b6545cacee149d2.png)

As I said in my original question, the create of this version it’s different.

```lia-code-sample
export declare class PromiseDefinitionsApi {
 private api;
 constructor(configuration: Configuration, requestFactory?: DefinitionsApiRequestFactory, responseProcessor?: DefinitionsApiResponseProcessor);
 archive(fromObjectType: string, toObjectType: string, associationTypeId: number, _options?: Configuration): Promise<void>;
 create(fromObjectType: string, toObjectType: string, publicAssociationDefinitionCreateRequest: PublicAssociationDefinitionCreateRequest, _options?: Configuration): Promise<CollectionResponseAssociationSpecWithLabelNoPaging>;
 getAll(fromObjectType: string, toObjectType: string, _options?: Configuration): Promise<CollectionResponseAssociationSpecWithLabelNoPaging>;
 update(fromObjectType: string, toObjectType: string, publicAssociationDefinitionUpdateRequest: PublicAssociationDefinitionUpdateRequest, _options?: Configuration): Promise<void>;
}

```

The create receives 4 parameters

---

<div class="post-metadata">

**Author:** ![DFall9](https://avatars.discourse-cdn.com/v4/letter/d/e68b1a/32.png) [@DFall9](https://community.hubspot.com/u/DFall9)\
**Post date:** [September 15, 2023, 3:14am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/4 "2023-09-15T03:14:24Z")

</div>

Using client v9.1.1

```lia-code-sample
client.crm.associations.v4.schema.definitionsApi.create('contact', 'company', {
 name: 'MyName',
 label: 'MyLabel'
 })

```

---

<div class="post-metadata">

**Author:** ![TShma](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tshma/32/16907_2.png) [@TShma](https://community.hubspot.com/u/TShma)\
**Post date:** [February 8, 2024, 10:40am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/5 "2024-02-08T10:40:15Z")

</div>

I am not sure what exactly are you trying to do, but if you want to use an HS defined association, just use the crm.associations.v4.basicApi.createDefault(). It receives the 4 first arguments as you sent it, and that’s all. The create() allows you to define new custom association types, and label them.  
The basic association between contact and company already defined by HS (and so, it’s considered “unlabeled”).

---

<div class="post-metadata">

**Author:** ![GRajput](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/grajput/32/102020_2.png) [@GRajput](https://community.hubspot.com/u/GRajput)\
**Post date:** [June 8, 2023, 10:52am UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/6 "2023-06-08T10:52:20Z")

</div>

Hi @gangelo

Sure, I can help you with that. Here is the code to create an association using the HubSpot Node.js API v4:

```  
const hubspot = require(‘hubspot-api-nodejs’);

// Create a new client  
const client = new hubspot.Client({  
apiKey: ‘\<your-api-key\>’,  
hubspotUrl: ‘\<your-hubspot-url\>’,  
});

// Get the contact and company records  
const contact = await client.crm.objects.contacts.get({  
id: ‘\<contact-id\>’,  
});

const company = await client.crm.objects.companies.get({  
id: ‘\<company-id\>’,  
});

// Create the association  
const association = await client.crm.associations.v4.definitionsApi.create({  
fromObjectType: ‘contact’,  
fromObjectId: contact.id,  
toObjectType: ‘company’,  
toObjectId: company.id,  
});  
```

This code will create an association between the contact and company records. The `fromObjectType` and `fromObjectId` properties specify the contact record, and the `toObjectType` and `toObjectId` properties specify the company record.

Hope this will helps you out. Please mark it as Solution Accepted to help other Community member.  
Thanks!

---

<div class="post-metadata">

**Author:** ![alianjum](https://avatars.discourse-cdn.com/v4/letter/a/779978/32.png) [@alianjum](https://community.hubspot.com/u/alianjum)\
**Post date:** [June 22, 2023, 2:36pm UTC](https://community.hubspot.com/t/creating-associations-in-hubspot-api-nodejs-new-version/82703/7 "2023-06-22T14:36:36Z")

</div>

You can create assocaition while creating the contact object or company object. In v9 of node hubspot/api-client. You need to pass associations parameter while creating an object.  
An example of creating a note and associate with contact.  
```

const note = await hsClient.crm.objects.notes.basicApi.create({

properties: {

hs\_timestamp: moment.utc().toISOString(),

hs\_note\_body: ‘test note’

},

associations: [

{

to: {

id: \<contactId\>

},

types: [

{

associationCategory: ‘HUBSPOT\_DEFINED’,

associationTypeId: \<notes\_to\_deal\_associationId\>

}

]

}

]

});  
```
