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 24, 2022 6:15 PM
Is it possible to call into a serverless function from another serverless function? The goal is to create three separate functions:
Thank you for your assistance!
Feb 3, 2022 11:42 PM
Hi @RLee6 ,
Yes you can do this , just call another function in response of the first function that will give you the api key.
Hope this helps!
If we were able to answer your query, kindly help the community by marking it as a solution.
Thanks and Regards.
Feb 1, 2022 12:20 PM
I am using my mirror @pChausseC 😛
| ||||||||||||
|
Feb 3, 2022 2:53 PM - edited Feb 3, 2022 2:54 PM
I believe it should be possible, as you'd simply be requesting a resource from an endpoint, but I've never done it like that. Looking at your situation I don't understand why that would be your goal (having 3 separate serverless functions).
Following the middleware pattern, you should be calling your middleware function in all serverless functions that need it.
In your case, the middleware would be the api key generator, as it is always need for 2nd and 3rd function. You would end up with 2 serverless functions (2nd and 3rd).
Why do you want to make them 3 separate functions, if you can't use 2nd or 3rd without having the key from the 1st one?
Note that I believe it would not be safe to call the 1st function from a client, and subsequently call 2nd or 3rd functions, passing the apikey return from 1st as a param. It would expose the key on the client side. It wouldnt even be safe to have the apikey generator be a serverless function withouth an authentication protocol. It would mean that anyone could ping your function and get an apikey to whatever service it gets access to.
Basically it feels odd to me that you want to expose the api key generator through a serverless function, instead of 'hiding' it in the serverless functions that need it.
Feb 3, 2022 5:30 PM
Hi @pChausseC. Thanks for the response.
I agree with the concern. I tried extracting out the API Key generation logic into a separate Javascript file and then importing the file into each serverless function that requires API calls to be made, but I couldn't get past errors related to importing the file. I'm fairly new to Node.JS, so I'm sure I'm missing something elementary. Do I need to add to serverless.json or a different configuration file so that the JS file is importable?
Feb 4, 2022 11:25 AM - edited Feb 4, 2022 12:03 PM
I think you'd have to bundle your separate js file and your serverless function js file together to get one final file, with webpack or any other bundler. I don't think hubspot provides any example of that, though. You could ask in the developer slack to see if anyone as had to this before. If you don't want to mess with that, the 'KISS' way is to keep the API key generation logic in the same file as the serverless function that needs it (and, unfortunatly, repeating that code) instead of importing it.
Feb 2, 2022 8:42 PM
Jan 26, 2022 3:10 PM
Any thoughts here?
![]() | Make sure to subscribe to our YouTube channel where you can find the HubSpot Community Developer Show |