Working with HubSpot Association API v4 in Custom Code
Master the HubSpot Associations v4 API in TypeScript to query, create, and remove multi-object relationships (Contacts, Companies, Deals, Custom Objects).
Understanding Associations v4 Architecture
HubSpot's **Associations API v4** allows developers to manage complex, multi-labeled relationships between any CRM objects (e.g., distinguishing between a "Billing Contact" vs. an "Executive Sponsor" on a Deal).
In Workflood, you can query and batch-create associations between: - Standard objects: `contacts`, `companies`, `deals`, `tickets` - Marketing objects: `marketing_events`, `lists` - Custom objects: `2-[object_type_id]`
Querying Associated Records
To retrieve all companies associated with a specific Contact ID:
// Fetch associated company IDs for a contact
const associations = await hubspot.client.crm.associations.v4.basicApi.getPage(
'contacts', // From Object Type
'12345678', // From Object ID
'companies', // To Object Type
10 // Limit (Max 500)
)
const companyIds = associations.results.map((r) => r.toObjectId)
console.log('Associated Company IDs:', companyIds)Creating Labeled Associations
When creating associations, you specify the **Association Category** (`HUBSPOT_DEFINED` or `USER_DEFINED`) and the **Association Type ID**:
// Associate a Deal to a Marketing Event for Revenue Attribution
await hubspot.client.crm.associations.v4.basicApi.create(
'deals', // From Object Type
dealId, // From Object ID
'marketing_events',// To Object Type
marketingEventId, // To Object ID
[
{
associationCategory: 'HUBSPOT_DEFINED',
associationTypeId: 15 // HubSpot-defined Deal to Event type
}
]
)Common Association Type IDs Reference
| From Object | To Object | Category | Standard Type ID |
|---|---|---|---|
| **Contact** | **Company** (Primary) | HUBSPOT_DEFINED | 1 |
| **Company** | **Contact** | HUBSPOT_DEFINED | 2 |
| **Deal** | **Contact** | HUBSPOT_DEFINED | 3 |
| **Contact** | **Deal** | HUBSPOT_DEFINED | 4 |
| **Deal** | **Company** (Primary) | HUBSPOT_DEFINED | 5 |
| **Company** | **Marketing Event** | HUBSPOT_DEFINED | 14 |
| **Deal** | **Marketing Event** | HUBSPOT_DEFINED | 15 |
Associate Company to Marketing Event on Attendance
When a contact attends a marketing event (webinar, conference), look up their associated company and link the company to the marketing event for account-level event reporting.
Frequently Asked Questions
How do I associate custom objects using Associations v4?
Use the fully qualified custom object name (e.g., "2-1234567") as the object type identifier in the basicApi.getPage and basicApi.create calls.
