X Enterprises

fastify-x-twilio

Fastify plugin for Twilio communications (SMS, MMS, Conversations, RCS) and SendGrid email — 48 methods across 4 service decorators.

fastify-x-twilio

Fastify plugin combining Twilio and SendGrid communications. Registers up to four service decorators: fastify.sms (SMS/MMS), fastify.conversations (Twilio Conversations), fastify.rcs (RCS rich messaging, optional), and fastify.email (SendGrid transactional email).

Installation

npm install @xenterprises/fastify-xtwilio

Quick Start

import Fastify from "fastify";
import xTwilio from "@xenterprises/fastify-xtwilio";

const fastify = Fastify();

await fastify.register(xTwilio, {
  twilio: {
    accountSid: process.env.TWILIO_ACCOUNT_SID,
    authToken: process.env.TWILIO_AUTH_TOKEN,
    phoneNumber: process.env.TWILIO_PHONE_NUMBER,
  },
  sendgrid: {
    apiKey: process.env.SENDGRID_API_KEY,
    fromEmail: process.env.SENDGRID_FROM_EMAIL,
  },
});

// Send an SMS
await fastify.sms.send("+15551234567", "Hello from Fastify!");

// Send an email
await fastify.email.send("user@example.com", "Welcome!", "<h1>Welcome!</h1>");

Options

twilio options

NameTypeDefaultRequiredDescription
accountSidstringYesTwilio Account SID.
authTokenstringYesTwilio Auth Token.
phoneNumberstringYes*Twilio phone number. Required if messagingServiceSid is not provided.
messagingServiceSidstringYes*Messaging Service SID. Required for scheduling and RCS.
activebooleantrueNoSet false to disable all Twilio services.

*Either phoneNumber or messagingServiceSid is required for SMS.

sendgrid options

NameTypeDefaultRequiredDescription
apiKeystringYesSendGrid API key.
fromEmailstringYesFrom email address.
fromNamestringNoFrom display name.
activebooleantrueNoSet false to disable email service.

Decorators

DecoratorDescription
fastify.smsSMS/MMS messaging via Twilio Messages API.
fastify.conversationsMulti-channel messaging threads via Twilio Conversations API.
fastify.rcsRich Communication Services messaging. Only registered when messagingServiceSid is provided.
fastify.emailTransactional email and contact management via SendGrid.

Methods

SMS

Conversations

RCS

Email

Error Reference

All errors use the [xTwilio] prefix.

ErrorCause
[xTwilio] twilio option must be an objecttwilio option is not an object.
[xTwilio] sendgrid option must be an objectsendgrid option is not an object.
[xTwilio] twilio.accountSid (string) is required for SMSMissing accountSid.
[xTwilio] twilio.authToken (string) is required for SMSMissing authToken.
[xTwilio] Either twilio.phoneNumber or twilio.messagingServiceSid is required for SMSNeither phone nor messaging SID provided.
[xTwilio] sendgrid.apiKey (string) is required for Email serviceMissing SendGrid API key.
[xTwilio] sendgrid.fromEmail (string) is required for Email serviceMissing from email.
[xTwilio] Failed to send SMS: <message>Twilio API error on send.
[xTwilio] Failed to send email: <message>SendGrid API error on send.

Environment Variables

VariableRequiredDescription
TWILIO_ACCOUNT_SIDYesTwilio Account SID.
TWILIO_AUTH_TOKENYesTwilio Auth Token.
TWILIO_PHONE_NUMBERYes*Twilio phone number.
TWILIO_MESSAGING_SERVICE_SIDYes*Messaging Service SID (required for scheduling, RCS).
SENDGRID_API_KEYYesSendGrid API key.
SENDGRID_FROM_EMAILYesFrom email address.

How It Works

On registration the plugin validates the top-level option types and calls four setup* functions. Each service initializes its own Twilio or SendGrid client independently and decorates its namespace on fastify. The rcs service silently skips registration when messagingServiceSid is not provided. Each service can be independently disabled with active: false. All Twilio/SendGrid API errors are caught, logged via fastify.log.error, and re-thrown with [xTwilio] prefix and the original error message attached.

AI Context

package: "@xenterprises/fastify-xtwilio"
type: fastify-plugin
use-when: SMS/MMS, Twilio Conversations, RCS rich messaging, and SendGrid transactional email in one plugin
decorators: fastify.sms, fastify.conversations, fastify.rcs (if messagingServiceSid provided), fastify.email
env: TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER, TWILIO_MESSAGING_SERVICE_SID, SENDGRID_API_KEY, SENDGRID_FROM_EMAIL
services: sms (11 methods), conversations (15 methods), rcs (10 methods), email (12 methods)
Copyright © 2026