fastify-xhubspot
customObjects.associate / customObjects.getAssociations
Create or read associations between a HubSpot custom object record and other CRM objects.
customObjects.associate / customObjects.getAssociations
Create a new association between a custom object record and another CRM object, or read all existing associations of a given type.
Signatures
fastify.xHubspot.customObjects.associate(
objectType: string,
objectId: string,
toObjectType: string,
toObjectId: string,
associationTypeId?: number
): Promise<true>
fastify.xHubspot.customObjects.getAssociations(
objectType: string,
objectId: string,
associationType: string
): Promise<Array<{ id: string; type: string }>>
Params
associate
| Name | Type | Required | Description |
|---|---|---|---|
objectType | string | Yes | The source custom object type name or ID (e.g., "subscriptions"). |
objectId | string | Yes | HubSpot record ID of the source custom object. |
toObjectType | string | Yes | Target object type (e.g., "contacts", "companies", "deals"). |
toObjectId | string | Yes | HubSpot record ID of the target object. |
associationTypeId | number | No | Custom association type ID. Defaults to 1 (the HubSpot default association type) when omitted. |
getAssociations
| Name | Type | Required | Description |
|---|---|---|---|
objectType | string | Yes | The source custom object type name or ID. |
objectId | string | Yes | HubSpot record ID of the source custom object. |
associationType | string | Yes | Target object type to read associations for (e.g., "contacts", "deals"). |
Returns
associate
true on success.
getAssociations
Array of { id, type } for each associated record.
Throws
[xHubspot] customObjects.associate requires an objectType string— ifobjectTypeis missing or not a string.[xHubspot] customObjects.associate requires an objectId— ifobjectIdis missing.[xHubspot] customObjects.associate requires a toObjectType string— iftoObjectTypeis missing or not a string.[xHubspot] customObjects.associate requires a toObjectId— iftoObjectIdis missing.[xHubspot] customObjects.getAssociations requires an objectType string[xHubspot] customObjects.getAssociations requires an objectId[xHubspot] customObjects.getAssociations requires an associationType- Re-throws HubSpot API errors on network failure after logging via
fastify.log.error.
Examples
associate — link a subscription record to a contact
await fastify.xHubspot.customObjects.associate(
"subscriptions",
"1001",
"contacts",
"12345"
);
associate — link with a specific association type
await fastify.xHubspot.customObjects.associate(
"subscriptions",
"1001",
"companies",
"67890",
98 // custom association type ID
);
getAssociations — find all contacts linked to a subscription
const contacts = await fastify.xHubspot.customObjects.getAssociations(
"subscriptions",
"1001",
"contacts"
);
// [{ id: "12345", type: "SUBSCRIPTION_TO_CONTACT" }]
Realistic — create and immediately link a record
fastify.post("/subscriptions", async (request, reply) => {
const { planName, billingCycle, contactId, companyId } = request.body;
const subscription = await fastify.xHubspot.customObjects.create("subscriptions", {
plan_name: planName,
billing_cycle: billingCycle,
status: "active",
});
await Promise.all([
fastify.xHubspot.customObjects.associate("subscriptions", subscription.id, "contacts", contactId),
fastify.xHubspot.customObjects.associate("subscriptions", subscription.id, "companies", companyId),
]);
return reply.code(201).send({ subscriptionId: subscription.id });
});
See also
- customObjects.create — create a custom object record to associate
- contacts.getAssociations — read associations from the contact side
- companies.getAssociations — read associations from the company side
AI Context
package: "@xenterprises/fastify-xhubspot"
methods: fastify.xHubspot.customObjects.associate(objectType, objectId, toObjectType, toObjectId, associationTypeId?) | fastify.xHubspot.customObjects.getAssociations(objectType, objectId, associationType)
use-when: Create or read associations between a custom object record and other HubSpot objects
returns: associate → true | getAssociations → Array<{ id, type }>
