APIs & Integrations

gangelo
Participant

Creating associations in hubspot-api-nodejs new version

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 (https://github.com/HubSpot/hubspot-api-nodejs#example-create-contact-company-and-associate-created-o...) 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


5 Replies 5
alianjum
Member

Creating associations in hubspot-api-nodejs new version

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>
}
]
}
]
});
```
0 Upvotes
GRajput
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Creating associations in hubspot-api-nodejs new version

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!




Gaurav Rajput
Director, MarTech( Growth Natives)

Book a meeting


himanshurauthan
Thought Leader | Elite Partner
Thought Leader | Elite Partner

Creating associations in hubspot-api-nodejs new version

Hello @gangelo 

create an association with the crm.associations.v4.definitionsApi.create method

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".

Digital Marketing & Inbound Expert In Growth Hacking Technology
gangelo
Participant

Creating associations in hubspot-api-nodejs new version

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
As I said in my original question, the create of this version it's different.

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


0 Upvotes
DFall9
Participant

Creating associations in hubspot-api-nodejs new version

Using client v9.1.1

client.crm.associations.v4.schema.definitionsApi.create('contact', 'company', {
    name: 'MyName',
    label: 'MyLabel'
  })
0 Upvotes