# Connect to external API using serverless function

**URL:** <https://community.hubspot.com/t/connect-to-external-api-using-serverless-function/29753>\
**Category:** CMS Development\
**Tags:** javascript\
**Created:** [January 5, 2021, 9:09pm UTC](https://community.hubspot.com/t/connect-to-external-api-using-serverless-function/29753 "2021-01-05T21:09:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EPasiak](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/epasiak/32/63860_2.png) [@EPasiak](https://community.hubspot.com/u/EPasiak)\
**Post date:** [January 5, 2021, 9:09pm UTC](https://community.hubspot.com/t/connect-to-external-api-using-serverless-function/29753/1 "2021-01-05T21:09:33Z")

</div>

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: [Get started with serverless functions - HubSpot docs](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.

```lia-code-sample
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!

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [January 6, 2021, 7:44pm UTC](https://community.hubspot.com/t/connect-to-external-api-using-serverless-function/29753/2 "2021-01-06T19:44:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![EPasiak](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/epasiak/32/63860_2.png) [@EPasiak](https://community.hubspot.com/u/EPasiak)\
**Post date:** [January 6, 2021, 9:11pm UTC](https://community.hubspot.com/t/connect-to-external-api-using-serverless-function/29753/3 "2021-01-06T21:11:02Z")

</div>

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!
