[Support Guide] Using environment variables on Netlify correctly

I finally got it. Ok so if you are using axios for your function request this return structure worked for me… this post: [Netlify Functions + axios Get not working in production - #2 by hrishikesh] - the odd thing is locally & using await on my request would return a response and included data, but production deploy was returning undefined/bad request no matter what I would try.

The documentation shows the return schema for the lambdas but is somewhat confusing in implementation - I had thought to return the actual data from the request and then eval it on the client as ‘response.statusCode’, NO this is incorrect, the netlify-function EXPECTS you to return a value called ‘statusCode’ no matter what the actual request response data is - so take note of that, dont try to assign a response-value, just assign it a value. In the end I scrapped sending the returned .status value from the request and just assigned a 200 if good and 422 if bad.

I also took a look at the function’s log and noticed that my NetlifyUI Environment vars I had quotes around the values - so it was sending → " ‘variable’ " (*again this worked locally, but not in prod): lesson, don’t put quotes around your vars in the UI.

After removing those and the other thing with my return values, it indeed does work. I hope this might save someone else frustration and copious amounts of time.

–>Don’t do this:

...
let responseStatusCode = 444;
...
await axios(config)
    .then((response) => {
        console.log("response:: ", response.status)
        responseStatusCode = response.status || 200
    })
    .catch((error) => {
        console.log("func-error:: ", error.response.status)
        if(error)
            responseStatusCode = error.status || 500
    })
    return { statusCode: responseStatusCode }


-->Do something like this instead (see the referenced post above for a prettier version): 

return axios(config)
    .then((response) => { 
        return { statusCode: 200, body: response.data ? JSON.stringify(response.data) : "no-response-data-given" }
    })
    .catch((error) => {
        console.log(error);
        return { statusCode: 422, body: `Error: ${error ? error : "no error data given"}`}
    })