Skip to main content

Create

Create a new template using the default context.
func (s *TemplatesSvcImpl) Create(params *CreateTemplateRequest) (*CreateTemplateResponse, error)

Parameters

params
*CreateTemplateRequest
required
The template creation parameters.

Returns

Returns a *CreateTemplateResponse containing the created template ID.

Example

template := &resend.CreateTemplateRequest{
  Name:    "Welcome Email",
  Alias:   "welcome-email",
  From:    "[email protected]",
  Subject: "Welcome {{{name}}}!",
  Html:    "<h1>Hello {{{name}}}</h1><p>Welcome to our service!</p>",
  Variables: []*resend.TemplateVariable{
    {
      Key:           "name",
      Type:          resend.VariableTypeString,
      FallbackValue: "there",
    },
  },
}

response, err := client.Templates.Create(template)
if err != nil {
  panic(err)
}

fmt.Println("Template ID:", response.Id)

CreateWithContext

Create a new template with a custom context.
func (s *TemplatesSvcImpl) CreateWithContext(ctx context.Context, params *CreateTemplateRequest) (*CreateTemplateResponse, error)

Parameters

ctx
context.Context
required
Context for the request, useful for timeouts and cancellation.
params
*CreateTemplateRequest
required
The template creation parameters.

Returns

Returns a *CreateTemplateResponse containing the created template ID.

Example

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

template := &resend.CreateTemplateRequest{
  Name:    "Welcome Email",
  Html:    "<h1>Hello {{{name}}}</h1>",
  Variables: []*resend.TemplateVariable{
    {
      Key:  "name",
      Type: resend.VariableTypeString,
    },
  },
}

response, err := client.Templates.CreateWithContext(ctx, template)
if err != nil {
  panic(err)
}

Types

CreateTemplateRequest

Request payload for creating a template.
type CreateTemplateRequest struct {
  Name      string              `json:"name"`
  Alias     string              `json:"alias,omitempty"`
  From      string              `json:"from,omitempty"`
  Subject   string              `json:"subject,omitempty"`
  ReplyTo   any                 `json:"reply_to,omitempty"` // string or []string
  Html      string              `json:"html"`
  Text      string              `json:"text,omitempty"`
  Variables []*TemplateVariable `json:"variables,omitempty"`
}
Name
string
required
The name of the template.
Alias
string
A unique alias for the template. Can be used instead of the ID to reference the template.
From
string
The sender email address. Can be a plain email or formatted as “Name <[email protected]>”.
Subject
string
The email subject line. Can include template variables like {{{variable_name}}}.
ReplyTo
any
Reply-to email address. Can be a string or array of strings.
Html
string
required
The HTML content of the email template. Use {{{variable_name}}} syntax for variables.Important: All variables referenced in Html must be declared in the Variables array, or the API will return a validation error.
Text
string
Plain text version of the email template.
Variables
[]*TemplateVariable
Array of variable declarations used in the template. All variables referenced in Html or Subject must be declared here.

CreateTemplateResponse

Response from creating a template.
type CreateTemplateResponse struct {
  Id     string `json:"id"`
  Object string `json:"object"`
}
Id
string
The unique identifier of the created template.
Object
string
The object type, always “template”.

TemplateVariable

Represents a variable in a template.
type TemplateVariable struct {
  Key           string       `json:"key"`
  Type          VariableType `json:"type"`
  FallbackValue any          `json:"fallback_value,omitempty"`
}
Key
string
required
The variable name (without the curly braces). For example, for {{{name}}}, use “name”.
Type
VariableType
required
The type of the variable. Can be VariableTypeString or VariableTypeNumber.
FallbackValue
any
Default value to use if the variable is not provided when sending an email.

VariableType

Type constants for template variables.
type VariableType string

const (
  VariableTypeString VariableType = "string"
  VariableTypeNumber VariableType = "number"
)

Build docs developers (and LLMs) love