APIs & Integrations

RLee6
Member

Call other serverless functions

Is it possible to call into a serverless function from another serverless function? The goal is to create three separate functions:

  • First function will generate an API key
  • Second and third functions will call out to different external APIs and passing in the API key generated from the first function

Thank you for your assistance!

0 Upvotes
webdew
Guide | Diamond Partner
Guide | Diamond Partner

Call other serverless functions

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. 

0 Upvotes
louischausse
Key Advisor | Platinum Partner
Key Advisor | Platinum Partner

Call other serverless functions

I am using my mirror @pChausseC 😛

 

Louis Chaussé

CEO

Auxilio

lchausse@auxilio.io
auxilio.io
Schedule a call
0 Upvotes
pChausseC
Participant | Platinum Partner
Participant | Platinum Partner

Call other serverless functions

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.

RLee6
Member

Call other serverless functions

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?

0 Upvotes
pChausseC
Participant | Platinum Partner
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.

RLee6
Member

Call other serverless functions

Hi @louischausse ,

 

Any guidance on this one?

0 Upvotes
dennisedson
Community Manager
Community Manager

Call other serverless functions

@louischausse ,

Any thoughts here?

Thanks,

Dennis




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