Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 190 additions & 0 deletions examples/templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
# frozen_string_literal: true

require_relative "../lib/resend"

raise if ENV["RESEND_API_KEY"].nil?

Resend.api_key = ENV["RESEND_API_KEY"]

# Create a simple template
simple_params = {
name: "welcome-email",
html: "<strong>Welcome to our platform!</strong>"
}

template = Resend::Templates.create(simple_params)
puts "Created template: #{template[:id]}"

# Create a template with variables
template_with_vars = {
name: "personalized-welcome",
html: "<strong>Hey, {{{NAME}}}, you are {{{AGE}}} years old.</strong>",
variables: [
{
key: "NAME",
type: "string",
fallback_value: "user"
},
{
key: "AGE",
type: "number",
fallback_value: 25
}
]
}

template_advanced = Resend::Templates.create(template_with_vars)
puts "Created template with variables: #{template_advanced[:id]}"

# Create a complete template with all optional fields
complete_params = {
name: "complete-template",
alias: "complete",
from: "Acme <[email protected]>",
subject: "Welcome to Acme",
reply_to: "[email protected]",
html: "<h1>Hello {{{FIRST_NAME}}} {{{LAST_NAME}}}</h1><p>Welcome to our platform!</p>",
text: "Hello {{{FIRST_NAME}}} {{{LAST_NAME}}}\n\nWelcome to our platform!",
variables: [
{
key: "FIRST_NAME",
type: "string",
fallback_value: "John"
},
{
key: "LAST_NAME",
type: "string",
fallback_value: "Doe"
}
]
}

complete_template = Resend::Templates.create(complete_params)
puts "Created complete template: #{complete_template[:id]}"
puts "Template object type: #{complete_template[:object]}"

# Get a template by ID
retrieved_template = Resend::Templates.get(template[:id])
puts "\nRetrieved template by ID: #{retrieved_template[:id]}"
puts "Template name: #{retrieved_template[:name]}"
puts "Template status: #{retrieved_template[:status]}"
puts "Template HTML: #{retrieved_template[:html]}"

# Get a template by alias
retrieved_by_alias = Resend::Templates.get("complete")
puts "\nRetrieved template by alias: #{retrieved_by_alias[:alias]}"
puts "Template ID: #{retrieved_by_alias[:id]}"
puts "Template from: #{retrieved_by_alias[:from]}"
puts "Template subject: #{retrieved_by_alias[:subject]}"

if retrieved_by_alias[:variables] && !retrieved_by_alias[:variables].empty?
puts "Template variables:"
retrieved_by_alias[:variables].each do |var|
# Use string keys for nested objects
puts " - #{var['key']} (#{var['type']}): #{var['fallback_value']}"
end
end

# Update a template by ID
update_params = {
name: "updated-welcome-email",
html: "<h1>Welcome!</h1><p>We're glad you're here, {{{NAME}}}!</p>",
variables: [
{
key: "NAME",
type: "string",
fallback_value: "friend"
}
]
}

updated_template = Resend::Templates.update(template[:id], update_params)
puts "\nUpdated template: #{updated_template[:id]}"

# Verify the update by retrieving it again
verified = Resend::Templates.get(template[:id])
puts "Updated template name: #{verified[:name]}"
puts "Updated template HTML: #{verified[:html]}"

# Update a template by alias
alias_update_params = {
subject: "Welcome to Acme - Updated",
from: "Acme Team <[email protected]>"
}

updated_by_alias = Resend::Templates.update("complete", alias_update_params)
puts "\nUpdated template by alias: #{updated_by_alias[:id]}"

# Publish a template by ID
published_template = Resend::Templates.publish(template[:id])
puts "\nPublished template: #{published_template[:id]}"

# Verify the template is published
published_status = Resend::Templates.get(template[:id])
puts "Template status after publish: #{published_status[:status]}"

# Publish a template by alias
published_by_alias = Resend::Templates.publish("complete")
puts "\nPublished template by alias: #{published_by_alias[:id]}"

# Check the published template
final_check = Resend::Templates.get("complete")
puts "Final template status: #{final_check[:status]}"
puts "Published at: #{final_check[:published_at]}"

# Duplicate a template by ID
duplicated_template = Resend::Templates.duplicate(template[:id])
puts "\nDuplicated template - New ID: #{duplicated_template[:id]}"

# Get the duplicated template to see its details
duplicated_details = Resend::Templates.get(duplicated_template[:id])
puts "Duplicated template name: #{duplicated_details[:name]}"
puts "Duplicated template status: #{duplicated_details[:status]}"

