Firebase Cloud Functions Intro
Cloud Functions let you run server-side code without managing a server. You write a JavaScript or TypeScript function, deploy it to Firebase, and Firebase runs it on Google's infrastructure whenever triggered. The server starts automatically when needed and scales to handle any load.
Why You Need Server-Side Code
Some tasks must run on a server, not in a browser. Browser code is visible to users and cannot be trusted with sensitive operations:
- Charging credit cards — requires a secret API key users must never see
- Sending emails — uses server credentials
- Running tasks on a schedule — no user is present to trigger them
- Responding to database changes — needs to react to Firestore events automatically
- Validating data before it reaches your database — adds a trust layer
Cloud Functions handle all of these without requiring you to set up, maintain, or pay for a server that runs continuously.
The Vending Machine Analogy
A vending machine sits dormant until someone presses a button. When pressed, it wakes up, dispenses the item, collects payment, and goes idle again. You do not pay for the machine to run continuously — only for each use.
Cloud Functions work the same way. They sleep until a trigger fires (an HTTP request, a database change, a scheduled time). They wake up, execute, and go idle. You pay only for the execution time.
Types of Cloud Function Triggers
Trigger Type | Fires When ---------------------|------------------------------------------ HTTP trigger | Someone calls a URL (like an API endpoint) Firestore trigger | A Firestore document is created/updated/deleted Auth trigger | A user account is created or deleted Storage trigger | A file is uploaded or deleted in Cloud Storage Pub/Sub trigger | A message is published to a topic Scheduled trigger | A set time arrives (like a cron job) Realtime DB trigger | Realtime Database data changes
Setting Up Cloud Functions
Cloud Functions require the Firebase CLI. Install it globally:
npm install -g firebase-tools
Log in to Firebase:
firebase login
Initialize functions in your project folder:
firebase init functions
Firebase asks whether to use JavaScript or TypeScript, and whether to install dependencies. Select your preferences and Firebase creates a functions/ folder in your project.
The Functions Folder Structure
my-app/ | +-- functions/ | +-- index.js (or index.ts) <-- your function code | +-- package.json | +-- .eslintrc.js | +-- firebase.json +-- .firebaserc
All functions are exported from index.js. Firebase discovers them from this file during deployment.
A Simple Hello World Function
// functions/index.js
const { onRequest } = require("firebase-functions/v2/https");
exports.helloWorld = onRequest((request, response) => {
response.send("Hello from Firebase Cloud Functions!");
});
Deploy this function:
firebase deploy --only functions
After deployment, Firebase gives you a URL like:
https://helloworld-abc123-uc.a.run.app
Open that URL in a browser and you see "Hello from Firebase Cloud Functions!"
Firebase Functions v1 vs v2
Firebase Functions supports two API generations. Version 2 (the current recommended version) runs on Cloud Run, supports larger memory allocations, and offers better cold start performance:
// v1 style (older)
const functions = require("firebase-functions");
exports.myFunc = functions.https.onRequest(handler);
// v2 style (current, recommended)
const { onRequest } = require("firebase-functions/v2/https");
exports.myFunc = onRequest(handler);
Use v2 for all new functions. The v1 API still works but v2 receives new features and improvements.
The Functions Emulator
Testing deployed functions is slow and costs money. The Firebase Emulator runs functions locally on your machine:
firebase emulators:start --only functions
The emulator gives you a local URL for each function. Changes to your code take effect after restarting the emulator. The emulator also connects to Firestore and Auth emulators so you can test complete workflows locally.
Billing for Cloud Functions
Cloud Functions require the Blaze (pay-as-you-go) plan. The free tier of Blaze includes 2 million function invocations per month and 400,000 GB-seconds of compute. Most small apps stay within the free tier. You only pay if your app exceeds those limits.
Key Takeaway
Cloud Functions run server-side code without managing servers. They trigger on HTTP requests, database changes, scheduled times, and other Firebase events. Install the Firebase CLI, run firebase init functions, write your functions in functions/index.js, and deploy with firebase deploy --only functions. Use the local emulator during development to avoid deployment delays and costs. Always use v2 function syntax for new projects.
