Making an API request on Firebase Cloud Functions - firebase

I'm trying to use OpenWeatherAPI for my Actions on Google project. I'm using Firebase Cloud Functions through Dialogflow. How can I make an API request to get data from OpenWeatherAPI?
request.get('http://api.openweathermap.org/data/2.5/uvi?appid=XXX&lat=37.75&lon=-122.37', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
Here are Firebase logs:
statusCode: undefined
body: undefined
error: { Error: getaddrinfo ENOTFOUND api.openweathermap.org api.openweathermap.org:80
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
code: 'ENOTFOUND',
errno: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'api.openweathermap.org',
host: 'api.openweathermap.org',
port: 80 }

By default, Firebase Cloud Functions and the Dialogflow editor use a project that is on Firebase's Spark plan. This is completely free, but does have some restrictions, including no network access outside of Google's services.
In order to access openweathermap.org, you will likely need to upgrade to a paid plan. I suggest the Blaze plan, which is pay as you go over a certain level of usage each month. You will need to register with a credit card, but the usage level for development (and even for a modest level of production usage) should be enough to keep you in the free tier.

Related

How to connect Firebase Admin to Emulator Auth

I am struggling to connect to the emulated Firebase Auth service via the Firebase Admin SDK. I cut down the code to really make the problem stand out, and hope someone can help.
This is the code of the test.js I run (in NodeJS):
// Someone said these two lines should allow the firebase-admin
// SDK to connect to the emulators, but... no.
process.env['GCLOUD_PROJECT'] = 'my-firebase-project-id'
process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080'
const admin = require('firebase-admin')
const app = admin.initializeApp()
const auth = app.auth()
console.log('I have an auth service object')
auth.listUsers().then(users => console.log(users))
I run the emulators like this:
firebase emulators:start --only auth
When I run the test.js file, I get this:
PS C:\...\functions> node .\test.js
I have an auth service object
(node:18232) UnhandledPromiseRejectionWarning: Error: Credential implementation provided to initializeApp() via the "credential" property failed to fetch a valid Google OAuth2 access token with the following error: "Error fetching access token: Error while making request: getaddrinfo EAI_AGAIN metadata.google.internal. Error code: EAI_AGAIN".
at FirebaseAppError.FirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:44:28)
at FirebaseAppError.PrefixedFirebaseError [as constructor] (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:90:28)
at new FirebaseAppError (C:\...\functions\node_modules\firebase-admin\lib\utils\error.js:125:28)
at C:\...\functions\node_modules\firebase-admin\lib\app\firebase-app.js:87:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)
I run this on Windows with the following versions of firebase:
"firebase-admin": "^10.0.2",
"firebase-functions": "^3.18.1",
I read about getting a secret credentials key and adding its path like this:
process.env['GOOGLE_APPLICATION_CREDENTIALS'] = 'C:\\...\\functions\\.runtimekey.json'
And that 'works' in as much as I then can access the real cloud auth instance (as long as the emulators is off) but that isn't what I want. I want to connect firebase-admin and get a list of users in the emulated Auth instance.
Many thanks for any help you can offer!
Set the environment variable FIREBASE_AUTH_EMULATOR_HOST
export FIREBASE_AUTH_EMULATOR_HOST=localhost:9099
Do not include the protocol scheme (i.e http/https)
Or in your case:
process.env['FIREBASE_AUTH_EMULATOR_HOST'] = 'localhost:9099'
Then you can initialise the app as per normal
admin.initializeApp()
Worked for me (after I finally figured out not to include the protocol scheme)
source: https://firebase.google.com/docs/emulator-suite/connect_auth#admin_sdks

Firebase Cloud Functions - Error when sending email [duplicate]

Trying to make a request to Paypal's API using PayPal-node-SDK
exports.requestPayment = functions.https.onRequest((req, res) => {
return new Promise(function (fullfilled, rejected) {
paypal.payment.create(create_payment_json, {}, function (error, payment) {
if (error) {
rejected(error);
} else {
console.log("Create Payment Response");
console.log(payment);
res.status(200).send(JSON.stringify({
paymentID: payment.id
})).end();
fullfilled(payment);
}
});
});
});
but I'm constantly getting an error:
Error: getaddrinfo ENOTFOUND api.sandbox.paypal.com api.sandbox.paypal.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
Things I've tried:
Making a request to a totally different host, still ENOTFOUND
Wrapping the request with cors(req,res, ()=>{...})
Prepending https:// to the host
What is the problem?
You'll need to be on a paid plan to make external API requests.
Firebase's Blaze plan (pay as you go) has a free allotment for Cloud Functions. https://firebase.google.com/pricing/
in my situation I had to wait and let what ever lag was happening pass. Now it's fine again.
I was having this issue because of weak internet, change the internet connection.
You need to include service account to the admin initialization. this fixed the same issue for me
Switch to the Firebase "Blaze" plan, which includes the free usage tier of the Spark plan before incurring any costs. Use the Blaze pricing calculator to see what you'd be charged for a given usage.
The first 5GB of outbound (egress) networking is free, which is the same as what "native" Google Cloud Functions would give you.

