sendinblue / APIv3-nodejs-library

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

Cannot read properties of undefined (reading 'body') #133

Closed s4malve closed 2 years ago

s4malve commented 2 years ago

I was trying to send a mail from an astro endpoint and when i made the request the server send me Cannot read properties of undefined (reading 'body') but still sending the email.

I followed the Send transactional email api and i dont know what i'm doing wrong

try {
     const apiInstance = new SibApiV3Sdk.TransactionalEmailsApi()

    apiInstance.setApiKey(
      SibApiV3Sdk.TransactionalEmailsApiApiKeys.apiKey,
      import.meta.env.SENDINBLUE_API_KEY
    )

    const emailInstance = new SibApiV3Sdk.SendSmtpEmail()
    emailInstance.subject = `message send`
    emailInstance.htmlContent = `<html><body><p>${message}</p></body></html>`
    emailInstance.sender = { name, email }
    emailInstance.to = [{ email, name }]
    apiInstance.sendTransacEmail(emailInstance).then(
      (data) => {
        // console.log(
        //   `API called successfully. Returned data: ${JSON.stringify(data)}`
        // )
        return new Response('Message send succesfully', { status: 200 })
      },
      (error) => {
        // console.error(error)
        return new Response(JSON.stringify(error), { status: 400 })
      }
    )
  } catch (error) {
    console.error(error)
  }
shubhamUpadhyayInBlue commented 2 years ago

Hi @s4malve

Please try the code below:

let defaultClient = SibApiV3Sdk.ApiClient.instance;

let apiKey = defaultClient.authentications['api-key'];

apiKey.apiKey = `YOUR_API_KEY`;

let apiInstance = new SibApiV3Sdk.TransactionalEmailsApi();

let sendSmtpEmail = new SibApiV3Sdk.SendSmtpEmail();

sendSmtpEmail.subject = "My {{params.subject}}";
sendSmtpEmail.htmlContent = "<html><body><h1>Hey! \n This is my first transactional email {{params.parameter}}</h1></body></html>";
sendSmtpEmail.sender = {"name":"John Doe","email":"example@example.com"};
sendSmtpEmail.to = [{"email":"shubham.upadhyay@sendinblue.com","name":"Jane Doe"}];
sendSmtpEmail.replyTo = {"email":"replyto@domain.com","name":"John Doe"};
sendSmtpEmail.headers = {"Some-Custom-Name":"unique-id-1234"};
sendSmtpEmail.params = {"parameter":"My param value","subject":"New Subject"};

apiInstance.sendTransacEmail(sendSmtpEmail).then(function(data) {
  console.log('API called successfully. Returned data: ' + JSON.stringify(data));
}, function(error) {
  console.error(error);
});

Reference for the endpoint: https://developers.sendinblue.com/reference/sendtransacemail

Happy to help!

shubhamUpadhyayInBlue commented 2 years ago

Please don't forget to add your account YOUR_API_KEY in the placeholder in the script above.

s4malve commented 2 years ago

thanks