I'm attempting to integrate with Sendgrid and am having a heck of a time. I have created a dynamic template - a new one, not a legacy one - with a single handle bar (first_name) in it. It has a subject. But I'm getting a load of errors that I could use some help with.
First the code:
public void sendEmail(String toEmail, String toName) throws IOException {
String fromEmail = "[email protected]";
String fromName = "Blah blah";
SendGrid sendGrid = new SendGrid("the_api_key");
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
String body = "see below...";
request.setBody(body);
Response response = sendGrid.api(request);
System.out.println(response.getStatusCode());
System.out.println(response.getBody());
System.out.println(response.getHeaders());
} catch (IOException ex) {
throw ex;
}
}
Taken almost entirely from the Java example code.
The JSON body, pretty printed...
{
"from": {
"email": "[email protected]",
"name": "Blah blah"
},
"personalizations": [
{
"to": [
{
"email": "[email protected]",
"name": "Blah Blah"
}
],
"dynamic_template_data": {
"first_name": "Babaloo"
}
}
],
"template_id": "[d-lotsandlotsofcharacters]"
}
And then a bunch of errors that make no sense (all of which link to a 404):
I'm not using a legacy template id according to the UI
I sent what I was given on the UI
.My template has a subject
A valid template_id was provided
I'm guessing the first issue is the template_id
field. It's strange JSON in that the value includes the array open/close. Putting the value inside as text gives a parse error so Sendgrid must be taking that directly.
Any directional help would be most appreciated. The docs are rather challenging
Twilio SendGrid developer evangelist here.
In your example, you show the template_id
as "template_id": "[d-lotsandlotsofcharacters]"
. The square brackets are not required in the template_id
, it should just be "template_id": "d-lotsandlotsofcharacters"
.
The documentation for sending an email with a dynamic template does show the the template_id
example as "template_id":"[template_id]"
but the entire [template_id]
string should be substituted for the real ID.