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 a single fastify.xHubspot namespace exposing five service objects (contacts, companies, deals, engagement, customObjects) plus the raw HubSpot SDK client for advanced use.

Installation

npm install @xenterprises/fastify-xhubspot fastify@5

Quick Start

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

const fastify = Fastify();

// The plugin never reads process.env — the consumer owns env access
// and passes values in via register options.
await fastify.register(xHubspot, {
  apiKey: process.env.HUBSPOT_ACCESS_TOKEN,
});

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

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

Options

OptionTypeDefaultRequiredDescription
apiKeystringYes*HubSpot Private App access token (pat-na1-...).
logRequestsbooleanfalseNoLog each HubSpot API call at debug level.
clientClientNoPre-built @hubspot/api-client Client instance (advanced/testing). When provided, apiKey is not required and no client is constructed.

* Required unless client is provided. The plugin never reads process.env; pass configuration in explicitly.

Decorators

The plugin decorates a single namespace, fastify.xHubspot, with these members:

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

Contacts

Companies

Deals

Engagement

Custom Objects

Error Reference

All methods re-throw the original HubSpot API error unchanged on failure (the message is logged via fastify.log.error first — no tokens or raw SDK error dumps). Input validation errors throw before any API call is made.

ConditionError Message
apiKey missing (and no client provided)xhubspot: missing required option `apiKey` (string), e.g. `app.register(xHubspot, { apiKey: 'pat-na1-...' })`
apiKey not a non-empty stringxhubspot: option `apiKey` must be a non-empty string, e.g. `app.register(xHubspot, { apiKey: 'pat-na1-...' })`
client provided but not an objectxhubspot: option `client` must be a @hubspot/api-client Client instance, e.g. `app.register(xHubspot, { client: new Client({ accessToken: 'pat-na1-...' }) })`
logRequests not a booleanxhubspot: option `logRequests` must be a boolean, e.g. `app.register(xHubspot, { apiKey: 'pat-na1-...', logRequests: true })`
Missing/invalid required arg on any method[xHubspot] <service>.<method> requires a <arg>
Company not found by domain[xHubspot] Company not found for domain: <domain> (error has .status = 404)

Environment Variables

The plugin never reads process.env. The consumer sets these variables and passes the values into app.register(xHubspot, { ... }) — this table documents that consumer convention, not plugin inputs.

VariableDescription
HUBSPOT_ACCESS_TOKENHubSpot Private App access token (App Settings → Private Apps), passed as the apiKey option. Not needed when a pre-built client is passed instead.

How It Works

At registration the plugin creates a single @hubspot/api-client instance using the provided apiKey (unless a pre-built client is passed in), then decorates one fastify.xHubspot namespace exposing the client plus five service objects (contacts, companies, deals, engagement, customObjects). Each service wraps the relevant HubSpot v3 CRM API calls with input validation, debug logging (when logRequests: true), and error forwarding. The raw fastify.xHubspot.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.xHubspot (single namespace: client, contacts, companies, deals, engagement, customObjects)
env: HUBSPOT_ACCESS_TOKEN (consumer-set; passed as the `apiKey` register option — the plugin never reads process.env)
services: contacts, companies, deals, engagement, customObjects — each with create/get/update/delete/list/search/batch/associations
Copyright © 2026