Backup Sendgrid templates
Tags: sendgrid, python, backup
Published: 2022-09-02 13:52:21

---
title: Backup Sendgrid templates
published: true
description: How to backup the templates created in the Sendgrid webapp.
tags: #sendgrid #python #backup
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usngyfuoar0gjsnc8xt6.jpg
# Use a ratio of 100:42 for best results.
---

Sendgrid is one of the most used email delivery service. Among other things, Sendgrid allows you to design your [email templates](https://sendgrid.com/solutions/email-api/dynamic-email-templates/) using [handlebars tags](https://docs.sendgrid.com/for-developers/sending-email/using-handlebars) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf6zgey4b32kqwvz4msq.jpg)

### 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! :laughing: :wink:

## 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](https://docs.sendgrid.com/api-reference/transactional-templates/retrieve-paged-transactional-templates).

![template list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7tvvwipqdbytuvkz8ad.png)

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](https://docs.sendgrid.com/api-reference/transactional-templates-versions/retrieve-a-specific-transactional-template-version) you don't retrieve only a template's metadata but also its html content.

![retrieve the template content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3ox4fftpjhiluadoxjs.png)

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](https://github.com/backupSendgridTemplates/backupSendgridTemplates).

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.