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 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.
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.
Jan 26, 2022 3:10 PM