Templates Management
Relevant source files
The following files were used as context for generating this wiki page:
Templates Management allows you to create, store, and maintain reusable email layouts within the Resend platform. This system replaces static hard-coded email bodies with dynamic templates that support variable substitution, aliases, and lifecycle states such as publishing and duplication.
The service handles the storage of HTML and plain text content, manages dynamic variable declarations, and provides unique identifiers or user-defined aliases for email delivery. Templates must typically be published before they are available for use in live email campaigns or automations.
Sources: [templates.go:121-137](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L121-L137), [examples/templates.go:167-180](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/templates.go#L167-L180), [CHANGELOG.md:16-17](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/CHANGELOG.md#L16-L17)
Core Components and Data Structures
The templates module relies on structured requests and response objects to define email layouts and their associated metadata.
Template Definition
A template consists of content (HTML/Text), sender information, and a set of required variables.
| Field | Type | Description |
|---|---|---|
Name | string | The display name of the template. |
Alias | string | An optional unique string identifier used to reference the template instead of a UUID. |
From | string | The default sender email address for this template. |
Subject | string | The default subject line, which can contain variable placeholders. |
Html | string | The HTML content of the email. |
Text | string | The plain text version of the email content. |
Variables | []*TemplateVariable | A list of all dynamic placeholders used in the content. |
Sources: [templates.go:25-34](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L25-L34), [templates.go:101-116](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L101-L116)
Variable Management
Variables allow for dynamic content insertion using the {{{VARIABLE_NAME}}} syntax. All variables used in the HTML or Subject must be explicitly declared in the Variables array during creation or updates.
| Property | Type | Description |
|---|---|---|
Key | string | The name of the variable (e.g., "NAME"). |
Type | VariableType | Supported types: string or number. |
FallbackValue | any | The value used if no specific data is provided during email sending. |
Sources: [templates.go:10-22](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L10-L22), [examples/templates.go:30-33](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/templates.go#L30-L33)
Template Lifecycle and Workflow
Templates follow a specific progression from creation to usage in production.
The diagram shows the state transitions of a template from initial draft creation to its published state and eventual removal.
Sources: [examples/templates.go:21-213](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/templates.go#L21-L213), [templates.go:121-255](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L121-L255)
Lifecycle Operations
- Creation: You initialize a template with a name and content. It starts in a "draft" status.
- Publication: You must call the
Publishmethod to transition a template to the "published" status. Only published templates can be used for sending emails. - Duplication: You create a copy of an existing template. The system generates a new template with a unique ID and "draft" status.
- Removal: You permanently delete a template and its associated metadata.
Sources: [examples/templates.go:167-213](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/templates.go#L167-L213), [templates.go:193-219](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L193-L219)
Integration with Email Services
The Email service utilizes templates by referencing their ID or Alias in a SendEmailRequest.
The sequence diagram illustrates the flow of sending an email using a predefined template.
Sources: [examples/send_email_with_template.go:62-80](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/send_email_with_template.go#L62-L80), [templates_test.go:881-912](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates_test.go#L881-L912)
Overriding Template Fields
When sending an email with a template, you can override default fields defined in the template (such as From, Subject, or ReplyTo) by specifying them directly in the SendEmailRequest.
emailParams := &resend.SendEmailRequest{
From: "support@resend.dev", // Overrides template default
To: []string{"user@example.com"},
Subject: "Custom Subject Override",
Template: &resend.EmailTemplate{
Id: "welcome-template",
Variables: map[string]any{
"userName": "Alice",
},
},
}
Sources: [examples/send_email_with_template.go:99-115](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/send_email_with_template.go#L99-L115), [templates_test.go:996-1015](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates_test.go#L996-L1015)
API Service Interface
The TemplatesSvc interface defines the operations available for template management.
type TemplatesSvc interface {
Create(params *CreateTemplateRequest) (*CreateTemplateResponse, error)
Get(identifier string) (*Template, error)
List(options *ListOptions) (*ListTemplatesResponse, error)
Update(identifier string, params *UpdateTemplateRequest) (*UpdateTemplateResponse, error)
Publish(identifier string) (*PublishTemplateResponse, error)
Duplicate(identifier string) (*DuplicateTemplateResponse, error)
Remove(identifier string) (*RemoveTemplateResponse, error)
}
Sources: [templates.go:121-137](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L121-L137)
Pagination and Identification
- Identification: Most methods accept an
identifierstring, which can be either the template's UUID or its user-definedAlias. - Listing: The
Listoperation supportsListOptionsfor cursor-based pagination usingLimit,After, andBeforeparameters. By default, the API returns the 20 most recent templates.
Sources: [examples/templates.go:94-118](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/templates.go#L94-L118), [templates.go:161-164](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates.go#L161-L164), [templates_test.go:343-375](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/templates_test.go#L343-L375)
Usage in Automations
Templates are frequently used as configuration components within automation steps. When configuring a send_email step in an automation, the template ID is provided in the configuration map.
{
Key: "send_welcome_email",
Type: resend.AutomationStepTypeSendEmail,
Config: map[string]any{
"template": map[string]any{
"id": template.Id,
},
},
}
Sources: [examples/automations.go:42-49](https://github.com/resend/resend-go/blob/4706405e418ddd389208526247900650fc7c7bc5/examples/automations.go#L42-L49)
Summary
Templates Management provides a robust framework for decoupling email content from application logic. By enforcing variable declarations and providing a clear publication lifecycle, it ensures that dynamic email content is validated and version-controlled before deployment. The system integrates seamlessly with the core Email and Automation services via UUIDs or human-readable aliases.