We use cookies to make HubSpot's community a better place. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. To learn more, and to see a full list of cookies we use, check out our Cookie Policy (baked goods not included).
Jan 26, 2021 2:15 PM - edited Jan 26, 2021 2:17 PM
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!
Jan 28, 2021 1:58 PM
Hello @HSollie
Welcome to the Community!
@piersg , @jpsanchez could you all help take a stab at this? 😺
![]() | Make sure to subscribe to our YouTube channel where you can find the HubSpot Community Developer Show |