X Enterprises

fastify-xhubspot

HubSpot CRM plugin for Fastify — contacts, companies, deals, engagements, and custom objects via the HubSpot v3 API.

fastify-xhubspot

Full HubSpot CRM integration for Fastify v5. Decorates five service objects (contacts, companies, deals, engagement, customObjects) plus the raw HubSpot SDK client for advanced use.

Installation

npm install @xenterprises/fastify-xhubspot @hubspot/api-client

Quick Start

import Fastify from "fastify";
import xHubspot from "@xenterprises/fastify-xhubspot";

const fastify = Fastify();

await fastify.register(xHubspot, {
  apiKey: process.env.HUBSPOT_API_KEY,
});

fastify.post("/contacts", async (request) => {
  const contact = await fastify.contacts.create({
    email: request.body.email,
    firstname: request.body.firstName,
    lastname: request.body.lastName,
  });
  return contact;
});

await fastify.listen({ port: 3000 });

Options

OptionTypeDefaultRequiredDescription
apiKeystringYesHubSpot Private App access token.
logRequestsbooleanfalseNoLog each service call at debug level.

Decorators

DecoratorTypeDescription
fastify.hubspotHubSpotClientRaw @hubspot/api-client instance for advanced/unsupported calls.
fastify.contactsobjectContact management methods.
fastify.companiesobjectCompany management methods.
fastify.dealsobjectDeal management methods.
fastify.engagementobjectEngagement (notes, tasks, calls, emails) methods.
fastify.customObjectsobjectCustom CRM object methods.

Contacts

Companies

Deals

Engagement

Custom Objects

Error Reference

All methods throw the original HubSpot API error on failure (the error is logged via fastify.log.error first). Input validation errors throw synchronously before any API call.

ConditionError Message
apiKey missing or not a string[xHubspot] apiKey is required and must be a non-empty string
logRequests not a boolean[xHubspot] logRequests must be a boolean
Missing required arg on any method[xHubspot] <service>.<method> requires a <arg>
Company not found by domain[xHubspot] Company not found for domain: <domain> (status 404)

Environment Variables

VariableRequiredDescription
HUBSPOT_API_KEYYesHubSpot Private App access token (not an API key — use the token from App Settings → Private Apps).

How It Works

At registration the plugin creates a single @hubspot/api-client instance using the provided accessToken, decorates it on fastify.hubspot, then decorates each of the five service objects. Each service wraps the relevant HubSpot v3 CRM API calls with input validation, debug logging (when logRequests: true), and error forwarding. The raw fastify.hubspot client is available for any HubSpot API surface not covered by the service objects.

AI Context

package: "@xenterprises/fastify-xhubspot"
type: fastify-plugin
use-when: HubSpot CRM integration — contacts, companies, deals, engagements, and custom objects
decorators: fastify.hubspot (raw SDK), fastify.contacts, fastify.companies, fastify.deals, fastify.engagement, fastify.customObjects
env: HUBSPOT_API_KEY (Private App access token)
peer-deps: "@hubspot/api-client"
services: contacts, companies, deals, engagement, customObjects — each with create/get/update/delete/list/search/batch/associations
Copyright © 2026