sendinblue / APIv3-nodejs-library

SendinBlue's API v3 Node.js Library
ISC License
102 stars 47 forks source link

sendTransacEmail 'Error: Forbidden' #100

Closed aryanm5 closed 3 years ago

aryanm5 commented 3 years ago

I'm trying to send a transactional email with my new SendInBlue account and I keep receiving an error that simply says "Forbidden". I followed the example on the docs to write this code:

const SibApiV3Sdk = require('sib-api-v3-sdk');

const defaultClient = SibApiV3Sdk.ApiClient.instance;

// Configure API key authorization: api-key
const apiKey = defaultClient.authentications['api-key'];
apiKey.apiKey = 'xxx';
// Uncomment the following line to set a prefix for the API key, e.g. 'Token' (defaults to null)
//apiKey.apiKeyPrefix = 'Token'

// Configure API key authorization: partner-key
//const partnerKey = defaultClient.authentications['partner-key'];
//partnerKey.apiKey = 'YOUR API KEY';
// Uncomment the following line to set a prefix for the API key, e.g. 'Token' (defaults to null)
//partnerKey.apiKeyPrefix['partner-key'] = 'Token'

const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();

const sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail(); // SendSmtpEmail | Values to send a transactional email

const sendEmail = async (to, subject, htmlBody, textBody) => {

    sendSmtpEmail.to = [{ email: to }];
    sendSmtpEmail.sender = { email: 'voluntime.aryanm@gmail.com', name: 'Voluntime' };
    sendSmtpEmail.htmlContent = `<html><body style='text-align:center'><strong>Voluntime</strong><br><br><p style='width:90%;padding-left:5%;padding-right:5%'>${htmlBody}</p><br><br>You can update your email preferences for <a href='https://voluntime.mittaldev.com'>Voluntime</a> in the Account page.<br><span style='opacity:0'>${Date.now()}</span></body></html>`;
    sendSmtpEmail.textContent = `Voluntime:\n\n${textBody}\n\nYou can update your email preferences for Voluntime in the Account page.\n\nTimestamp: ${Date.now()}`;
    sendSmtpEmail.subject = `Voluntime - ${subject}`;
    sendSmtpEmail.replyTo = { email: 'voluntime.aryanm@gmail.com' };

    const result = await apiInstance.sendTransacEmail(sendSmtpEmail);
    console.log(result);
    return result;
};

export { sendEmail };

The apiInstance.sendTransacEmail(sendSmtpEmail) line throws a 403 error that simply says "Forbidden". How do I fix this?

aryanm5 commented 3 years ago

I have found out that I need to submit a request to SendInBlue to authorize my account to send transaction emails.