Cloud Functions

Cloud Functions are serverless functions: we run code on servers operated by Google.

As it is a secure environment, we run sensitive tasks: authenticate requests, perform server-side validation, use API keys, make sensitive writes to the database, and more.

Functions trigger on spontaneous requests, or on events happening in the Firebase ecosystem, such as the registration of new users through Firebase Auth.

react to spontaneous requests: two options

The first option is to establish a bare-bones REST-API endpoint, called a HTTP function. It exposes a regular REST API endpoint, with an Express.js like API.

The second option is to establish a Callable function, a pattern that involves both a server SDK and a client SDK, which work hand in hand to provide a better developer experience, such as managing authentication.

onRequest and onCall are the two helpers to define those function:

import { onRequest, onCall } from "firebase-functions/https"

select and deploy functions

The main file, through the functions that it exports, determines the functions to be deployed. The main file is the one we set in package.json. It must be a JavaScript file:

{
    "main": "lib/index.js"
}

It is usually a barrel file that re-exports functions from their own files:

export { requestPlayer } from "./requestPlayer.js"

We deploy functions imperatively, all or a few:

firebase deploy --only functions
firebase deploy --only functions:requestPlayer
firebase deploy --only functions:requestPlayer,functions:requestPlanet

To delete a function, we remove it from the main file and run its deploy command. The CLI detects its absence and prompts us for confirmation.

define functions with TypeScript

We use a workflow that transpiles to JS since the main file must be JavaScript. The convention is to store TypeScript code in src/ and transpile towards lib/. The main file is lib/index.js.

tsconfig.json configures the transpilation, targeting the Node.js runtime:

{
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "nodenext",
        "outDir": "lib",
        "esModuleInterop": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "sourceMap": true,
        "strict": true,
        "target": "es2020"
    },
    "compileOnSave": true,
    "include": ["src"]
}

We ask the transpile to be continuous with the watch flag. Then, the emulator detects changes in the generated JS files, and updates the emulator services:

tsc -w

admin SDK

Within cloud functions, we interact with other Firebase services through the admin SDK. For example, we work with the project's Firestore database:

import { initializeApp } from "firebase-admin/app"
import { getFirestore } from "firebase-admin/firestore"

const app = initializeApp()
const db = getFirestore(app)
earlymorning logo

© Antoine Weber 2026 - All rights reserved

Cloud Functions

Cloud Functions are serverless functions: we run code on servers operated by Google.

As it is a secure environment, we run sensitive tasks: authenticate requests, perform server-side validation, use API keys, make sensitive writes to the database, and more.

Functions trigger on spontaneous requests, or on events happening in the Firebase ecosystem, such as the registration of new users through Firebase Auth.

react to spontaneous requests: two options

The first option is to establish a bare-bones REST-API endpoint, called a HTTP function. It exposes a regular REST API endpoint, with an Express.js like API.

The second option is to establish a Callable function, a pattern that involves both a server SDK and a client SDK, which work hand in hand to provide a better developer experience, such as managing authentication.

onRequest and onCall are the two helpers to define those function:

import { onRequest, onCall } from "firebase-functions/https"

select and deploy functions

The main file, through the functions that it exports, determines the functions to be deployed. The main file is the one we set in package.json. It must be a JavaScript file:

{
    "main": "lib/index.js"
}

It is usually a barrel file that re-exports functions from their own files:

export { requestPlayer } from "./requestPlayer.js"

We deploy functions imperatively, all or a few:

firebase deploy --only functions
firebase deploy --only functions:requestPlayer
firebase deploy --only functions:requestPlayer,functions:requestPlanet

To delete a function, we remove it from the main file and run its deploy command. The CLI detects its absence and prompts us for confirmation.

define functions with TypeScript

We use a workflow that transpiles to JS since the main file must be JavaScript. The convention is to store TypeScript code in src/ and transpile towards lib/. The main file is lib/index.js.

tsconfig.json configures the transpilation, targeting the Node.js runtime:

{
    "compilerOptions": {
        "module": "NodeNext",
        "moduleResolution": "nodenext",
        "outDir": "lib",
        "esModuleInterop": true,
        "noImplicitReturns": true,
        "noUnusedLocals": true,
        "sourceMap": true,
        "strict": true,
        "target": "es2020"
    },
    "compileOnSave": true,
    "include": ["src"]
}

We ask the transpile to be continuous with the watch flag. Then, the emulator detects changes in the generated JS files, and updates the emulator services:

tsc -w

admin SDK

Within cloud functions, we interact with other Firebase services through the admin SDK. For example, we work with the project's Firestore database:

import { initializeApp } from "firebase-admin/app"
import { getFirestore } from "firebase-admin/firestore"

const app = initializeApp()
const db = getFirestore(app)