Templates
Relevant source files
The following files were used as context for generating this wiki page:
Templates provide a system for managing reusable email content within the Resend platform. They allow developers to define email structures with dynamic placeholders, enabling consistent messaging across automated communications.
The template system operates on a state-based workflow where content begins as a draft and must be published before use in live email delivery. Users interact with templates via the resend templates command group to handle the full lifecycle from creation to deployment.
Sources: src/commands/templates/index.ts:13-20, skills/resend-cli/references/templates.md
Template Lifecycle
Templates transition through several states to ensure content safety and version control.
- Draft: All new templates or updates to existing ones start as drafts.
- Published: A template becomes available for email delivery only after a explicit publish action.
- Republishing: When you modify a published template, the changes remain in draft until you publish again.
Lifecycle Flow
The following diagram illustrates the standard progression of a template from initial creation to active use.
The diagram shows how templates move from draft status to a published state, and how updates revert the working version to a draft.
Sources: src/commands/templates/index.ts:13-20, src/commands/templates/publish.ts:12-16
Content Definition Methods
The CLI supports three primary methods for defining template body content. You must provide at least one of these when creating a template.
- Inline HTML: Use the
--htmlflag to provide raw HTML strings directly in the terminal. - HTML Files: Use the
--html-file <path>flag to load content from local files. Passing-reads from standard input (stdin). - React Email: Use the
--react-email <path>flag to bundle and render.tsxcomponents into HTML automatically.
Sources: src/commands/templates/create.ts:15-35, src/commands/templates/create.ts:110-128
Template Variables
Templates utilize dynamic placeholders using the triple-brace syntax: {{{VARIABLE_NAME}}}. Every variable used in the HTML body must be declared during the create or update process.
Variable Declaration
Variables are defined using the --var flag with the format KEY:type or KEY:type:fallback.
| Type | Description |
|---|---|
string | Represents text content. |
number | Represents numeric values. |
Variable Parsing Logic
The CLI validates variable declarations before sending them to the API. It ensures the key exists, the type is valid, and numeric fallbacks are actual numbers.
This diagram represents the internal logic used to parse variable strings into structured data for the Resend SDK.
Sources: src/commands/templates/utils.ts:20-53, src/commands/templates/index.ts:22-26
Integration with Email Delivery
To send an email using a template, use the emails send command with the --template <id> flag.
Usage Rules
- Mutual Exclusivity: You cannot use
--templatealongside body flags like--html,--text, or--react-email. - Variable Injection: Provide values for declared variables using the
--var key=valuesyntax. Note that the send command uses=for assignments, while template management uses:for declarations. - Attachments: You cannot provide custom attachments via the CLI when using a template; attachments must be defined within the template itself on the platform.
Sources: src/commands/emails/send.ts:133-157, src/commands/emails/send.ts:230-245
Command Summary
| Command | Action | Key Arguments/Flags |
|---|---|---|
create | Creates a new draft template. | --name, --html, --var |
list | Displays all templates in a table. | --limit, --after |
update | Modifies an existing template. | <id>, --subject, --from |
publish | Makes a draft live for sending. | <id> |
duplicate | Copies a template as a new draft. | <id> |
open | Opens the template in the dashboard. | [id] |
Sources: src/commands/templates/index.ts:38-46, src/commands/templates/list.ts:35-50, skills/resend-cli/references/templates.md
Example: Creation and Publication Workflow
The sequence shows the transition from a local command to an API-backed draft, followed by the publication step required for email use.
Sources: src/commands/templates/create.ts:134-157, src/commands/templates/publish.ts:25-40
Conclusion
The Templates module provides a structured way to manage email content through a draft-and-publish lifecycle. By supporting local HTML files and React Email integration, it allows developers to maintain complex layouts while using the CLI for deployment and variable management. All templates must be explicitly published before they can be utilized in the email delivery system.
Sources: src/commands/templates/index.ts:13-20, src/commands/templates/create.ts:40-50