//native
/**
* Create Associations
* Associate a contact with another CRM record using one or more association types, e.g. [{"associationCategory": "HUBSPOT_DEFINED", "associationTypeId": 279}] for contact to company. Use Create Association for other object types or the default association.
*/
export async function main(
auth: RT.Hubspot,
contact_id: number,
to_object_type: string,
to_object_id: number,
associations: {
associationCategory:
| "HUBSPOT_DEFINED"
| "USER_DEFINED"
| "INTEGRATOR_DEFINED"
associationTypeId: number
}[]
) {
const url = new URL(
`https://api.hubapi.com/crm/v4/objects/contacts/${contact_id}/associations/${to_object_type}/${to_object_id}`
)
const response = await fetch(url, {
method: "PUT",
headers: {
Authorization: `Bearer ${auth.token}`,
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify(associations),
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
return await response.json()
}
Submitted by hugo989 1 day ago