This site runs best with JavaScript enabled.

Super simple start to Firebase functions

Photo by Red Zeppelin


The simplest getting started guide for Firebase Cloud functions

I was one of the first users of Firebase back in the day. In fact, I spearheaded their "torch program" for firebase advocates (this has been long discontinued and was way before they were bought by Google). I was there when they announced Firebase hosting. I've built a dozen or so small apps using Firebase for data and/or authentication. But for one reason or another, I dropped off the firebase bandwagon and tried some other services that treated me exceedingly well.

But now I'm back and I'm excited to try all that firebase now has to offer. In particular, I'm playing around with remix and I've just added Firebase Firestore for data storage and want to add authentication. Firebase Firestore + Firebase Authentication + Firebase Cloud Functions just seems like the perfect combination. And while I've used Netlify's functions feature, Firebase is a bit of a different animal. Breaking things down to their most basic and absolutely necessary components helps me get the right idea of the abstractions I'm working with, so without any further ado, here's your "Super simple start to Firebase functions."

How to use Firebase Cloud Functions

Before you get started, you'll need a GitHub account, a Firebase account (just use your Google account), and a fresh repository. Once you've got the project locally on your computer, then you can start with the following steps:

Step 1: Create a Firebase project

Firebase console showing an "Add project" button

Go to the firebase console and create a new project. Follow the steps there (your option selections here don't matter).

You will need to remember the name of your project. You can find it in the URL after your project is created:

1https://console.firebase.google.com/u/0/project/{your-project-id-is-here}/functions

It's generated when you give the project a name, and you can change it at that time as well:

Let's start with a name for your project. Project name: My Awesome Firebase Thing. my-awesome-firebase-thin-33d3a. A unique identifier for your project

Step 2: Create a firebase.json file

For firebase to recognize your project as a firebase project, it needs to have a firebase.json file. I'm sure there's lots of important config that can go in here, but no config is needed for this super simple start, so just put in an empty object:

1{}

Step 3: Create a .firebaserc file

This file is where you actually associate your repository with the firebase project (#spoileralert so when you run firebase deploy later, it knows which project to deploy your code to).

Here's all you need in that file:

1{
2 "projects": {
3 "default": "{your-project-id}"
4 }
5}

Make sure to change {your-project-id} to whatever your project ID is. Again, you can get that from the URL of your project on the firebase console as mentioned above. You can also get it from your project's settings page:

Firebase console showing the setting page with the Project ID

Step 4: Create a functions/package.json file

Here's what you need in there:

1{
2 "engines": {
3 "node": "12"
4 },
5 "dependencies": {
6 "firebase-admin": "^9.3.0",
7 "firebase-functions": "^3.11.0"
8 }
9}

firebase-admin and firebase-functions are required dependencies to run a Firebase Cloud Function. You technically don't have to use firebase-admin but if you don't have it installed, your function won't deploy. We'll use firebase-functions next.

engines.node is also required so Firebase knows which version of Node.js you want your functions in.

Step 5: Add .gitignore

Before we install these dependencies, let's get .gitignore ready to ignore node_modules:

1node_modules
2
3# ignores a few log files firebase creates when running functions locally
4*.log

Step 6: Install function dependencies

1cd functions && npm install

This should get all dependencies installed and it'll generate a package-lock.json file which you can safely ignore for now (but you will want to commit it to your repository).

Step 7: Create a functions/index.js file

Now that you have dependencies installed, you can create your first function.

Here's what you'll put in there:

1const functions = require('firebase-functions')
2
3module.exports = {
4 hello: functions.https.onRequest((req, res) => {
5 const subject = req.query.subject || 'World'
6 res.send(`Hello ${subject}!`)
7 }),
8}

Every property of that object you're exporting will be an individual function. So with this, we have a single function called hello.

You might notice other folks use this syntax:

1exports.hello = functions.https.onRequest((req, res) => {})

These are functionally equivalent. I just happen to prefer module.exports = {}. But it doesn't make any substantive difference.

Also this file doesn't have to be called index.js, but if you change it to something else, then you'll need to set that in the main field in your package.json. Easier to just go with the index.js convention I say.

Step 8: Install firebase-tools

To deploy locally, you'll need to have firebase-tools installed so you can use the firebase CLI:

1npm install --global firebase-tools

Now you can login:

1firebase login

That should pop open your browser and allow you to login with your Google account. Go ahead and login. When you're done, you'll be logged in locally.

Terminal output from the firebase login command

Step 9: Test locally

Now that you're logged in, you can test your firebase function before deploying it to production. You can do so with firebase emulator which is pretty sweet.

From within your project directory, run the following command:

1firebase emulators:start

That does a bunch of stuff, but here's the important output:

1i emulators: Starting emulators: functions
2... some irrelevant stuff here ...
3✔ functions[hello]: http function initialized (http://localhost:5001/{your-project-id}/us-central1/hello).
4
5┌───────────────────────────────────────────────────────────────────────┐
6│ ✔ All emulators ready! View status and logs at http://localhost:4000 │
7└───────────────────────────────────────────────────────────────────────┘
8
9┌───────────┬────────────────┬─────────────────────────────────┐
10│ Emulator │ Host:Port │ View in Emulator UI │
11├───────────┼────────────────┼─────────────────────────────────┤
12│ Functions │ localhost:5001 │ http://localhost:4000/functions │
13└───────────┴────────────────┴─────────────────────────────────┘
14 Other reserved ports: 4400, 4500

If you open the Emulator UI, you'll see the emulator overview and you can select the function logs:

Emulator overview

If you hit your serverless function in a browser you should see the output:

Browser saying "Hello World!"

And then your logs will show that the function was run:

Function logs

Awesome! So the function is working! On to the next step.

Step 10: Deploy locally (maybe?)

Now you're ready to deploy your function to the world wide web. From within your project directory, run the following command:

1firebase deploy

This will trigger your function to be deployed.... But wait! What's this I see?

1Error: HTTP Error: 400, Billing account for project '189225252738' is not found.
2Billing must be enabled for activation of service(s)
3'cloudbuild.googleapis.com,containerregistry.googleapis.com' to proceed.

What the? What's that even mean? Well, it turns out that firebase could use an improvement in this error message with instructions on what to do next because this part was a real pain honestly. But here's what you need to do...

Step 11: Enable billing

So you'll go back to the firebase console for your project and navigate to "Usage and billing" and then go to the "Details & settings" tab:

Usage and billing settings page

Then you'll click "Modify plan" and then "Select plan" under "Blaze Pay as you go":

Firebase billing plans

Don't worry, Firebase has a generous free tier and (I don't think) you should be charged for anything until you actually start using the function a lot. But at this point it will have you set up a billing account at console.cloud.google.com/billing so you can have a credit card on file.

Once you have that figured out, then you can continue to actually deploy your function.

Step 12: Deploy locally (for real now)

Now that your account is configured for billing, you can try to deploy again:

1firebase deploy

It will take a little bit of time, but when it's finally finished, you should get output like this:

1=== Deploying to '{your-project-id}'...
2
3i deploying functions
4i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
5i functions: ensuring required API cloudbuild.googleapis.com is enabled...
6✔ functions: required API cloudbuild.googleapis.com is enabled
7✔ functions: required API cloudfunctions.googleapis.com is enabled
8i functions: preparing functions directory for uploading...
9i functions: packaged functions (23.46 KB) for uploading
10✔ functions: functions folder uploaded successfully
11i functions: creating Node.js 12 function hello(us-central1)...
12✔ functions[hello(us-central1)]: Successful create operation.
13Function URL (hello): https://us-central1-{your-project-id}.cloudfunctions.net/hello
14
15✔ Deploy complete!
16
17Project Console: https://console.firebase.google.com/project/{your-project-id}/overview

And if you hit the endpoint they give you there, you should be able get your function's output. For extra fun, our function handles providing a custom subject, so if you add ?subject=Bob%20Ross to the end of that URL, you get a nice greeting. Hello there Bob Ross!

Browser saying "Hello Bob Ross!"

Step 13: Auto-deploy Firebase Cloud Functions with GitHub Actions

I don't know about you, but I don't like deploying from my local machine. I prefer a continuous integration server to do that for me. Luckily, our project is part of a GitHub repo and we can use GitHub actions to do this for us.

First, create a .github/workflows/deploy.yml file and put this in it:

1name: deploy
2on:
3 push:
4 branches:
5 - main
6jobs:
7 main:
8 runs-on: ubuntu-latest
9 steps:
10 - name: ⬇️ Checkout repo
11 uses: actions/checkout@v2
12
13 - name: ⎔ Setup node
14 uses: actions/setup-node@v1
15 with:
16 node-version: 12
17
18 - name: 📥 Download deps
19 working-directory: functions
20 run: npm install
21
22 - name: 🚀 Deploy
23 run: npx firebase-tools deploy --token "$FIREBASE_TOKEN"
24 env:
25 FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}

Here's what this does when you push the main branch (or master if you're still using master as your default branch name):

  1. Clones the repo
  2. Installs Node v12
  3. Installs dependencies via npm in the functions directory
  4. Deploys to firebase using firebase-tools just like we did

