fastify-xrcs
template() / ContentTemplateBuilder
Fluent builder for Twilio Content API payloads supporting all content types — text, media, card, carousel, quick-reply, call-to-action, and list-picker.
template() / ContentTemplateBuilder
fastify.xrcs.template(name?) returns a new ContentTemplateBuilder. Use it to compose Twilio Content API payloads with one or more content types, then pass the result to createTemplate or sendCard/sendCarousel internally.
Signature
fastify.xrcs.template(friendlyName?: string): ContentTemplateBuilder
class ContentTemplateBuilder {
language(lang: string): this
variables(vars: Record<string, string>): this
text(body: string): this
media(urls: string | string[]): this
card(cardOrBuilder: CardBuilder | object): this
carousel(carouselOrBuilder: CarouselBuilder | object): this
quickReply(body: string, actions: QuickReplyAction[]): this
callToAction(body: string, actions: CtaAction[]): this
listPicker(body: string, button: string, items: ListItem[]): this
build(): ContentAPIPayload
}
Builder Methods
| Method | Description |
|---|---|
.language(lang) | Set template language. Default: "en" |
.variables(vars) | Set substitution variables ({ "1": "default" }) |
.text(body) | Add a twilio/text content type |
.media(urls) | Add a twilio/media content type (string or array of URLs) |
.card(cardOrBuilder) | Add a twilio/card content type |
.carousel(carouselOrBuilder) | Add a twilio/carousel content type |
.quickReply(body, actions) | Add a twilio/quick-reply content type |
.callToAction(body, actions) | Add a twilio/call-to-action content type |
.listPicker(body, button, items) | Add a twilio/list-picker content type (WhatsApp/Messenger only) |
.build() | Returns the payload. Throws if no content types added. |
Returns from .build()
{
friendly_name: string,
language: string,
types: Record<string, object>,
variables?: Record<string, string>
}
Throws
[xRCS] Content template must have at least one content type— no content added before.build()
Examples
Text template with variable substitution
const template = fastify.xrcs.template("Order Confirmation")
.text("Hi {{1}}, your order {{2}} has shipped!")
.variables({ "1": "Customer", "2": "#0000" });
const registered = await fastify.xrcs.createTemplate(template);
await fastify.xrcs.sendMessage("+15551234567", registered.sid, {
"1": "Alice",
"2": "#4821"
});
Multi-channel template (text + card)
const template = fastify.xrcs.template("Promo")
.text("Check out our sale!")
.card(
fastify.xrcs.card()
.title("Summer Sale")
.body("20% off all items")
.urlButton("Shop", "https://example.com/sale")
);
const registered = await fastify.xrcs.createTemplate(template);
See Also
- createTemplate(builder, register?) — register a built template
- card() — build card content to embed in a template
- carousel() — build carousel content to embed in a template
AI Context
package: "@xenterprises/fastify-xrcs"
method: fastify.xrcs.template(name?)
use-when: Fluent builder for Twilio Content API payloads — set friendly name and content type, then use with createTemplate()
returns: ContentTemplateBuilder (fluent) → plain template payload on .build()
no-credentials: available in builder-only mode
