CMS Development

EPasiak
Participante

Connect to external API using serverless function

resolver

Hello all,

I am currently having troubles using the HubSpot CMS serverless function to connect to an AWS API.

The API end point functions correctly and has been tested using Postman.

I would like to use a serverless function in a NodeJS environment to connect to the API and send the result to the HubSpot CMS.

I have set up a the NodeJS serverless function following this tutorial: https://developers.hubspot.com/docs/cms/guides/getting-started-with-serverless-functions

I have now replaced the body of the tutorial function with a simple Http request using the built in https module included in NodeJS but it does not seem to be connecting to my API.

For the purpose of this question I have included the code snippet with an example http request that follows the same strategy I have been trying to implement with my own API.

Running the serverless function below I do not seem to be hitting the API code and the functionResponse is not updated with the result of the call.

exports.main = (context, sendResponse) => {
  // your code called when the function is executed
  var functionResponse = "Congrats! You've just deployed a Serverless Function."
  
  const https = require('https');
  let data = '';
  https.get('https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY', (resp) => {
    
    // A chunk of data has been received.
    resp.on('data', (chunk) => {
      data += chunk;
    });
  
    // The whole response has been received. Print out the result.
    resp.on('end', () => {
      console.log(JSON.parse(data).explanation);
      functionResponse = JSON.parse(data).explanation;
    });
  
  }).on("error", (err) => {
    console.log("Error: " + err.message);
  });

  sendResponse({body: functionResponse, statusCode: 200});
};


 Thank you for your help!

0 Avaliação positiva
1 Solução aceita
EPasiak
Solução
Participante

Connect to external API using serverless function

resolver

 I was able to resolve the issue!

The logs were not updating properly and I was not seeing the most recent log when using the hs logs --follow CLI command.

When I checked the logs using hs logs --latest I was getting the expected result.

 

Thank you for your response!

Exibir solução no post original

2 Respostas 2
dennisedson
Equipe de Produto da HubSpot
Equipe de Produto da HubSpot

Connect to external API using serverless function

resolver

Hello @EPasiak 

Welcome to the Community!

I am wondering if @lynton can posit some thoughts here.  I believe he is deep into serveless now.  Could be wrong

0 Avaliação positiva
EPasiak
Solução
Participante

Connect to external API using serverless function

resolver

 I was able to resolve the issue!

The logs were not updating properly and I was not seeing the most recent log when using the hs logs --follow CLI command.

When I checked the logs using hs logs --latest I was getting the expected result.

 

Thank you for your response!