Templates Management
Relevant source files
The following files were used as context for generating this wiki page:
Templates Management provides the interface for creating, retrieving, updating, and publishing email templates within the Resend platform. This module handles the conversion of React-based or HTML content into API-compatible formats and manages the lifecycle of a template from draft to published status.
The system supports dynamic content via variables and allows for chaining operations, such as publishing a template immediately after creation or duplication.
Sources: src/templates/templates.ts:1-125, src/templates/interfaces/template.ts:1-24
Template Architecture and Data Model
The Templates class interacts with the Resend API to manage template entities. Each template contains metadata, content, and variable definitions.
Template Entity Structure
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the template. |
name | string | Display name of the template. |
status | 'draft' | 'published' | Current lifecycle state. |
html | string | The HTML content of the template. |
variables | TemplateVariable[] | List of placeholders for dynamic data. |
alias | string | null | Human-readable identifier for the template. |
Sources: src/templates/interfaces/template.ts:1-17
Variable Definitions
Templates use TemplateVariable objects to define dynamic placeholders. Each variable requires a key and a type (string or number), and can optionally include a fallback_value.
Sources: src/templates/interfaces/template.ts:19-25
Core Lifecycle Operations
The Templates class provides methods to manage the full lifecycle of a template.
Creation and Content Rendering
When you create a template, you provide either raw html or a react component. If you provide a react component, the system renders it to HTML using an internal render function before sending the data to the API.
The create method returns a ChainableTemplateResult. This allows you to call .publish() directly on the result of the creation.
Sources: src/templates/templates.ts:31-62, src/templates/interfaces/create-template-options.interface.ts:7-10
Template Updates and Transformation
The system transforms camelCase options from the SDK into snake_case fields required by the API via parseTemplateToApiOptions. This utility ensures fields like replyTo and fallbackValue are correctly mapped to reply_to and fallback_value.
Sources: src/common/utils/parse-template-to-api-options.spec.ts:25-50, src/templates/templates.ts:117-124
Chainable Operations
The SDK supports operational chaining for specific actions. This pattern simplifies workflows where a template must be active immediately after being created or copied.
Chaining Flow
The following sequence diagram illustrates how the duplicate().publish() chain operates:
API Endpoint Reference
The Templates class maps to the following Resend API endpoints:
| Action | Method | Endpoint | Description |
|---|---|---|---|
| Create | POST | /templates | Creates a new template draft. |
| Get | GET | /templates/{id} | Retrieves template details by ID. |
| List | GET | /templates | Lists templates with optional pagination. |
| Update | PATCH | /templates/{id} | Updates an existing template. |
| Remove | DELETE | /templates/{id} | Deletes a template. |
| Duplicate | POST | /templates/{id}/duplicate | Creates a copy of an existing template. |
| Publish | POST | /templates/{id}/publish | Transitions a template to published status. |
Sources: src/templates/templates.ts:31-125, src/templates/interfaces/list-templates.interface.ts:1-25
Pagination and Listing
The list method supports PaginationOptions including before, after, and limit. These parameters are converted into query string properties during the request.
Sources: src/templates/templates.ts:77-86, src/templates/templates.spec.ts:600-640
Summary
Templates Management enables structured email authoring with support for React rendering, dynamic variables, and a robust draft-to-publish workflow. By utilizing chainable results, developers can execute multi-step operations efficiently within a single functional flow.
Sources: src/templates/templates.ts:1-125, src/templates/templates.spec.ts:1-700