loading...
Cover image for Your first Firebase Function

Your first Firebase Function

gabrielrufino profile image Gabriel Rufino ・3 min read

With Firebase Cloud Functions, you don't have to worry about server management. All the boring tasks are made by the Firebase, and you worry about the main thing: the business rule of your application.

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. There's no need to manage and scale your own servers.

Above, you read the definition of Firebase Cloud Functions according to the documentation.

The purpose here is to implement your first function triggered by an HTTP call.

Create a project

Obviously, you gonna need a firebase project to deploy your first function. It's pretty easy and I explain how to do it in my post Deploy a website using Firebase Hosting.

This is a post in English written by a Brazilian. I decided to try to write a series of articles in a new language with more reach than Portuguese using a translator eventually because I'm not fluent yet. So, I ask you for feedback and corrections if you find any errors. Thanks!

Init a folder

If you don't have the CLI of firebase, e execute this command:

$ npm install -g firebase-tools

So, now you create a folder and init a firebase project. Some like this:

$ mkdir firebase-lab-functions
$ cd firebase-lab-functions
$ firebase init

And follow the init steps:

  1. Select the option Functions: Configure and deploy Cloud Functions
  2. Select Use an existing project if you already create the project.
  3. Choose the project.
  4. Choose between JavaScript or TypeScript as the language of the functions.
  5. Ask if you want to use ESLint style
  6. Input Y to install the dependencies.

Look at my init inputs:

Firebase init inputs

Create a function

Let's create our function. Note that the init create a folder called functions with a file called index.js with this content:

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
//   functions.logger.info("Hello logs!", {structuredData: true});
//   response.send("Hello from Firebase!");
// });

We can remove the comments of the function helloWorld:

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Firebase!");
});

Note that you need to export the function specifying the trigger. In this case, we export the function by exports.helloWorld using the trigger functions.https for HTTP calls! And then, we say to execute our controller as the callback of the event listener onRequest.

I would like to change the response:

const functions = require('firebase-functions');

// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
exports.helloWorld = functions.https.onRequest((request, response) => {
  functions.logger.info("Hello logs!", {structuredData: true});
  response.send("Hello from Gabriel Rufino!");
});

Deploy

The last task is to deploy the function with a simple command at the folder you created:

$ firebase deploy

This problem occurred to me: https://stackoverflow.com/questions/62852071/cloud-functions-deployment-requires-the-pay-as-you-go-blaze-billing-plan

The deploy will give you the endpoint of your function!

Thanks!

Posted on by:

gabrielrufino profile

Gabriel Rufino

@gabrielrufino

Fullstack Developer around JavaScript stack. Solving problems, generating value!

Discussion

markdown guide
 

great post! are you using Firebase functions in production now?

 

When I last used firebase functions they had a restriction accessing external APIs which were not firebase. Is this still the case? i've switched to Netlify functions :)