I've setup a Firebase function for a Google action with Dialogflow that makes an API call using node-fetch
but I'm running into problems when I make the actual request. Even though I can visit the URL in the browser, I'm not able to get it to resolve when my function runs.
Error:
{"message":"request to https://jsonplaceholder.typicode.com/users failed, reason: getaddrinfo ENOTFOUND jsonplaceholder.typicode.com jsonplaceholder.typicode.com:443","type":"system","errno":"ENOTFOUND","code":"ENOTFOUND"}
Code:
import * as functions from 'firebase-functions';
import fetch from 'node-fetch';
export const fetchTrainTimetable = async (): Promise<object> => {
const path = `https://jsonplaceholder.typicode.com/users`
try {
const response = await fetch(path, {method: 'GET'});
return await response.json();
} catch (error) {
return error;
}
}
Is there something missing that I need to include in the request in order to make an outbound request with Firebase functions? It doesn't seem to matter what the path is, I always end up with this error.
The issue is that you are using Firebase Functions under the default "spark" plan, which is free, but which has the limitation that it can't access web or network services outside of Google's.
You can upgrade to the "blaze" plan, which allows network access, but which requires you to register a credit card and charges for use. However, even on the blaze plan, there is a free tier which they won't bill you for. This free tier is generally enough for experimenting, initial development, and use under early deployment. Once you are deployed, the Google Assistant has offers for cloud credits which should offset this as well.