3

Given a large library of SendGrid templates, how does one move them to a different SendGrid account in one click, say for a test environment?

rothschild86
  • 1,433
  • 16
  • 22

2 Answers2

4

Does not appear to be possible out of the box. The following C# code will get the job done via the API.

Code uses the following nuggets: NewtonSoft Json.NET, SendGrid API Client.

One big caveat: template IDs will be different after the migration. Does not appear to be a way to preserve them.

    public ActionResult MigrateSendGridTemplates() {

        //https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html
        //https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/versions.html

        var fromClient = new SendGridClient("full access api key"); //full access key
        var toClient = new SendGridClient("full access api key"); //full access key - assume blank slate

        //fetch all existing templates

        var templatesRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;

        var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);

        var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>().Reverse();

        foreach (var template in templatesEnumerable)
        {
            //fetch template with versions attached

            var templateWithVerisonRaw = fromClient.RequestAsync(SendGridClient.Method.GET, null, null, $"templates/{template.id}").Result;

            var templateWithVersion = templateWithVerisonRaw.DeserializeResponseBody(templateWithVerisonRaw.Body);

            //create template on the new account

            var templateNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateWithVerisonRaw.Body.ReadAsStringAsync().Result, null, "templates").Result;

            var activeVersion = ((IEnumerable)templateWithVersion["versions"]).Cast<dynamic>().Where(v => v.active).SingleOrDefault();

            if (activeVersion == null)
                continue; //this template does not have any versions to migrate

            //create template version on new account

            var templateNewId = templateNewRaw.DeserializeResponseBody(templateNewRaw.Body)["id"];

            var templateSerialized = JsonConvert.SerializeObject(activeVersion, Formatting.None);

            var templateVersionNewRaw = toClient.RequestAsync(SendGridClient.Method.POST, templateSerialized, null, $"templates/{templateNewId}/versions").Result;
        }

        return Content($"Processed {templatesEnumerable.Count()} templates.");
    }

What follows from this, is that your code should not rely on template IDs if you want your code to work across different sendgrid accounts. Instead, you can build a lookup dictionary and refer to your templates by their friendly names, like so:

public static Dictionary<string, string> GetSendGridTemplates()
{
    var templatesRaw = Persistent.ConfiguredSendGridClient.RequestAsync(SendGridClient.Method.GET, null, null, "templates").Result;

    var templates = templatesRaw.DeserializeResponseBody(templatesRaw.Body);

    var templatesEnumerable = ((IEnumerable)templates.First().Value).Cast<dynamic>();

    var results = new Dictionary<string, string>();

    foreach (dynamic template in templatesEnumerable)
    {
        var activeVersion = ((IEnumerable)template.versions).Cast<dynamic>().Where(v => v.active).SingleOrDefault();

        if (activeVersion == null)
            continue; //skip this one

        results.Add((string)activeVersion.name, (string)template.id);
    }

    return results;
}

Sample result:

reactivated_121519737023655 -> "03e2b62f-51ed-43d0-b140-42bc98a448f6" deactivated_11519736715430 -> "fb3e781b-5e67-45de-a958-bf0cd2682004"

rothschild86
  • 1,433
  • 16
  • 22
2

You can absolutely do it by following the steps:

  • go to your existing template in the old account
  • open Build -> Advanced
  • click on Export HTML

The HTML file will be exported with all the metadata from Sendgrid template editor.

Next, open up the new account and create an template template there:

  • open Build -> Advanced
  • import Drag & Drop HTML
  • save

Now you're able to use the elements in the editor just like in your old template.

Ilyas Assainov
  • 1,901
  • 13
  • 15