Skip to content

Instantly share code, notes, and snippets.

@Suvink
Last active April 14, 2020 19:45
Show Gist options
  • Save Suvink/a6d1f7528b35e46d5a9c292fe5720018 to your computer and use it in GitHub Desktop.
Save Suvink/a6d1f7528b35e46d5a9c292fe5720018 to your computer and use it in GitHub Desktop.
SecureAPI
const functions = require('firebase-functions');
const request = require('request');
exports.secureAPI = functions.https.onRequest((req, res) => {
if(req.method !== "GET"){
return res.status(400).json({
message: "Not Allowed"
})
}
request('your http URL goes here', function (error, response, body) {
if (!error) {
responseData = response.toJSON()
res.status(200).send(responseData.body)
}
})
})

SecureAPI

SecureAPI is a simple Firebase Cloud Function that relays insecure http APIs into secured https APIs. This comes in handy when some API providers charge you for https APIs. :wink

How to use

  • Step 1

    • Install Firebase CLI and NodeJS if you havent got them already.
  • Step 2

    • Goto Firebase console an create a fresh project. Note down the project ID.
  • Step 3

    • Create a new directory
    • Execute the command firebase init inside the directory.
  • Step 3

    • Select Functions from CLI features you want to setup dialog and in the next step choose the project that you noted down earlier when you created a new project. Then select Javascript for the language
  • Step 4

    • Install Request Package from command line by executing npm install --save request
  • Step4

    • Copy the content of index.js and paste them in your local index.js file
  • Step 5

    • Install the dependancies by executing npm install
    • Run the function by executing firebase emulators:start --only functions

Deploying

When you're done with testing and want to deploy this into Firebase, do the following.

First you need up upgrade your firebase project into **Blaze plan**. Firebase's always free **Spark Plan** does not support calling 3rd party APIs.

Then you can deploy your function simply by executing firebase deploy on your terminal.

Tutorial

If you are not familiar with Firebase functions, You can find a beginners tutorial I wrote here.

Link: https://dev.to/suvink/how-to-create-a-firebase-cloud-function-585g

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment