Search code examples
sendgridsendgrid-api-v3sendgrid-templates

Is there a way to retrieve the generated HTML email body of a dynamic template via SendGrid API call?


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?


Solution

  • 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.