Getting a 502 Error only on live server when calling function

Site name: https://resolv.netlify.app

Issue: My code works just fine on the dev environment, but when I deploy it to the live server I am met with this 502 error saying the site cannot load my functions:
image

Things I’ve Already Tried/Discovered:

  • Error happens in less than 10 sec (time is not the issue)
  • Made sure npm and node versions are the same on local and prod
  • Used ${process.env.URL} to create a definite path… didn’t work
  • Functions are successfully getting deployed (seen in my logs)

Here’s my code. I’ve added everything in case there’s a function that I am using that isn’t supported by Netlify’s current version of node (as I saw in another solution):

const validateEmail = (email) => {
  return email.indexOf('@') !== -1;
};

async function registerUser() {
  const form = document.getElementById('input');
  const successBox = document.getElementById('success-box');
  const errorBox = document.getElementById('error-box');
  const feedbackBox = document.getElementById('feedback-box');
  const email = document.getElementById('email-input').value;

  if (!validateEmail(email)) {
    feedbackBox.classList.add('d-block');
    return;
  }
 
  const headers = new Headers();
  headers.append('Content-Type', 'application/json');
  
  const options = {
    method: 'POST',
    headers,
    body: JSON.stringify({email}),
  };

  const response = await fetch('/.netlify/functions/index', options);
  
  if (response.ok) {
    form.classList.add('d-none');
    successBox.classList.remove('d-none');
    feedbackBox.classList.add('d-none');
  }
  else {
    form.classList.add('d-none');
    errorBox.classList.remove('d-none');
  }
}

I pray you guys can help… been at this one for 6 hours :smiling_face_with_tear:

While I originally thought the server wasn’t able to locate my netlify functions through the URL provided (/.netlify/functions/index), I found that the issue was actually that my API token was being read as “invalid” because my .env file wasn’t automatically uploaded into my netlify site. A simple netlify env:import .env command worked to upload them to the deployed site.

Hi @DaKingLawson, :wave:t6: thank you for coming back to the forums and letting us know what helped solved your issue and unblock you in the build process. :netliconfetti: If other users have environment file issues this will certainly help them in the future with their build. We really appreciate this. :smiling_face_with_three_hearts: