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. This module allows you to create, version, and organize email layouts using HTML and plain text with dynamic variable substitution.
The system distinguishes between draft and published states, enabling controlled updates to email communications. Developers interact with templates through the Template service, which handles the communication with the Resend API to perform CRUD operations, publishing, and duplication of template resources.
Template Resource Model
The Template class represents the data structure of an email template. It includes metadata, content, and configuration for variables.
Data Structure
The following table describes the fields available in a template resource:
| Field | Type | Description |
|---|---|---|
id | string | Unique identifier for the template. |
name | string | Human-readable name of the template. |
alias | string | Unique string identifier used to reference the template. |
status | string | Current state: published or draft. |
from | string | Default sender email address. |
subject | string | Default subject line for emails sent with this template. |
html | string | HTML content of the email. |
text | string | Plain text fallback content. |
variables | array | Collection of keys and types used for dynamic replacement. |
created_at | string | ISO timestamp of creation. |
Sources: src/Template.php:6-21, tests/Fixtures/Template.php:5-28
Variables Configuration
Templates support dynamic content through a variables array. Each variable entry defines a key, a type (such as string), and an optional fallback_value.
'variables' => [
[
'id' => 'e169aa45-1ecf-4183-9955-b1499d5701d3',
'key' => 'user_name',
'type' => 'string',
'fallback_value' => 'John Doe',
],
]
Sources: tests/Fixtures/Template.php:18-27
Template Service Architecture
The Resend\Service\Template class executes API operations by constructing specific Payload objects and passing them to a Transporter.
Service Methods
The service implements seven primary operations to manage the template lifecycle:
- get(string $id): Retrieves a specific template by its ID.
- create(array $parameters): Generates a new template resource.
- list(array $options): Returns a paginated collection of all templates.
- update(string $id, array $parameters): Modifies an existing template.
- remove(string $id): Deletes a template from the system.
- publish(string $id): Transitions a template from
drafttopublishedstatus. - duplicate(string $id): Creates a copy of an existing template.
Sources: src/Service/Template.php:14-94
Data Flow Diagram
The following diagram illustrates how the Template service interacts with the internal Transporter and the Payload value object to execute an API request.
This flow ensures that request parameters are validated and formatted into the correct HTTP methods before transmission.
Sources: src/Service/Template.php:25-33, tests/ValueObjects/Transporter/Payload.php:115-125
Template Lifecycle and Operations
Templates follow a specific lifecycle involving creation, modification, and publication.
Lifecycle Management
When you create a template, it typically begins as a draft. You must explicitly publish it to make it active for production use.
The diagram shows the transition states between draft and published versions.
Sources: src/Service/Template.php:75-94, tests/Service/Template.php:67-93
Operation Summary
| Operation | HTTP Method | Endpoint |
|---|---|---|
| Create | POST | /templates |
| Get | GET | /templates/{id} |
| Update | PATCH | /templates/{id} |
| List | GET | /templates |
| Delete | DELETE | /templates/{id} |
| Publish | POST | /templates/{id}/publish |
| Duplicate | POST | /templates/{id}/duplicate |
Sources: src/Service/Template.php:14-94, tests/Service/Template.php:7-93
Technical Implementation Details
The implementation relies on the Payload value object to define HTTP behaviors like method types and path construction.
Payload Construction
For operations like publish and duplicate, the system sends a POST request with an empty JSON body ({}).
$payload = Payload::publish('templates', $id);
$result = $this->transporter->request($payload);
Sources: src/Service/Template.php:77-79, tests/ValueObjects/Transporter/Payload.php:115-125
Pagination
The list method supports pagination options including limit, before, and after. These are passed as query parameters to the Resend API.
Sources: src/Service/Template.php:39-49, tests/ValueObjects/Transporter/Payload.php:48-55
Templates allow developers to decouple email design from application logic. By utilizing the Template service, you manage content via IDs and aliases, ensuring that layout changes do not require code deployments.