Reputation: 17455
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
Upvotes: 1
Views: 3767
Reputation: 4416
I was getting the same error. I had failed to copy the Template ID correctly. The Template ID is displayed when you click in the sidebar of the SendGrid control panel, "Email API ➔ Dynamic Templates". It is not the same as the ID you get from the /v3/designs
endpoint, alas.
The error message is ambiguous. It says "Cannot use dynamic template data with a legacy template ID"
, when in fact it should say "Template ID not found"
. I was misled into believing that my Template was a legacy version, which it was not. Four hours I'll never get back.
Upvotes: 3
Reputation: 73027
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.
Upvotes: 2