Sendgrid-nodejs: How to do substitution in a template with node.js

Created on 30 Nov 2016  ·  13Comments  ·  Source: sendgrid/sendgrid-nodejs

Issue Summary

I am using API 3 for integrating sendgrid with my node.js app. I got the basic email working but I need to substitute certain things in the template with data. Below is the code I am using to send email, the email goes out but the substitutions do not work.

var sg = require('sendgrid')('API-KEY');

    var request = sg.emptyRequest();
    request.body = {
        "from": {
            "email": "[email protected]",
            "name": "Admin"
        },
        "personalizations" : [
            {
                "to": [
                    {
                        "email": recepientEmail,
                        "name": "User"
                    }
                ]
            }
        ],
        "subject": "This is subject",
        "template_id": "template-id-here",
        "substitution": {
            "-sub1-": ["This is the new substitured text"],
            "-sub2-": ["Substitured text"]
        }
    };
    request.method = 'POST';
    request.path = '/v3/mail/send';

    sg.API(request, function(error, response) {
        console.log(response.statusCode)
        console.log(response.body)
        console.log(response.headers)
    });

Technical details:

  • sendgrid-nodejs Version: master (latest commit: [commit number])
  • Node.js Version: 4.1.2
question

Most helpful comment

Just an update that the above url mentioned by @thinkingserious slightly changed: https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-templates.md

All 13 comments

Hi @neeraj87,

I hope these examples will help you: https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-templates.md

Please let me know if you run into any issues with those. Thanks!

Hi @neeraj87,

Simply, your JSON structure is not true.

Substitution must be inner the personalizations array. In this way, you can define different substitutions for each person.

Also the name(key) of a substitution array must be substitutions and values of a substitution must be a string. Not an array.

"substitutions": { "-sub1-": "This is the new substitured text", "-sub2-": "Substitured text" }

@thinkingserious thank you for the link to the documentation, it worked. I also noticed I was missing the plural "s" in substitutions.

@alperenozlu thanks for the answer, fixed the JSON structure and the key value pair.

@thinkingserious one quick questions, I am trying to substitute the tags using variables but it keeps giving me : errors: [ { message: 'Bad Request', field: null, help: null } ]

var request = sg.emptyRequest();
    request.body = {
        "from": {
            "email": "[email protected]",
            "name": "Admin"
        },
        "personalizations" : [
            {
                "to": [
                    {
                        "email": recepientEmail,
                        "name": userNameVariable
                    }
                ],
                "substitution": {
                    "-sub1-": variableB,
                    "-sub2-": variableA
                }
            }
        ],
        "subject": "This is subject",
        "template_id": "template-id-here"
    };
    request.method = 'POST';
    request.path = '/v3/mail/send';

    sg.API(request, function(error, response) {
        console.log(response.statusCode)
        console.log(response.body)
        console.log(response.headers)
    });

This is the error I get

400
{ errors: [ { message: 'Bad Request', field: null, help: null } ] }
{ server: 'nginx',
date: 'Thu, 01 Dec 2016 07:08:51 GMT',
'content-type': 'application/json',
'content-length': '63',
connection: 'keep-alive' }

Hi @neeraj87,

Please check type of your variables, their type must be string. Also please change the key of the substitution to substitutions.

@alperenozlu you are right, one of the substitution was an integer. converted it to string and it works.

Thanks for the help.

Hello guys!
How can I do a substitution with an array of values, instead of just a string?

@Deniscapp you can read the array elements, concatenate them to form a string.

@neeraj87 that's what I did! But I was just wondering if they had added this feature somehow.
Thanks for the answer! :)

@Deniscapp oops, didn't know that. I think it was in the older version. Maybe someone "official" from SendGrid can answer this.

@Deniscapp, @neeraj87

From the API docs:

A collection of key/value pairs following the pattern "substitution_tag":"value to substitute". All are assumed to be strings. These substitutions will apply to the text and html content of the body of your email, in addition to the subject and reply-to parameters.

Could be useful to have the library allow for arrays and then we concatenate them to form a string behind the scenes.

Just an update that the above url mentioned by @thinkingserious slightly changed: https://github.com/sendgrid/sendgrid-nodejs/blob/master/use-cases/transactional-templates.md

Thanks @vaskort!

I've updated the link in my original comment.

Was this page helpful?
0 / 5 - 0 ratings