CMS Development

HSollie
Member | Diamond Partner
Member | Diamond Partner

External API to populate custom module

Hi! 

 

I wish to populate a custom module with data from an external API, which requires an api_key in the request header in order to authorize. I currently receive the response 401 on my request. I have followed the https://developers.hubspot.com/docs/cms/guides/getting-started-with-serverless-functions in order to create serveless functions, with bad luck. I have put my api-key as a secret with the HubSpot Cli. Code is below: 

 

congratulations.js in my .functions-folder 

 

 

var axios = require("axios");
var API_KEY = process.env.API_SECRET_KEY;

exports.main = (context, sendResponse) => {
  var config = {
    method: "GET",
    url: `https://example.api.com`,
    headers: {
      "X-Api-Key": API_KEY,
    },
  };
  axios(config)
    .then(function(response) {
      sendResponse({ body: { response: response.data }, statusCode: 200 });
    })
    .catch(function(error) {
      sendResponse({ body: { error: error.message }, statusCode: 500 });
    });
};

 

 

 

and this is mye serverless.json

 

 

{
  "runtime": "nodejs12.x",
  "version": "1.0",
  "secrets": ["API_SECRET_KEY"],
  "endpoints": {
    "congratulations": {
      "method": "GET",
      "file": "congratulations.js"
    }
  }
}

 

 

 

Do you know why I get 401 response - am I missing something? And is this the easiest way to populate a custom module with data from external API? Please let me know if there is any better way to do it - or let me know if HubSpot does not support external APIs to populate modules.. 

 

Thank you in advance! 

0 Upvotes
1 Reply 1
dennisedson
Community Manager
Community Manager

External API to populate custom module

Hello @HSollie 

Welcome to the Community!

@piersg , @jpsanchez  could you all help take a stab at this? 😺

Thanks,

Dennis




HubSpot Community Developer ShowMake sure to subscribe to our YouTube channel
where you can find the HubSpot Community Developer Show
0 Upvotes