DEV Community

Cover image for Backup Sendgrid templates
Sergio Morstabilini
Sergio Morstabilini

Posted on

Backup Sendgrid templates

Sendgrid is one of the most used email delivery service. Among other things, Sendgrid allows you to design your email templates using handlebars tags to dynamically change the content of your messages given the payload you provide.

The WISIWYG editor

Sendgrid online tools allow you to create your templates both with a WYSIWYG editor and a text editor. That means that you can create the templates and your designer coworkers can fix the colors, the fonts or the copy of your work with an easy-to-use interface.

the editor

Drawbacks

Using an online tool your colleagues don't have to learn how to use git or to setup a work environment on their computers. On the other side, if don't physically have your files on your computer you can't save them in a git repository.

Sendgrid doesn't keep the changes history of your templates, so it was a bit creepy for me to put effort in creating a template knowing that a few clicks could cancel the job done till that very moment. After all, a misplaced { is enough to break a template and we all know sh*t happens. In the worst scenario you might have to create again the template from scratch.
What did you say? Has this ever happened to me? Nooo, I'm too careful to accidentally overwrite my templates! 😆 😉

Backupping the templates

With the Sendgrid APIs it's easy to retrieve your precious files.

Getting the list of templates

First you need to retrieve the list of existing templates calling this endpoint.

template list

Just remember that you need an API key with full access.
You can also specify the page_size param but since the max number of templates you can create is limited to 300 you shouldn't have any problem getting the list in just one shot.

When you have the list of existing templates you loop over them and retrieve their HTML code.

Retrieving the template content

Using this other endpoint you don't retrieve only a template's metadata but also its html content.

retrieve the template content

At this point you can save the template where it best suits you.

Using a premade solution

Sendgrid provides wrappers for their APIs for multiple languages: python, nodejs, java, etc.
Using the 2 above endpoints you can easily create a custom solution, but if you want something that it's already available to downloads and save your templates you can have a look at my package: backup-sendgrid-templates.

You can use it at command line to backup the files or inside your python code. You can store the downloaded files on the file system or on a S3 bucket.
If you want to store the templates in a different way you just need to extend the Strategy class and pass it to the main service.

Restoring a template

A backup is useless if you don't know what to do with it...
To restore a template, open the Sendgrid dashboard and create a new template.
In the design page, open the Build tab and expand the advanced section.

Click on Import Drag & Drop HTML and paste the html content retrieved from the backup.

Latest comments (0)