For that last step it's doing a few things differently:

  1. It's using npx so we don't have to worry about installing things globally
  2. It's using the --token flag so we don't have to worry about logging in
  3. It's exposing a FIREBASE_TOKEN environment variable.

Let's get that firebase token set up.

Step 14: Create a Firebase token for CI

Run this:

1firebase login:ci

This will take you through the login process once again, when you've finished, it will give you the token in the terminal output:

1✔ Success! Use this token to login on a CI server:
2
31//06SeHVPB6AOF9CgYIARAAGAYSNwF-L9IrcdTvBfmn_ako1m3iJXRNpyV6tROQ193ZTGUtwLB95aoCC1l45ES40dRYfa5YjNCRxRc
4
5Example: firebase deploy --token "$FIREBASE_TOKEN"

And don't even try using that token, I've already revoked it 😜 Get your own!

1firebase logout --token 1//06SeHVPB6AOF9CgYIARAAGAYSNwF-L9IrcdTvBfmn_ako1m3iJXRNpyV6tROQ193ZTGUtwLB95aoCC1l45ES40dRYfa5YjNCRxRc
2✔ Logged out token "1//06SeHVPB6AOF9CgYIARAAGAYSNwF-L9IrcdTvBfmn_ako1m3iJXRNpyV6tROQ193ZTGUtwLB95aoCC1l45ES40dRYfa5YjNCRxRc"

Step 15: Add Firebase token to GitHub secrets

Great, now you'll copy that token into your GitHub repository's secrets configuration: https://github.com/{your-username}/{your-repo-name}/settings/secrets/actions

GitHub secrets UI adding the firebase token

With that added, you're ready to deploy!

Step 16: Commit and push

Now commit and push all of your changes:

1git add -A
2git commit -m "firebase functions rock"
3git push

Then go to your repo's actions page (https://github.com/{your-username}/{your-repo-name}/actions) and watch the deploy happen automatically!

Action output showing the deploy

Then celebrate 🎉

Conclusion

Firebase Cloud Functions are super powerful. You can find the finished repo here. Hopefully getting this super simple start to Firebase functions was helpful to you! Good luck and may your functions be functional! ✌

Discuss on TwitterEdit post on GitHub

Share article
Kent C. Dodds

Kent C. Dodds is a JavaScript software engineer and teacher. He's taught hundreds of thousands of people how to make the world a better place with quality software development tools and practices. He lives with his wife and four kids in Utah.

Join the Newsletter



Kent C. Dodds