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.customObjects.associate(
objectType: string,
objectId: string,
toObjectType: string,
toObjectId: string,
associationTypeId?: string
): Promise<true>
fastify.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 | string | No | Custom association type ID. Uses 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— ifobjectTypeis missing.[xHubspot] customObjects.associate requires an objectId— ifobjectIdis missing.[xHubspot] customObjects.associate requires a toObjectType— iftoObjectTypeis missing.[xHubspot] customObjects.associate requires a toObjectId— iftoObjectIdis missing.[xHubspot] customObjects.getAssociations requires an objectType[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.customObjects.associate(
"subscriptions",
"1001",
"contacts",
"12345"
);
associate — link with a specific association type
await fastify.customObjects.associate(
"subscriptions",
"1001",
"companies",
"67890",
"98" // custom association type ID
);
getAssociations — find all contacts linked to a subscription
const contacts = await fastify.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.customObjects.create("subscriptions", {
plan_name: planName,
billing_cycle: billingCycle,
status: "active",
});
await Promise.all([
fastify.customObjects.associate("subscriptions", subscription.id, "contacts", contactId),
fastify.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.customObjects.associate(objectTypeId, recordId, toObjectId, toObjectType) | fastify.customObjects.getAssociations(objectTypeId, recordId, toObjectType)
use-when: Create or read associations between a custom object record and other HubSpot objects
returns: associate → void | getAssociations → { results }
