Sorry there’s no public repo for this atm. (Newbie mistake - I deployed my API keys to GitHub at some point I did make a sample repo with a clean history, but it’s not up-to-date).
fwiw, here’s my function. I’m doing something wrong with my error handling because the response I get back for these failing calls is a 200 even though none of these API calls are working.
(i.e. putting through an email that already exists will still print “Adding new email.” Putting in an email that does not exist prints the same. No changes are made within Sendinblue.)
// functions/sendinblue.js
require("dotenv").config();
const fs = require("fs").promises;
const SibApiV3Sdk = require("sib-api-v3-sdk");
var defaultClient = SibApiV3Sdk.ApiClient.instance;
var apiKey = defaultClient.authentications["api-key"];
apiKey.apiKey = process.env.SENDINBLUE;
var apiInstance = new SibApiV3Sdk.ContactsApi();
const headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Headers": "Content-Type",
};
/* FIX error handling */
export async function handler(event, context) {
let contactObj = { statusCode: 200, headers, response: "something happened" };
try {
const data = JSON.parse(event.body);
const attributes = {
FIRSTNAME: data.firstName,
LASTNAME: data.lastName,
};
let payments = await fs.readFile("./src/data/payments.json");
payments = JSON.parse(payments);
const product = payments.find((payment) => payment.name === data.product);
const listId = parseInt(product.list);
const contacts = [];
/* Get all contacts */
const existingContact = await apiInstance
.getContacts()
.then(function (output) {
const stringified = JSON.stringify(output);
const parsed = JSON.parse(stringified);
parsed.contacts.forEach((contact) => {
contacts.push(contact.email);
});
console.log("ping");
return contacts.includes(data.email);
});
/* Check if contact is in list */
if (existingContact) {
console.log("Existing contact: ", data.email);
var contactEmails = new SibApiV3Sdk.AddContactToList();
contactEmails.emails = [data.email];
apiInstance.addContactToList(listId, contactEmails).then(
function (output) {
const message = "API called successfully. Returned data: " + output;
console.log(message);
contactObj.response = message;
return contactObj;
},
function (error) {
console.error(error);
contactObj.statusCode = error.status;
contactObj.response = error.error;
return contactObj;
}
);
} else {
console.log("Add new contact: ", data.email);
var createContact = new SibApiV3Sdk.CreateContact();
createContact.email = data.email;
createContact.listIds = [listId];
createContact.attributes = attributes;
apiInstance.createContact(createContact).then(
function (output) {
output = JSON.stringify(output);
const message = "API called successfully. Returned data: " + output;
console.log(message);
contactObj.response = message;
return contactObj;
},
function (error) {
console.log(error);
contactObj.statusCode = error.status;
contactObj.response = error.error;
return contactObj;
}
);
}
return contactObj;
} catch (error) {
console.log(error);
contactObj.statusCode = 400;
contactObj.response = error;
return contactObj;
} finally {
console.log("in finally block");
}
}
Except for the error handling (seeing “This request has no response data available”), this works fine in dev.
Your current thread wasn’t too far in line from your previous one, so here we go.
About SendinBlue, I’ve used it in the past with success, however I didn’t use SendInBlue package. I used emailjs which does the same thing - send emails.
Here’s how I had used it in the past:
Would it be possible for you to try that?
If not, I think we’d need a repo so that we can test this ourselves. Yes, we can copy-paste the code, but having a pre-made setup to test the exact problem is much easier than trying to create the problem.
Your repo was slightly off than testing conditions , but I was able to get around it. About your problem, I didn’t try to use the library as that would need me to read their documentation and debug their issues. So instead, I went with Axios and got it working:
I’d recommend using Axios (or Node Fetch) for the following reasons:
You’re relying on one-less dependency that can have breaking changes in the future. Yes, we’re swapping the official library with Axios, but I feel Axios is more tried-and-tested than adding a more complex library.
Since we’ll always be using v3 API in this case, it’s more resilient to breaking changes in the future.
We can control the HTTP requests being used which can be easier to debug in my opinion.
I followed this guide for API endpoint reference:
But I believe, you can employ a similar structure using the library if you wish to.
On a side note (but not required), I’d probably recommend storing the environment variable in Netlify UI over using dotenv - but that’s my personal preference.
I’m curious why you think the code I’m using works in dev but not in prod? I would prefer to use the Sendinblue library if I can. This seems like a config issue possibly?
Your repo was slightly off than testing conditions