Example in README not working?
Hi, I tried setting my API Key with version 2.2.0 (as stated in the example):
const brevo = require('@getbrevo/brevo'); let apiInstance = new brevo.TransactionalEmailsApi();
let apiKey = apiInstance.authentications['apiKey'];
However "authentications" seems to be protected and not usable as stated in the example:
error TS2445: Property 'authentications' is protected and only accessible within class 'TransactionalEmailsApi' and its subclasses.
I tried downgrading to 2.1.1 which is the same version as in the example, but it still fails.
How to set the API Key with this package? Is it project even still maintained? I raised an issue over a month ago and no one looked into it yet...
Hi, you can try :
const brevo = require('@getbrevo/brevo');
let apiInstance = new brevo.TransactionalEmailsApi();
apiInstance.setApiKey(
TransactionalEmailsApiApiKeys.apiKey,
"YOUR_KEY"
);
I plus one this, had a hard time figuring this out and should be in the docs.
For anyone looking to test Brevo and struggling with the doc, you can see the following example that perfectly run in Deno:
import * as brevo from 'npm:@getbrevo/brevo'
const API_KEY = 'MY API KEY'
async function sendEmail() {
try {
const apiInstance = new brevo.TransactionalEmailsApi()
// Set the API key for transactional emails
apiInstance.setApiKey(brevo.TransactionalEmailsApiApiKeys.apiKey, API_KEY)
const sendSmtpEmail = new brevo.SendSmtpEmail()
sendSmtpEmail.subject = 'Hello from Brevo'
sendSmtpEmail.htmlContent =
'<html><body><h1>Hello :)</h1></body></html>'
sendSmtpEmail.sender = {
name: 'Support',
email: '[email protected]'
}
sendSmtpEmail.to = [
{
email: '[email protected]',
name: 'Gabriel'
}
]
const result = await apiInstance.sendTransacEmail(sendSmtpEmail)
console.log('Email sent successfully:', result)
} catch (error) {
console.error('Error sending email:', error)
}
}
await sendEmail()
For those struggling... you can use any http client instead their api
eg.
import axios, { AxiosInstance } from 'axios';
export default class BrevoEmailService {
private axiosInstance: AxiosInstance;
constructor() {
const API_KEY = process.env.BREVO_API_KEY || '';
this.axiosInstance = axios.create({
baseURL: 'https://api.brevo.com/v3/smtp/email/',
headers: { 'api-key': API_KEY, 'Content-Type': 'application/json' },
})
}
async sendEmail(to: string, subject: string, htmlContent: string) {
try {
const result = await this.axiosInstance.post('', {
sender: { name: 'Name', email: '[email protected]' },
to: [{ email: '[email protected]', name: 'name' }],
htmlContent: '<html><body><h1>Hello :)</h1></body></html>',
subject: 'Hello from Brevo',
});
console.log('Email sent successfully:', result.status);
} catch (error) {
console.log('Failed to send emailI');
}
}
}