-
Notifications
You must be signed in to change notification settings - Fork 9
feat: Impl templates API #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8028707
feat: impl create template
drish 7fec529
feat: get template
drish 1b92dab
feat: update template
drish 8287c98
feat: template update
drish 603dfc8
feat: template duplicate
drish c5294e0
feat: delete and list
drish 3267523
merge main
drish bef44fa
feat: examples for sending emails with templates
drish bbbc66f
fix: do not use should
drish File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = {}) | ||
| 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 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
updatemethod signature diverges from the established pattern in other API modules likeDomainsandEmails. To maintain consistency across the SDK, this method should accept a singleparamshash and derive thetemplate_idfrom it, rather than requiring the ID as a separate argument.Prompt for AI agents