Netlify Background Functions - Unable to contact external script?

Hey all,

For this app: spontaneous-conkies-b6d9f2.netlify.app I created a background function, using the docs I could find from Netlify. The script initializes and attempts to run, however I’ve noticed a consistent issue: I can’t seem to use it to post to an external API.

The function I’m working on needs to contact Brevo (an email sending server) and send emails. This functionality works on a non-background script hosted on Netlify, but when I run the background version … it just hangs, and I don’t get any response from my calls. I can load from another source (Sanity) but not the calls to Brevo. As I write this, I’m wondering if it’s a CORS error, though I get no errors or responses in the Netlify log.

Any directions people could point me in would be very helpful. Thanks!

Hi @mark1 , without sharing any code snippet it would be difficult to assist.
However since you are depending on an external API you can console log the the API response to see if your request was actually successful.

If you are expecting a response from a background function note that background functions do not send responses like you would expect. The below is quoted from the Netlify official documentation

Background functions don’t support response streaming because they don’t return responses.

You have to handle responses in a different manner as stated on the Netlify official documentation

When a background function is successfully executed, you generally pass the result to a destination other than the originating client.

For more information kindly check the Netlify documentation on how background functions work below.

Thanks for the response!

Sorry, I might have asked my question unclearly: if i run the background function in my local environment (using netlify dev) it successfully hits sends the email via Brevo. When I try the same code on Netlify, it doesn’t return anything in the log; from the functions log it just stops and doesn’t show the contents of the console.logs I’ve added. It does show everything before the Brevo calls, in the log.

Crucially, the emails don’t get sent. Weirdly, though, if I set it as a regular function, it works fine, and the email is sent. I’m hoping to use a background function due to the increased timeout limit.

Hi @mark1 as I stated before without sharing any code snippet it would be difficult to assist.
However a debugging tip that I can suggest for you in order to investigate if the problem is happening on Netlify or on Brevo’s end is that you comment out the Brevo call and just do a regular console.log to see if the function returns all logs.

If it works that means the Brevo call on production is what is causing the issue.
The reasons for the above happening could be many.
For example Brevo could be blocking requests from Netlify servers, cors e.t.c

Doing the above will help with the debugging.

Thanks.