sendgrid-nodejs: substitutions don't work in transactional templates

Issue Summary
Substitutions don’t work when using this library. It works when using curl.
Steps to Reproduce
- Create a transactional email template in your SendGrid account
- Have either <%body%> in the template, %firstName% or both (I tried both)
- Execute this node code (fill in the config)
`'use strict';
var config = {
apiKey: '',
from: '',
to: '',
templateId: ''
}
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(config.apiKey);
async function sendTest() {
var newMsg = {
"from": {
"name": "Test Script",
"email": config.from
},
"personalizations": [{
"to": config.to,
"substitutions": {
"-firstName-": "Maciejs",
"%firstName%": "Maciej",
"-first_name": "Maciej",
"%first_name%": "Maciej"
}
}],
"subject": "test subject",
"content": [{
"type": "text/plain",
"value": "Hello, %firstName%!"
},
{
"type": "text/html",
"value": "<html><p>Hello, %firstName%!</html> "
}],
"template_id": config.templateId
}
await sgMail.send(newMsg);
}
sendTest();`
- Notice that nothing will be replaced in the delivered email. Again, it works perfectly with curl.
I’m also attaching a screenshot of the template in SendGrid editor and the email I get

Technical details:
- sendgrid-nodejs Version: 6.1.4
- Node.js Version: 8.9.2
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Reactions: 2
- Comments: 20 (7 by maintainers)
To everyone as desperate as me yesterday: in v3 API you need to use
dynamic_template_datainstead ofsubstitutions, this isn’t yet documented anywhere!Then use
{{var_name}}in email template anddynamic_template_data: { var_name: "var_value" }in API call.@MaciejKrawczyk Have you tried using
setSubstitutionWrapperand omitting the wrappers from thesubstitutions?Ex:
sgMail.setSubstitutionWrappers('-', '-');"substitutions": { "firstName": "Maciejs" }Not sure how you’d do multiple wrappers at once this way, but I was able to get subs working with this version by not specifying the wrappers in the substitution attribute. My subs look like
emailOptions.substitutions = {}emailOptions.substitutions.CLIENT_NAME = myClientName@tkafka Thanks a lot! This is so frustrating! Took me 3hrs of my day…
Here is the entire code in case somebody is looking for the same:
The documentation of sendgrid is horrible to say the least.
This should be definitely put in the README! Cost me 2hrs to figure this out!
It works. Thank you!
Hah,
dynamic_template_data. I solved your little puzzle! 😄For other typescript users - Create file
mail.d.ts:@tkafka oh my god! thanks for this man, been pulling my hair out on this for the past 2 days.
Not yet. I will update if it works or not soon. Sorry for the late reply.
Thanks @MaciejKrawczyk,
This is very good feedback and I’ve added this to our team’s backlog for a deeper dive. We will provide updates on this thread.
Thanks!
With Best Regards,
Elmer
This should definitely be on the readme. I tried everything, I read all docs on the SendGrid api reference , everything that’s on GitHub (I thought), contacted support and I would be making my own api wrapper if not for @kmcurry I think it would be much more easier and less confusing if the helper library would work exactly like the API itself (see my code at the beginning of the thread).
@thinkingserious Yes. This doc did help a lot. However, since I came from the official website, where the substitution keys should include the wrapper, I didn’t pay too much attention to the use cases.