Znote (recipes)
  Get Znote  

Brevo Email Integration (Node.js)

Quickly integrate Brevo to create contacts and send transactional emails

 

Brevo Email Integration (Node.js)

Easily send transactional emails or manage contacts with Brevo's official Node.js SDK.


🔧 Prerequisites

Install the SDK

npm install -S sib-api-v3-sdk

Generate an API Key Create your key from your Brevo dashboard: 👉 https://app.brevo.com/settings/keys/api

Store your credentials securely Create a brevo.json file with your API key:

{ "key": "YOUR_API_KEY" }

If using Znote's block loader, you can persist this file with:

const credentials = loadBlock('brevo');
_fs.writeFileSync(`${__dirname}/brevo.json`, credentials);

👤 Create a Contact

You can trigger automated workflows when new contacts are added to a list. 💡 To find your list IDs, visit: https://my.brevo.com/lists Set up automation rules here: https://app.brevo.com/automation/automations

const credentials = require('./brevo.json');
const SibApiV3Sdk = require('sib-api-v3-sdk');

const client = SibApiV3Sdk.ApiClient.instance;
client.authentications['api-key'].apiKey = credentials.key;

const api = new SibApiV3Sdk.ContactsApi();

const contact = new SibApiV3Sdk.CreateContact();
contact.email = 'example@example.com';
contact.listIds = [2]; // Replace with your actual list ID(s)

api.createContact(contact).then(data => {
  console.log('Contact created:', JSON.stringify(data));
}).catch(error => {
  console.error('Error creating contact:', error);
});

✉️ Send a Transactional Email

Send custom emails with dynamic content, or use pre-defined templates with templateId.

const credentials = require('./brevo.json');
const SibApiV3Sdk = require('sib-api-v3-sdk');

const client = SibApiV3Sdk.ApiClient.instance;
client.authentications['api-key'].apiKey = credentials.key;

const api = new SibApiV3Sdk.TransactionalEmailsApi();

const email = new SibApiV3Sdk.SendSmtpEmail({
  subject: "My {{params.subject}}",
  htmlContent: "<html><body><h1>This is my first transactional email: {{params.parameter}}</h1></body></html>",
  sender: { name: "John Doe", email: "example@example.com" },
  to: [{ email: "recipient@example.com", name: "Jane Doe" }],
  replyTo: { email: "replyto@znote.io", name: "John Doe" },
  params: {
    parameter: "My param value",
    subject: "Dynamic Subject"
  }
  // You can also use a predefined template:
  // templateId: 1
});

api.sendTransacEmail(email).then(data => {
  console.log('Email sent:', JSON.stringify(data));
}).catch(error => {
  console.error('Error sending email:', error);
});

Related recipes