Your first Firebase Function
Tags: javascript, typescript, firebase, devops
Published: 2020-07-16 22:35:17

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](https://firebase.google.com/docs/functions).

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](https://dev.to/gabrielrufino/deploy-a-website-using-firebase-hosting-4ek4).

> 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](https://dev-to-uploads.s3.amazonaws.com/i/8my641lsbon12d0fiau8.png)

## 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:

```js
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`:

```js
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:

```js
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!