How to set up and deploy the firebase function?

Vikas Kohli
3 min readJul 30, 2020

Create the firebase project from the firebase console, if you have already created the project then skip that step.

Now we need to set up the Firebase CLI and node js in our system.

  1. Install NodeJs
  2. Now for the firebase CLI, we need to install via npm
npm install -g firebase-tools

3. We need to login into firebase

firebase login --interactive - for mine that command worksfor some user the below command also works
firebase login
If still problem persists, then please refer to the following issue, they have prescribed some i=options for the login
https://github.com/firebase/firebase-tools/issues/77

After successful login, you will see something like that

Firebase Login Success

Now we’re ready to initialize the firebase functions

firebase init functions

This will create functions folder and setup the base index file while initializing the Firebase function, we can use whether we need to go with typescript or javascript.

Firebase Project Setup

You will see they created the src folder inside there is index file and outside ts config, .gitignore, package.json files.

In the index.ts file, by default, they have provided one API of helloworld but they have commented that, so just uncomment that and here we’re ready with first firebase function api.

import * as functions from 'firebase-functions';// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const helloWorld = functions.https.onRequest((request, response) => {
functions.logger.info("Hello logs!", {structuredData: true});
response.send("Hello from Firebase!");
});

We run this in our local emulator by using the below command

firebase emulators:start

If we haven’t selected any project then we need to add the project flag like that

firebase emulators:start --project project-id

Now our emulators start, it will use two ports, one for API port and other for the emulator logs

firebase emulator run

By default for emulator port 4000 and API port 5001, if that ports some other next ports will be used.

My API URL will look like http://localhost:5001/MY_PROJECT_ID/us-central1/functionname

http://localhost:5001/my-project-id/us-central1/helloWorld
Firebase Function Api
Firebase Emulator

Now we can see that firebase emulator logs when I hit the API it shows in the emulator logs as well and in the terminal if anything is written on the console too.

Now we’re ready to deploy the firebase function to the server via the following command

firebase deploy --only functions --project my-project-id

Before deploy make sure you on the API in the Google Console

1.API cloudfunctions.googleapis.com is enabled…

2. API cloudbuild.googleapis.com is enabled…

and for this one, you need to make your account as billing account otherwise you can’t enable the API and indirectly you can deploy the functions.

After deploying, your API has the following URL

https://us-central1-MY_PROJECT_ID.cloudfunctions.net/helloWorld

Happy Deploying with Firebase 😊

--

--

Vikas Kohli

B.E Software Developer, Enthusiastic, Ego-surfing