X Enterprises
fastify-xhubspot

engagement.createEmail

Log an email engagement against a HubSpot contact.

engagement.createEmail

Create an email engagement and associate it with a HubSpot contact. This logs an outbound or inbound email on the contact timeline without sending the email itself.

Signature

fastify.engagement.createEmail(
  contactId: string,
  emailData: {
    subject?: string;
    body?: string;
    from?: string;
    to?: string;
    ownerId?: string;
  }
): Promise<{ id: string; properties: Record<string, string>; createdAt: string }>

Params

NameTypeRequiredDescription
contactIdstringYesHubSpot contact ID to associate the email engagement with.
emailData.subjectstringNoEmail subject line. Stored as hs_email_subject.
emailData.bodystringNoEmail body text. Stored as hs_email_text.
emailData.fromstringNoSender email address. Stored as hs_email_from.
emailData.tostringNoRecipient email address. Stored as hs_email_to.
emailData.ownerIdstringNoHubSpot owner ID to credit the email to. Stored as hubspot_owner_id.

Returns

FieldTypeDescription
idstringHubSpot engagement ID for the email record.
propertiesobjectStored properties including hs_email_subject, hs_email_text, hs_email_direction ("EMAIL"), hs_timestamp.
createdAtstringISO 8601 creation timestamp.

Throws

  • [xHubspot] engagement.createEmail requires a contactId — if contactId is missing.
  • [xHubspot] engagement.createEmail requires an emailData object — if emailData is not an object.
  • Re-throws the HubSpot API error on network failure after logging via fastify.log.error.

Examples

Basic — log a sent email

const email = await fastify.engagement.createEmail("12345", {
  subject: "Your trial is starting soon",
  body: "Hi Alice, your 14-day trial begins tomorrow...",
  from: "sales@example.com",
  to: "alice@acme.com",
});

console.log(email.id); // "55321"

Realistic — log a transactional email after send

fastify.post("/emails/send", async (request, reply) => {
  const { contactId, subject, body, senderEmail, ownerId } = request.body;

  // Send via your email provider first
  await sendEmail({ to: request.body.recipientEmail, subject, body });

  // Then log it in HubSpot
  const engagement = await fastify.engagement.createEmail(contactId, {
    subject,
    body,
    from: senderEmail,
    to: request.body.recipientEmail,
    ownerId,
  });

  return reply.send({ engagementId: engagement.id });
});

See also

AI Context

package: "@xenterprises/fastify-xhubspot"
method: fastify.engagement.createEmail(emailData)
use-when: Log an email engagement against a HubSpot contact
params: { subject, body, contactId, ownerId? }
returns: { id, properties }
Copyright © 2026