Getting 502 error from netlify function on form submission

site name:
lambda func. name:

Hey y’all! Getting a 502 when the form on this pages submits. The form works locally without no problems. I tried the other solutions on this site and not of them work. Checkout the code below.

Network error


const { GoogleSpreadsheet } = require('google-spreadsheet')

exports.handler = async (event, context, callback) => {
  // Getting lead data from form
  const getLeadData = JSON.parse(event.body)

  // Google Sheet Doc
  const doc = await new GoogleSpreadsheet(

  // Google Authentication
  await doc.useServiceAccountAuth({
    client_email: process.env.GATSBY_GOOGLE_SHEET_LEADS_CLIENT_EMAIL,
    private_key: process.env.GATSBY_GOOGLE_DEV_PRIVATE_KEY,

  await doc.loadInfo() // loads document properties and worksheets

  const sheet = await doc.sheetsByIndex[0] // or use doc.sheetsById[id]

  await sheet.addRow(getLeadData)

  callback(null, {
    statusCode: 200,
    body: JSON.stringify(getLeadData),

Here is the link to the repo.


It was a problem on how I was importing the API key. Here is where I found the solution.

1 Like

Amazing, thanks so much for sharing the solution!