9

We have a site where our agents enter in some data, and then that data is sent to a client, via a SendGrid dynamic template.

The email content includes a lot of calculations based on the data entered, so we want our agents to have the ability to preview the email and verify the content first before sending it to the client.

Is there a way to use the SendGrid API to send a request with our json object, but instead of sending the email to the client, receive the generated email body so that we can display it to the agent and let them review it first?

daybreaker
  • 1,320
  • 1
  • 10
  • 17
  • I’m looking for something like this. Did you find a way to provide “data” and then substitute - or is that something that could be done with handlebars manually? – DauleDK Jul 22 '21 at 21:14
  • The answer that I pasted works, but only sends back the template with the handlebar templating in place. So, you'd need to grab it via the API, and do your own local handlebars stuff. I never actually went through and used this because of that. – daybreaker Jul 23 '21 at 00:21
  • Yeah ok - I'm currently experimenting with a cypress + ethereal mail combination to solve the same problem. Seems to work great - but feels a bit scary to send all those e-mails just for testing – DauleDK Jul 23 '21 at 17:31
  • Did anyone ever find a solution to retrieving html content render with send data of sendgrid template? – Hammad Aug 17 '22 at 09:22

2 Answers2

4

Answered my own question. API v3 has GET methods for Dynamic Transactional Templates and Template Versions.

API Call:

/templates/{template_id}/versions/{version_id}

using sendgrid-ruby:

sg = SendGrid::API.new(api_key: sendgrid_api_key)
sg.client.templates._(template_id).versions._(template_version_id).get

(Note: the template_version_id is the ID and not the Name of the template version)

The response body then includes a field called html_content which is the full rendered HTML of a dynamic template version with any handlebar templating.

daybreaker
  • 1,320
  • 1
  • 10
  • 17
  • Hi @daybreaker is html_content after substituting the variables if any in the template? I am looking for a way to get the html content after substituting the variables.. I want to pass the variables and their data and get the generated email content. Any idea how can I get it? – girish Dec 27 '21 at 11:22
  • @girish you just have to render the HTML with handlebars to see what it would look like with the variable replacements. Check out https://handlebarsjs.com/ – Matthew Purdon Feb 21 '22 at 16:56
1

You can make API call via postman as:

https://api.sendgrid.com/v3/templates/d-d44fdfsdfdsfd342342343

with Bearer token along with Sendgrid API key like:

Bearer SG.Fvsdfsdjfksdfsdfjsdkjfsdfksjdfsdfksjdfkjsdkfjsdf

The response is:

{
"id": "d-d55d081558a641b48a8a1145b4549fbe",
"name": "Bt_Payment_Reminder (Active)",
"generation": "dynamic",
"updated_at": "2021-12-21 07:35:12",
"versions": [
    {
        "id": "a95c3652-e49f-4608-a9dd-5aa4831c2dc3",
        "user_id": 11702857,
        "template_id": "d-d55d081558a641b48a8a1145b4549fbe",
        "active": 1,
        "name": "Bt_Payment_Reminder_Updated",
        "html_content": "Hello {{firstName}}",
        "plain_content": "Hello {{firstName}}",
        "generate_plain_content": true,
        "subject": "{{subject}}",
        "updated_at": "2021-12-21 07:37:48",
        "editor": "code",
        "test_data": "{\n    \"firstName\":\"Virendra\"}",
        "thumbnail_url": "sdasdasdasdasdasdsd"
    }
]

}