# Duplicate a template by alias
duplicated_by_alias = Resend::Templates.duplicate("complete")
puts "\nDuplicated template by alias - New ID: #{duplicated_by_alias[:id]}"

# Get details of the template duplicated from alias
alias_duplicate_details = Resend::Templates.get(duplicated_by_alias[:id])
puts "Alias duplicate name: #{alias_duplicate_details[:name]}"
puts "Alias duplicate from: #{alias_duplicate_details[:from]}"
puts "Alias duplicate subject: #{alias_duplicate_details[:subject]}"

# List all templates
all_templates = Resend::Templates.list
puts "\nListing all templates:"
puts "Total templates: #{all_templates[:data].length}"
puts "Has more: #{all_templates[:has_more]}"

all_templates[:data].each do |t|
puts " - #{t['name']} (#{t['id']}) - Status: #{t['status']}"
end

# List templates with pagination
paginated_templates = Resend::Templates.list({ limit: 2 })
puts "\nListing templates with limit of 2:"
puts "Returned: #{paginated_templates[:data].length} templates"
puts "Has more: #{paginated_templates[:has_more]}"

# List templates after a specific ID
if paginated_templates[:data].length > 0
first_id = paginated_templates[:data][0]['id']
after_templates = Resend::Templates.list({ limit: 2, after: first_id })
puts "\nListing templates after ID #{first_id}:"
puts "Returned: #{after_templates[:data].length} templates"
end

# Remove the duplicated template by ID
removed_template = Resend::Templates.remove(duplicated_template[:id])
puts "\nRemoved template: #{removed_template[:id]}"
puts "Deleted: #{removed_template[:deleted]}"

# Remove the second duplicated template by its ID
removed_by_alias_dup = Resend::Templates.remove(duplicated_by_alias[:id])
puts "\nRemoved duplicated template: #{removed_by_alias_dup[:id]}"
puts "Deleted: #{removed_by_alias_dup[:deleted]}"

# Verify templates were removed by listing again
final_list = Resend::Templates.list
puts "\nFinal template count: #{final_list[:data].length}"
1 change: 1 addition & 0 deletions lib/resend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
require "resend/contacts"
require "resend/domains"
require "resend/emails"
require "resend/templates"

# Rails
require "resend/railtie" if defined?(Rails) && defined?(ActionMailer)
Expand Down
50 changes: 50 additions & 0 deletions lib/resend/templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# frozen_string_literal: true

module Resend
# Templates api wrapper
module Templates
class << self
# https://resend.com/docs/api-reference/templates/create-template
def create(params = {})
path = "templates"
Resend::Request.new(path, params, "post").perform
end

# https://resend.com/docs/api-reference/templates/get-template
def get(template_id = "")
path = "templates/#{template_id}"
Resend::Request.new(path, {}, "get").perform
end

# https://resend.com/docs/api-reference/templates/update-template
def update(template_id, params = {})
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Oct 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The update method signature diverges from the established pattern in other API modules like Domains and Emails. To maintain consistency across the SDK, this method should accept a single params hash and derive the template_id from it, rather than requiring the ID as a separate argument.

Prompt for AI agents
Address the following comment on lib/resend/templates.rb at line 20:

<comment>The `update` method signature diverges from the established pattern in other API modules like `Domains` and `Emails`. To maintain consistency across the SDK, this method should accept a single `params` hash and derive the `template_id` from it, rather than requiring the ID as a separate argument.</comment>

<file context>
@@ -0,0 +1,50 @@
+      end
+
+      # https://resend.com/docs/api-reference/templates/update-template
+      def update(template_id, params = {})
+        path = &quot;templates/#{template_id}&quot;
+        Resend::Request.new(path, params, &quot;patch&quot;).perform
</file context>
Fix with Cubic

path = "templates/#{template_id}"
Resend::Request.new(path, params, "patch").perform
end

# https://resend.com/docs/api-reference/templates/publish-template
def publish(template_id = "")
path = "templates/#{template_id}/publish"
Resend::Request.new(path, {}, "post").perform
end

# https://resend.com/docs/api-reference/templates/duplicate-template
def duplicate(template_id = "")
path = "templates/#{template_id}/duplicate"
Resend::Request.new(path, {}, "post").perform
end

# https://resend.com/docs/api-reference/templates/list-templates
def list(params = {})
path = Resend::PaginationHelper.build_paginated_path("templates", params)
Resend::Request.new(path, {}, "get").perform
end

# https://resend.com/docs/api-reference/templates/delete-template
def remove(template_id = "")
path = "templates/#{template_id}"
Resend::Request.new(path, {}, "delete").perform
end
end
end
end
Loading