Get Started With Cloud Functions
Learn to write, deploy and delete a Cloud Function.
As stated in the previous lesson, the command to initialize Cloud Functions also creates some files and folders in our editor. Let’s go over the most important ones:
File/Folder | Description |
| This file is created in the root of the project directory. It defines the functions/hosting configurations of the project. |
| This is the new directory created in the root of the project containing all Cloud Functions code. |
| This is an npm package file used by Node to describe the project. It contains dependencies required to run Cloud Functions. All other modules and packages to be used in our Functions will also go here. |
| This is the main source file where we write/define all Cloud Functions. It comes with a sample |
| This is the folder contained in the |
Here is an illustration to help us visualise the files and folders created:
The Firebase Admin SDK
The Admin SDK is a set of libraries available on Firebase that allows developers to interact with Firebase and Firebase services from privileged or trusted environments. With this, developers can perform user management operations. We can read and write data to a database or storage with full admin privileges.
To use the Admin SDK, we neede the
firebase-admin
module in our Node.js environment and then initialize the SDK. Then, we can access the Firebase services we need as JavaScript methods on the initialized Admin SDK:
const admin = require("firebase-admin");admin.initializeApp();// use Firestore with Admin SDKconst firestore = admin.firestore()// use Realtime database with Admin SDKconst database = admin.database()
Explanation:
- Line 1: Here we fetch the
firebase-admin
module and assign it to anadmin
constant. This module, alongside thefirebase-functions
module, was one of the two modules created when we initialized Cloud Functions in our project. It is also required to interact with Firebase services. -** Line 3:** We initialize the Firebase Admin SDK with theinitializeApp
function. - Lines 6–9: We access Cloud Firestore and the Realtime