title: How do I call a third party Rest API from Firebase function for Actions on Google
body: <p>I am trying to call a rest API from Firebase function which servers as a fulfillment for Actions on Google. </p>
  
  <p>I tried the following approach:</p>
  
  <pre><code>const { dialogflow } = require('actions-on-google');
  const functions = require('firebase-functions');
  const http  = require('https');
  const host = 'wwws.example.com';
  
  const app = dialogflow({debug: true});
  
  app.intent('my_intent_1', (conv, {param1}) =&gt; {
  
           // Call the rate API
          callApi(param1).then((output) =&gt; {
              console.log(output);
              conv.close(`I found ${output.length} items!`); 
          }).catch(() =&gt; {
              conv.close('Error occurred while trying to get vehicles. Please try again later.'); 
          });
  
  });
  
  function callApi (param1) {
      return new Promise((resolve, reject) =&gt; {
          // Create the path for the HTTP request to get the vehicle
          let path = '/api/' + encodeURIComponent(param1);
          console.log('API Request: ' + host + path);
  
  
          // Make the HTTP request to get the vehicle
          http.get({host: host, path: path}, (res) =&gt; {
              let body = ''; // var to store the response chunks
              res.on('data', (d) =&gt; { body += d; }); // store each response chunk
              res.on('end', () =&gt; {
                  // After all the data has been received parse the JSON for desired data
                  let response = JSON.parse(body);
                  let output = {};
  
                  //copy required response attributes to output here
  
                  console.log(response.length.toString());
                  resolve(output);
              });
              res.on('error', (error) =&gt; {
                  console.log(`Error calling the API: ${error}`)
                  reject();
              });
          }); //http.get
      });     //promise
  }
  
  exports.myFunction = functions.https.onRequest(app);
  </code></pre>
  
  <p>This is almost working. API is called and I get the data back. The problem is that without async/await, the function does not wait for the "callApi" to complete, and I get an error from Actions on Google that there was no response. After the error, I can see the console.log outputs in the Firebase log, so everything is working, it is just out of sync.</p>
  
  <p>I tried using async/await but got an error which I think is because Firebase uses old version of node.js which does not support async. </p>
  
  <p>How can I get around this?</p>