SendGrid Template Transfer Ruby Script

Currently, there is not an easy option for transferring templates within SendGrid from one account to another; say, from a development account to a production. While trying to execute this for my current job, I could not find a solution that was already created for this very purpose.

However, SendGrid does provide a Template API for this very purpose. This particular solution is for Ruby, but I’m sure other languages can be supported as well. I plan to create a Python solution script for this as well in my free time, so look out for this in the future!I wrote this to save others the pain of doing this from scratch; I do hope that you find this useful! Below I outline the transfer flow, with includedside notes of discoveries I made along the way. Enjoy!


Please refer to the script repo on specifics for configuration.

Script Flow

This script was born out of the lack of resources on how to transfer templates between accounts, using the supplied SendGrid Template API and corresponding Template Versions documentation for transferring multiple versions of a template.

Note: This solution was coded in Ruby 2.1.2; to code in another language, use this flow as a guide, and code against appropriate analogs.

Important Caveat: Currently, this script *does not** selectively copy templates. This script will copy ALL of the templates from the first account.*

With that out of the way, here’s the current flow, all examples within are drawn from the Template API docs:

Step 1. Retrieve all available templates from Account1

In this step, all templates are copied from the first account (the transfer from account). This operation does not copy the template content; only the identifying information is retrieved:

  "templates": [
      "id": "e8ac01d5-a07a-4a71-b14c-4721136fe6aa",
      "name": "example template name",
      "versions": [
          "id": "5997fcf6-2b9f-484d-acd5-7e9a99f0dc1f",
          "template_id": "9c59c1fb-931a-40fc-a658-50f871f3e41c",
          "active": 1,
          "name": "example version name",
          "updated_at": "2014-03-19 18:56:33"

Template versions are also retrieved, but still no template content.

As such, the approach here is to retrieve all the templates, and store the main :id and :name of each in an array in memory. A sample Curl example to the API looks like this:

curl -X GET

Step 2. Recursively retrieve each template from Account1

Using the stored array in memory, each template is then retrieved using :id;

curl -X GET

This operation now retrieves the information of interest needed to transfer the templates intact to the receiving account:

  "templates": [
      "id": "e8ac01d5-a07a-4a71-b14c-4721136fe6aa",
      "name": "example template name",
      "versions": [
          "id": "de37d11b-082a-42c0-9884-c0c143015a47",
          "user_id": 1234,
          "template_id": "d51480ba-ca3f-465c-bc3e-ceb71d73c38d",
          "active": 1,
          "name": "example version",
          "html_content": "<%body%>Click to Reset",
          "plain_content": "Click to Reset<%body%>",
          "subject": "<%subject%>",
          "updated_at": "2014-05-22 20:05:21"

Of interest are the following key pairs for transferring templates intact; :name, :subject, :html_content, :plain_content, and :active. Refer to Template Versions POST action for more information.

Step 3. Backup templates to an external file

The templates (and their versions) will be stored in a JSON file, with the named format as:


This is an additional step meant to safeguard against a complete loss of templates should something happen after transferring templates, e.g., deleting the first account.

Step 4. Create an empty template in Account2

Before any templates can be copied to the second account (transfer to account), a new template needs to be created. A name is required to create the new template; the :name key pair saved in Step #1 is used in this step. A prepend string can be configured to distinguish the new template from any existing templates in the second account:

curl -H "Content-Type: application/json" -X POST -d {"name":"Prepend_text"+"template_name"}


1. Newly created template will have a different :id than that of the existing template.

2. If the template already exists, template creation will fail, and a 400 response is returned. There is currently no check to skip already created templates, and it is unknown how the process will fail as this scenario has not been tested yet.

3. Version names must be unique across templates within an account.

Step 5. Populate template

Transfer the template content to the newly created template, using the Template Versions API.

curl -H "Content-Type: application/json" -X POST -d @template_content.json

The versions of each template is pushed, one at a time, to the new template, using the required key pairs described in Step #2. As a safeguard, all other information is stripped from the version before pushing changes, i.e., information such as :user_id and :updated_at, as these are regenerated on transfer.

Step 6. Wash, Rinse, Repeat

Repeat Steps #2 – #5 for every template in the array retrieved in Step #1. At the end, you should have all templates transferred from your first account to the second!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Back to top
%d bloggers like this: