Error in creating a contact via HubSpot API V3

I am going to create a contact using API v3 according to this documentation. But it returns “invalid from object type 0-1 for associations to be created. expected: 0-2”. Any idea on this? I just follow the HS documentation.

Hey @dmadusanka,

If all you need to do is create a contact using the v3 Contacts API you can do so by using the request below, there is no need to include the “associations” array.

POST https://api.hubapi.com/crm/v3/objects/contacts
HEADER Authorization Bearer: {{ your_access_token }}
BODY (application/json}

{
 "properties": {
 "company": "Marvel",
 "email": "peterparker@marvel.com",
 "firstname": "Peter",
 "lastname": "Parker",
 "website": "marvel.com"
 }
}

If you would also like to associate a contact to a specific company when you create them you can include the “associations” array but it would require that you know the ID of the company in HubSpot you’d like to associate. A request like this would suffice:

POST https://api.hubapi.com/crm/v3/objects/contacts
HEADER Authorization Bearer: {{ your_access_token }}
BODY (application/json}

{
 "properties": {
 "company": "Marvel",
 "email": "peterparker@marvel.com",
 "firstname": "Peter",
 "lastname": "Parker",
 "website": "marvel.com"
 },
 "associations": [
 {
 "to": {
 "id": "8293678285"
 },
 "types": [
 {
 "associationCategory": "HUBSPOT_DEFINED",
 "associationTypeId": 1
 }
 ]
 }
 ]
}

When creating an association you need to include an “associationTypeId”. For Contacts to Companies there are two standard ones. 1 can be used to make the company the primary associated company. 279 can be used to associate a secondary company. You can retrieve these labels by using the Associations API.

GET https://api.hubapi.com/crm/v4/associations/contacts/companies/labels
HEADER Authorization Bearer: {{ your_access_token }}

RESPONSE
{
 "results": [
 {
 "category": "HUBSPOT_DEFINED",
 "typeId": 279,
 "label": null
 },
 {
 "category": "HUBSPOT_DEFINED",
 "typeId": 1,
 "label": "Primary"
 }
 ]
}

Hope that helps!

@coldrickjack Yes. This is very helpful. I found my issue. I just needed to create a contact but no need for any association. I just removed the associations and it worked. Thank you so much for your detailed post. It really helped me.

Hey @coldrickjack

When I remove associations from the payload I get a typescript error :

Property ''associations'' is missing in type '{ properties: { company: string; email: string; firstname: string; lastname: string; phone: string; website: string; }; }' but required in type 'SimplePublicObjectInputForCreate'.

Do you know how to solve this issue ?