Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created May 8, 2024 15:52
Show Gist options
  • Save cosimo/84bcc6e466e97e38558b5a2379efe032 to your computer and use it in GitHub Desktop.
Save cosimo/84bcc6e466e97e38558b5a2379efe032 to your computer and use it in GitHub Desktop.
Migrate Sendgrid templates between different accounts
"""
Migrates SendGrid templates from one account to another.
Requires a `.env` file with the following variables:
- SOURCE_SENDGRID_API_KEY
- DESTINATION_SENDGRID_API_KEY
There is not much error handling here. This is not a place of honor.
It turned out to run quite smoothly still.
"""
import dotenv
import json
import os
import sendgrid
dotenv.load_dotenv()
src_sg = sendgrid.SendGridAPIClient(os.environ.get("SOURCE_SENDGRID_API_KEY"))
dst_sg = sendgrid.SendGridAPIClient(os.environ.get("DESTINATION_SENDGRID_API_KEY"))
#
# Retrieve all "transactional" emails from the source account
#
response = src_sg.client.templates.get()
json_str = response.body.decode("utf-8")
templates = json.loads(json_str)
templates = templates["templates"]
for template in templates:
"""
{'generation': 'legacy',
'id': 'c1ebf23f-935d-4925-89bd-340d6176fa33',
'name': 'Template name here',
'updated_at': '2019-01-29 10:17:34',
'versions': [{'active': 1,
'editor': 'code',
'generate_plain_content': True,
'id': '9f600f14-2e35-4b04-8497-21330586cda5',
'name': 'Untitled_11548757058978',
'subject': 'Subject of your email',
'template_id': 'c1ebf23f-935d-4925-89bd-340d6176fa33',
'updated_at': '2019-01-29 10:18:19'}]}
"""
template_id = template["id"]
new_template = {
"id": template_id,
"generation": template["generation"],
"name": template["name"],
}
# Create the new template in the destination account
response = dst_sg.client.templates.post(request_body=new_template)
"""
b'{"id":"98cc9f3c-c6a5-4c59-9848-8852d310a3a1",
"name":"Template name here",
"generation":"legacy",
"updated_at":"2024-05-08 12:40:05",
"versions":[]}\n'
"""
json_str = response.body.decode('utf-8')
new_template_object = json.loads(json_str)
new_template_id = new_template_object["id"]
"""
{'active': 1,
'editor': 'code',
'generate_plain_content': True,
'html_content': 'Hi *|template_variable|*,\n ...'
'id': '9f600f14-2e35-4b04-8497-21330586cda5',
'name': 'Untitled_11548757058978',
'plain_content': 'Hi *|template_variable|*,\n ... ',
'subject': 'Subject of your email',
'template_id': 'c1ebf23f-935d-4925-89bd-340d6176fa33',
'updated_at': '2019-01-29 10:18:19'
}
"""
for version in template["versions"]:
version_id = version["id"]
response = src_sg.client.templates._(template_id).versions._(version_id).get()
json_str = response.body.decode('utf-8')
version_object = json.loads(json_str)
response = dst_sg.client.templates._(new_template_id).versions.post(
request_body={
"active": version["active"],
"generate_plain_content": version_object["generate_plain_content"],
"html_content": version_object["html_content"],
"name": version["name"],
"plain_content": version_object["plain_content"],
"subject": version["subject"],
"template_id": new_template_id,
"updated_at": version["updated_at"],
}
)
# Print the new template name and ID, in case there's some
# configuration to be updated somewhere
print(template["name"] + "\t" + new_template_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment