'My nodeJS cloud function crashes on client.request

trying to make a cloud function NodeJS using HTTP trigger but it always crashes and throws me error 500. Looking into the logs it simply tells me

client.request is not a function

My code looks like the following where I simply take POST request body email and send it to SendGrid. I took the code from SendGrid and tested it locally without being inside a (req, res) request and worked fine.

require('dotenv').config()
const client = require('@sendgrid/mail')
const sendgridKey = process.env.sendgrid_api_key

client.setApiKey(sendgridKey);


exports.welcomeEmail = (req, res) => {
  
  const data = {
    "contacts": [
      {
        "email": req.body.record.email
      }
    ],
    "list_ids": ["somevalue"]
  };
  
  const request = {
    url: `/v3/marketing/contacts`,
    method: 'PUT',
    body: data
  }
  
  client.request(request)
    .then(([response, body]) => {    
      let message = response.body
    return message
    })
    .catch(error => {
      let message = error
    
    return message
    });
  
  res.status(200).send(message);
};


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source