Below is my implementation of Bulk Send and Customise Transactional Messages in Node.js
const brevo = require("@getbrevo/brevo");
let defaultClient = brevo.ApiClient.instance;
let apiKey = defaultClient.authentications["apiKey"];
apiKey.apiKey =
"xkeysib-xx-xxx-xxx-xxxxx-xxx";
let apiInstance = new brevo.TransactionalEmailsApi();
let sendSmtpEmail = new brevo.SendSmtpEmail();
sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.htmlContent =
"<html><body><h1>Common: This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = { name: "John", email: "[email protected]" };
sendSmtpEmail.to = [{ email: "[email protected]", name: "John" }];
sendSmtpEmail.replyTo = { email: "[email protected]", name: "John" };
sendSmtpEmail.headers = { "Some-Custom-Name": "unique-id-1234" };
sendSmtpEmail.params = {
parameter: "My param value",
subject: "common subject",
};
sendSmtpEmail.messageVersions = [
{
to: [
{
email: "[email protected]",
name: "John",
},
],
headers: {
"Message-Id": "<[email protected]>",
},
params: {
greeting: "Welcome onboard!",
headline: "Be Ready for Takeoff.",
},
subject: "+001",
htmlContent: "<html><body><h1>+001 content</h1></body></html>",
},
{
to: [
{
email: "[email protected]",
name: "Steve",
},
],
params: {
greeting: "Greeting 1.",
headline: "Some bathing suits you might like",
},
subject: "+002",
},
];
apiInstance.sendTransacEmail(sendSmtpEmail).then(
function (data) {
console.log(
"API called successfully. Returned data: " + JSON.stringify(data)
);
},
function (error) {
console.error(error);
}
);
Even though it has been mentioned in the Brevo Docshttps://developers.brevo.com/reference/sendtransacemail
I am getting the error given below.
let defaultClient = brevo.ApiClient.instance;
^
TypeError: Cannot read properties of undefined (reading ‘instance’)
at Object. (C:UsersHARIHARAN REDDYDesktopNew folderindex.js:2:37)
at Module._compile (node:internal/modules/cjs/loader:1376:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
at Module.load (node:internal/modules/cjs/loader:1207:32)
at Module._load (node:internal/modules/cjs/loader:1023:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:135:12)
at node:internal/main/run_main_module:28:49
Node.js v20.10.0