X Enterprises
fastify-xrcs

createTemplate

Build a Twilio Content API payload and optionally register it to get a reusable template SID.

createTemplate

Builds a Twilio Content API payload from a ContentTemplateBuilder (or plain object) and optionally registers it with Twilio to obtain a reusable content SID. When register: false, returns the raw payload without a network call — useful for inspecting or testing the shape.

Signature

fastify.xrcs.createTemplate(
  templateBuilder: ContentTemplateBuilder | object,
  register?: boolean
): Promise<RegisteredTemplate | RawPayload>

Params

NameTypeRequiredDescription
templateBuilderContentTemplateBuilder | objectYesA ContentTemplateBuilder instance (calls .build() internally) or a pre-built payload object
registerbooleanNotrue (default) registers with Twilio and returns a SID; false returns the raw payload without API call

Returns

When register: true (default):

{
  sid: string,
  friendlyName: string,
  language: string,
  types: object,
  dateCreated: Date
}

When register: false: The raw Twilio Content API payload object (no sid).

Throws

  • [xRCS] Twilio credentials required to register templates — called with register: true but no credentials
  • [xRCS] Content template must have at least one content type — builder has no content types
  • [xRCS] Failed to create template: {message} — Twilio API error

Examples

Register a card template and reuse the SID

const template = fastify.xrcs.template("Promo Card")
  .card(
    fastify.xrcs.card()
      .title("Summer Sale")
      .body("Get 20% off all orders this weekend")
      .urlButton("Shop Now", "https://example.com/sale")
      .quickReply("No Thanks", "dismiss")
  );

const registered = await fastify.xrcs.createTemplate(template);
// registered.sid → "HXabc123..."

// Later, send to multiple recipients without re-registering:
await fastify.xrcs.sendMessage("+15551234567", registered.sid);
await fastify.xrcs.sendMessage("+15559876543", registered.sid);

Dry-run — inspect payload without registering

const payload = await fastify.xrcs.createTemplate(
  fastify.xrcs.template("Test").text("Hello {{1}}").variables({ "1": "World" }),
  false  // register: false
);
console.log(JSON.stringify(payload, null, 2));
// { friendly_name: "Test", language: "en", types: { "twilio/text": { body: "Hello {{1}}" } }, variables: { "1": "World" } }

See Also

AI Context

package: "@xenterprises/fastify-xrcs"
method: fastify.xrcs.createTemplate(builder, register?)
use-when: Build a Content API payload and optionally register it with Twilio to get a content SID
params: builder (CardBuilder | CarouselBuilder | ContentTemplateBuilder), register (boolean, default true)
returns: { contentSid, payload } | { payload } (when register: false)
requires: Twilio credentials
Copyright © 2026