I am trying to make a call to apples receipt verification server using Cloud Functions for Firebase. Any idea how to make an HTTP call?
Requests on your Firebase Hosting site can be proxied to specific HTTP functions. This also allows you to use your own custom domain with an HTTP function.
For Cloud Functions (1st gen): In the Trigger type field, select HTTP. In the Authentication field, select an option depending on whether you want to allow unauthenticated invocations of your function. By default, authentication is required.
Cloud Functions for Firebase is a serverless framework that lets you automatically run backend code in response to events triggered by Firebase features and HTTPS requests. Your JavaScript or TypeScript code is stored in Google's cloud and runs in a managed environment.
onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.
Keep in mind that your dependency footprint will affect deployment and cold-start times. Here's how I use https.get()
and functions.config()
to ping other functions-backed endpoints. You can use the same approach when calling 3rd party services as well.
const functions = require('firebase-functions'); const https = require('https'); const info = functions.config().info; exports.cronHandler = functions.pubsub.topic('minutely-tick').onPublish((event) => { return new Promise((resolve, reject) => { const hostname = info.hostname; const pathname = info.pathname; let data = ''; const request = https.get(`https://${hostname}${pathname}`, (res) => { res.on('data', (d) => { data += d; }); res.on('end', resolve); }); request.on('error', reject); }); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With