Firebase Cloud Functions post to Telegram API [duplicate]

Trying to make a request to Paypal's API using PayPal-node-SDK
exports.requestPayment = functions.https.onRequest((req, res) => {
return new Promise(function (fullfilled, rejected) {
paypal.payment.create(create_payment_json, {}, function (error, payment) {
if (error) {
rejected(error);
} else {
console.log("Create Payment Response");
console.log(payment);
res.status(200).send(JSON.stringify({
paymentID: payment.id
})).end();
fullfilled(payment);
}
});
});
});
but I'm constantly getting an error:
Error: getaddrinfo ENOTFOUND api.sandbox.paypal.com api.sandbox.paypal.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
Things I've tried:
Making a request to a totally different host, still ENOTFOUND
Wrapping the request with cors(req,res, ()=>{...})
Prepending https:// to the host
What is the problem?
You'll need to be on a paid plan to make external API requests.
Firebase's Blaze plan (pay as you go) has a free allotment for Cloud Functions. https://firebase.google.com/pricing/
in my situation I had to wait and let what ever lag was happening pass. Now it's fine again.
I was having this issue because of weak internet, change the internet connection.
You need to include service account to the admin initialization. this fixed the same issue for me
Switch to the Firebase "Blaze" plan, which includes the free usage tier of the Spark plan before incurring any costs. Use the Blaze pricing calculator to see what you'd be charged for a given usage.
The first 5GB of outbound (egress) networking is free, which is the same as what "native" Google Cloud Functions would give you.

Fetching google sheets data from firebase "Billing account not configured" even if it's google api

I'm trying to fetch some data from a google spreadsheet. According to the firebase pricing it says that cloud functions are limited to Google services which means that this should work. However when I try to fetch the data I get the the error:
Error: getaddrinfo ENOTFOUND spreadsheets.google.com spreadsheets.google.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
I am trying with the following code:
export const fetchExcel = functions.https.onRequest((req, res) => {
const query = "https://spreadsheets.google.com/feeds/list/hidden/od6/public/values?alt=json"
https.get(query, resp => {console.log (resp)});
});
so I am not making an api call to a non-google service yet somehow I believe it's blocking me. How else can you do it?
Without a payment plan required, Cloud Functions can make outgoing HTTP requests to APIs under control of Google. Typically these are well documented APIs, and are protected from abuse. It's worth noting however, this does not necessarily include anything in the google.com domain. An API has to be specifically whitelisted for use without a payment plan.
If you run into a formal Google-controlled API that is not whitelisted, please file a feature request to have that evaluated.

If I can use axios library for requesting 3rd api in firebase cloud functions [duplicate]

Trying to make a request to Paypal's API using PayPal-node-SDK
exports.requestPayment = functions.https.onRequest((req, res) => {
return new Promise(function (fullfilled, rejected) {
paypal.payment.create(create_payment_json, {}, function (error, payment) {
if (error) {
rejected(error);
} else {
console.log("Create Payment Response");
console.log(payment);
res.status(200).send(JSON.stringify({
paymentID: payment.id
})).end();
fullfilled(payment);
}
});
});
});
but I'm constantly getting an error:
Error: getaddrinfo ENOTFOUND api.sandbox.paypal.com api.sandbox.paypal.com:443
at errnoException (dns.js:28:10)
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:76:26)
Things I've tried:
Making a request to a totally different host, still ENOTFOUND
Wrapping the request with cors(req,res, ()=>{...})
Prepending https:// to the host
What is the problem?
You'll need to be on a paid plan to make external API requests.
Firebase's Blaze plan (pay as you go) has a free allotment for Cloud Functions. https://firebase.google.com/pricing/
in my situation I had to wait and let what ever lag was happening pass. Now it's fine again.
I was having this issue because of weak internet, change the internet connection.
You need to include service account to the admin initialization. this fixed the same issue for me
Switch to the Firebase "Blaze" plan, which includes the free usage tier of the Spark plan before incurring any costs. Use the Blaze pricing calculator to see what you'd be charged for a given usage.
The first 5GB of outbound (egress) networking is free, which is the same as what "native" Google Cloud Functions would give you.

Resources