Search code examples
sendgridsendgrid-api-v3sendgrid-templates

How to send templated email via SendGrid API v3?


In their docs SendGrid describes a structure to be sent to API:

{ "to": // list of emails
  "sub": // list of substitutions
  "filters": // template ID here
}

But how exactly should it be used together with the mail send structure?

{ "personalizations": 'to' and 'subject' here
  "from": ...
  "content": ... }

Also the template doc says:

If you are using the Web API v3 mail send endpoint, you can specify which transactional template you would like to use simply by setting the template ID in the template_id parameter of your JSON payload.

"Simply". Ok. But how my substitutuins gonna be specified then?


Solution

  • Well, below is a working example. And that differs from the v3 docs.

    {
      "personalizations" : [ {
        "substitutions" : {
          ":name" : "John"
        },
        "subject" : "Hello from Java",
        "to" : [ {
          "email" : "[email protected]"
        } ]
      } ],
      "from" : {
        "email" : "[email protected]"
      },
      "template_id" : "11111111-1111-1111-1111-111111111111"
    }