sendgrid-nodejs: Transactional Email Doesn't Work

Hi, I created a “transactional email” in the sendgrid dashboard. I am using the exact code snippet here but with my template id and my from email address:

 sgMail.setApiKey(process.env.SENDGRID_KEY);
    const msg = {
      to: 'myToAddress@website.com',
      from: 'myFromAddress@website.com',
      templateId: 'd-f43daeeaef504760851f727007e0b5d0',
      dynamic_template_data: {
        subject: 'Testing Templates',
        name: 'Some One',
        city: 'Denver',
      },
    };
    await sgMail.send(msg);

When I completely remove the template reference in my msg object and just send some text I receive the email every time:

const msg = {
      to: 'myToAddress@website.com',
      from: 'myFromAddress@website.com',
      subject: 'Testing Templates',
      html: 'is sendgrid broken?'
    };

Is sendgrid transactional emails broken? Why is this not working for me?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (7 by maintainers)

Most upvoted comments

Hello all,

I’ve tried reproducing the bug locally, with package versions 7.0.1 and 7.2.1 with no success. Here is my code snippet using templates that has been successfully sending emails:

sg = require('@sendgrid/mail');

sg.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'you@example.com',
  from: 'me@authorized-domain.com',
  templateId: 'd-baddb3551a76483598ece251aa7fae6a', // Make sure you're using your own template ID!
  dynamic_template_data: {
    subject: 'Testing Templates',
    name: 'Someone',
    city: 'Denver',
  },
};
sg.send(msg).then((resp) => {
  console.log(resp);
}).catch(err => {
  console.log(err.response.body);
});

@JimLynchCodes The one thing I noticed is that templateId in your original code snippet exactly matches the template ID in the example code. Please double check that you have substituted its value with your own template ID. As for why it’s not reading your API key correctly, it’s hard to tell without more information. Maybe try using an environment variable?

@msacket I noticed that you’re not quite handling errors properly, which may be why you’re not seeing errors. You’ll want to look at error.response.body to get the proper API error response. For more information, please take a look at the error handling docs I linked in my last comment. As an aside, the following msg object formats also worked successfully for me:

const msg = {
  personalizations: [{
    to: 'you@example.com',
    dynamic_template_data: {
      subject: 'Testing Templates',
      name: 'Someone',
      city: 'Denver',
    },
  }],
  from: 'me@authorized-domain.com',
  templateId: 'd-baddb3551a76483598ece251aa7fae6a',
};

const msg = {
  personalizations: [{
    to: [{
      email: 'you@example.com',
    }],
    dynamic_template_data: {
      subject: 'Testing Templates',
      name: 'Someone',
      city: 'Denver',
    },
  }],
  from: 'me@authorized-domain.com',
  templateId: 'd-baddb3551a76483598ece251aa7fae6a',  // also works with the key being `template_id`
};

As a final note, the Legacy Transactional Templates have been deprecated for Dynamic Templates. If your account was created recently, your API key will only give you access to Dynamic Templates.