Access Firebase Cloud Firestore using Google Cloud Functions - Part 1

February 03, 2020

In this tutorial, we will setup gloud SDK and Firebase CLI at the local machine.

Series

There are two parts to this series. Please read here for Part 2.

Prerequisites

  1. You need to know how to create a Firebase project; You can refer to the starter guide here.
  2. Familiar with JavaScript/TypeScript
  3. Familiar with Python

Overview

In this tutorial, we will set up gcloud SDK to access Cloud Firestore from our local machine. Otherwise, you will not be able to access Cloud Firestore.

  1. We first download gcloud SDK: You will be asked a few questions during this installation. By pressing Enter as default for most of the questions.
    $ curl https://sdk.cloud.google.com | bash
  2. We need to restart the shell by typing the following command.
    $ exec -l $SHELL
  3. Before you initialize gcloud, make sure you had set up your Firebase project at firebase console. Now we initialize our gclould SDK. You will be asked to select your account and the project you just created at Firebase console.

    $ gcloud init
  4. We want to log in to gcloud; You will be asked to log in to your account with a new webpage after you type the following command at the shell.
    $ gcloud auth application-default login

Setup Firebase CLI

Now we want to write cloud function to serve between your Could Firestore and your API call from other machines. I strongly suggest you watch this video series from Google to familiarize yourself with TypeScript and how the overall functions gonna look like.

Now, let’s start setting this up.

  1. We first just start by installing npm.
    $ sudo apt install nodejs
  2. You should check the installation of node and npm by.
    $ node --version
    $ npm --version
  3. We will now install Firebase CLI.
    $ sudo npm install -g firebase-tools
  4. We will create a folder for our project and then we go into the folder that you just created.
    $ mkdir firecast
    $ cd firecast
  5. We will log in to firebase at shell; you will be introduced with new pages if you haven't logged in yet.
    $ firebase login
  6. After authenticated, type in the following command to initialize. While you are running it, select Functions: Configure and deploy Cloud Functions using arrow key and space bar to select an item. Then select Firebase project you want to use. After that, Choose TypeScript as a language to use for this project. You want to choose TSLint to catch bugs and enforce style. Next, we select Yes to install dependencies with npm.
    $ firebase init

Next

We have set up gcloud SDK and Firebase CLI. Now we are ready to write some code. Let’s proceed to Part 2.