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.
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, 202211:25 AM - edited Feb 4, 202212:03 PM
Participant | Platinum Partner
Call other serverless functions
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.