I cannot send transactional email through SendInBlue API

I checked the json and i connected to sendinblue helpdesk, but i can’t to compose a valid json. I think this is problem not in my code, i think this problem in your configurating of json or interface of fetch. In console i get a following message:

{"error":{"status":400,"message":"Input must be a valid JSON object","code":"bad_request"}}

I have following js module:

import {fetch} from 'wix-fetch';  
export function sendWithService(key, senderMail, recipient, subject, body) {
  const url = 'https://api.sendinblue.com/v3/smtp/email';
  const headers = {
    "content-type": "application/json",
    "api-key": key
  };
  const sender = {
  	"email" : senderMail
  };
  const to= [{
  	"email" : recipient,
  	"name" : "Product Advice"
  }];
  const replyTo = {
  	"email" : senderMail,
  	"name" : "Product Advice"
  };
 
 const data = {
 	"sender" : sender,
 	"to" : to,
 	"htmlContent": body,
 	"subject": subject,
 	"replyTo" : replyTo,
 };
  const request = {
    "method": "POST", 
    "headers": headers,
    "body": data
  };
console.log(request);
  return fetch(url, request)
   .then(response => response.json());
}

Referense information about API of transactional email can be found in Getting started
Please, help me understand this.

Correctly code, which work good:

import {fetch} from 'wix-fetch'; 

export function sendWithService(key, senderMail, recipient, subject, body) {
  const url = 'https://api.sendinblue.com/v3/smtp/email';
  
  const headers = {
    "content-type": "application/json",
    "api-key": key
  };

  const sender = {
  	"email" : senderMail
  };
  
  const to= [{
  	"email" : recipient,
  	"name" : "Product Advice"
  }];
  
  const replyTo = {
  	"email" : senderMail,
  	"name" : "Product Advice"
  };
 
 const data = {
 	"sender" : sender,
 	"to" : to,
 	"htmlContent": body,
 	"subject": subject,
 	"replyTo" : replyTo,
 };
 
  const request = {
    "method": "POST", 
    "headers": headers,
    "body": JSON.stringify(data)
  };

  return fetch(url, request)
   .then(response => response.json());

}
1 Like

Did you solve your problem with Sendinblue api ?

not resolved, still error

Did you try the proposed solution by Sandy here ?

function sendEmailWithSendInBlue() {
var url = “https://api.sendinblue.com/v3/smtp/email”;
var myAccessKey = “[enter your v3 access key]”
var data = {
“sender”:{“name":“Name”,“email”:"name@domain.com”},
“htmlContent”:“this is the body”,
“subject”:“subject”,
“replyTo”:{“email":"name@domain.com”,“name”:“Name”},
“to”:[{“email":"name@domain.com”,“name”:“Name”}]
}
var options = {
‘method’: ‘post’,
‘contentType’: ‘application/json’,
‘payload’: JSON.stringify(data),
‘headers’: {‘api-key’:myAccessKey},
};
var response = UrlFetchApp.fetch(url, options);
Logger.log(response.getResponseCode())
}

Mauro, thx u, my code did not work, because I did not use the " JSON.stringify( )" function. I thought it was strange to use it, because we are already presenting data in the format json.

Not at all, u r welcome. I imagined the use of stringify could be diriment. Just your code will work, could you publish here ? So a correct example could remain here for me and others people (Y)
Thx in advance

Oh sure. I updated the post

could you please tell me the complete method of doing this? i followed sendGrid tutorial but it didn’t work
i have a custom form on a Wix page and i made a database then linked it’s fields to the form, it’s working fine. i now need a way to get notified by email everytime a client fills in the form " a notification containing what he wrote"
i know i need to make a JSW & a JS file in the backend and write some code in the page code cointaining the form, i just don’t know what code to write (i’m a noob :smiley: )

i also know i need to change the API key, sender & receiver emails, database name, and elements names

so could you please give me the code for these 3 sections? pagecode/JSW file/JS file?