How to send an email from SendinBlue with Node.js

August 7, 2021

For a project I'm working on, I wanted to be able to send emails to notify my users of events and updates.

To begin with, I just wanted to send a simple email to myself to get everything working for now and learn how to use the API. I'm posting this as their current documentation isn't very up-to-date at the time of posting. I'm using SendinBlue as I already use it behind the scenes for my website's emails.

What is a Transactional Email?

Let's get this out of the way, there are two types of email with SendinBlue: Campain and Transactional. Campaigns are just as they seem, used to send mass emails to all of your subscribers. This is used for promotions among other things. Transactional emails are more direct, usually used for sending password reset, verification and one-off notification emails.

The Setup

Setup with Node.js is pretty simple, in my project I have created a file for handling emails and, soon, it'll handle text messaging too. Feel free to have a look at their v3 API over on GitHub. There's a lot of information there with plenty of functions for everything.

To install the package, just open your working directory in a terminal and type:

sh

1npm install sib-api-v3-sdk --save

Then open the file you want to handle emails, mine's simply called systemMailer.js and require the SDK along with adding the needed configuration. I won't go into detail as it's pretty self-explanatory.

js

1var SibApiV3Sdk = require("sib-api-v3-sdk");
2var defaultClient = SibApiV3Sdk.ApiClient.instance;
3
4// Configure API key authorization: api-key
5var apiKey = defaultClient.authentications["api-key"];
6apiKey.apiKey = "API_KEY_HERE";

The Implementation

For this next part, I'll include a simple function I use. It's pretty simple and mostly copied from the official docs, just with a few tweaks. These tweaks include adding the sender email address, subject and textContent and removing templateId and params. These are for sending from a template on the SendinBlue website. While I will make use of this later down the line, I don't need it now. Simple plain-text emails will do.

js

1function SendTestEmail(address) {
2  var apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();
3  var sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail();
4
5  sendSmtpEmail = {
6    sender: { email: "sender@email.com" },
7    to: [
8      {
9        email: "person@email.com",
10        name: "Person Name",
11      },
12    ],
13    subject: "Test Email",
14    textContent: "Test Email Content",
15  };
16
17  apiInstance.sendTransacEmail(sendSmtpEmail).then(
18    function (data) {
19      console.log("API called successfully. Returned data: " + data);
20    },
21    function (error) {
22      console.error(error);
23    }
24  );
25}

This creates the API Instance and SMTP objects required to send the email. Below the creation of these objects, we're using sendSmtpEmail to also set different variables that are required to send the email. Make sure the sender address matches one in your SendinBlue account or you may face errors. After all of these, we're using the API Instance and calling sendTransacEmail(), which sends the email. If there's a problem, you'll be notified in the console or check your SendinBlue dashboard for logs.


If you're new here you may not know I do quite a few things with software from game development to app development. The reason for me using this is for an app I'm currently working on. This Node.js project is for its backend API.

Hopefully, this helped you. If it did, consider buying me a coffee on Ko-Fi at https://ko-fi.com/rhyce.

Did this post help you?

Consider buying me a slice of pizza!