<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sergio Morstabilini</title>
    <description>The latest articles on DEV Community by Sergio Morstabilini (@s_mosta).</description>
    <link>https://dev.to/s_mosta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F125556%2F744e1e21-0058-438c-90eb-5c1afca6f879.jpg</url>
      <title>DEV Community: Sergio Morstabilini</title>
      <link>https://dev.to/s_mosta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/s_mosta"/>
    <language>en</language>
    <item>
      <title>Backup Sendgrid templates</title>
      <dc:creator>Sergio Morstabilini</dc:creator>
      <pubDate>Fri, 02 Sep 2022 13:52:21 +0000</pubDate>
      <link>https://dev.to/s_mosta/backup-sendgrid-templates-27p2</link>
      <guid>https://dev.to/s_mosta/backup-sendgrid-templates-27p2</guid>
      <description>&lt;p&gt;Sendgrid is one of the most used email delivery service. Among other things, Sendgrid allows you to design your &lt;a href="https://sendgrid.com/solutions/email-api/dynamic-email-templates/"&gt;email templates&lt;/a&gt; using &lt;a href="https://docs.sendgrid.com/for-developers/sending-email/using-handlebars"&gt;handlebars tags&lt;/a&gt; to dynamically change the content of your messages given the payload you provide.&lt;/p&gt;

&lt;h2&gt;
  
  
  The WISIWYG editor
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--FmXkD_84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf6zgey4b32kqwvz4msq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--FmXkD_84--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf6zgey4b32kqwvz4msq.jpg" alt="the editor" width="880" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Drawbacks
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;code&gt;{&lt;/code&gt; 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.&lt;br&gt;
What did you say? Has this ever happened to me? Nooo, I'm too careful to accidentally overwrite my templates! 😆 😉&lt;/p&gt;

&lt;h2&gt;
  
  
  Backupping the templates
&lt;/h2&gt;

&lt;p&gt;With the Sendgrid APIs it's easy to retrieve your precious files.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting the list of templates
&lt;/h3&gt;

&lt;p&gt;First you need to retrieve the list of existing templates calling &lt;a href="https://docs.sendgrid.com/api-reference/transactional-templates/retrieve-paged-transactional-templates"&gt;this endpoint&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DEcxo2xV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7tvvwipqdbytuvkz8ad.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DEcxo2xV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n7tvvwipqdbytuvkz8ad.png" alt="template list" width="880" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just remember that you need an API key with full access.&lt;br&gt;
You can also specify the &lt;code&gt;page_size&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;When you have the list of existing templates you loop over them and retrieve their HTML code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Retrieving the template content
&lt;/h3&gt;

&lt;p&gt;Using this &lt;a href="https://docs.sendgrid.com/api-reference/transactional-templates-versions/retrieve-a-specific-transactional-template-version"&gt;other endpoint&lt;/a&gt; you don't retrieve only a template's metadata but also its html content.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zjNvanML--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3ox4fftpjhiluadoxjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zjNvanML--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3ox4fftpjhiluadoxjs.png" alt="retrieve the template content" width="880" height="508"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point you can save the template where it best suits you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Using a premade solution
&lt;/h3&gt;

&lt;p&gt;Sendgrid provides wrappers for their APIs for multiple languages: python, nodejs, java, etc. &lt;br&gt;
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: &lt;a href="https://github.com/backupSendgridTemplates/backupSendgridTemplates"&gt;backup-sendgrid-templates&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
If you want to store the templates in a different way you just need to extend the &lt;code&gt;Strategy&lt;/code&gt; class and pass it to the main service.&lt;/p&gt;

&lt;h2&gt;
  
  
  Restoring a template
&lt;/h2&gt;

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

&lt;p&gt;Click on &lt;code&gt;Import Drag &amp;amp; Drop HTML&lt;/code&gt; and paste the html content retrieved from the backup.&lt;/p&gt;

</description>
      <category>sendgrid</category>
      <category>python</category>
      <category>backup</category>
    </item>
  </channel>
</rss>
