📖 Scenario: You are building a simple app using Firebase. You want to add custom backend logic without managing servers.
🎯 Goal: Learn how to create and deploy a serverless function in Firebase to extend app capabilities.
📋 What You'll Learn
Create a Firebase Cloud Function that responds to HTTP requests
Add a configuration variable to customize the function behavior
Implement the core logic to handle the request and send a response
Deploy the function with proper export to Firebase
💡 Why This Matters
🌍 Real World
Serverless functions let you add backend features to your Firebase app without managing servers. This is useful for sending emails, processing data, or customizing responses.
💼 Career
Understanding serverless functions is important for cloud developers and backend engineers working with Firebase or other cloud platforms.
Progress0 / 4 steps
1
Set up a basic Firebase Cloud Function
Create a Firebase Cloud Function named helloWorld that responds to HTTP requests and sends back the text 'Hello from Firebase!'. Use functions.https.onRequest and the parameters request and response.
Firebase
Hint
Use exports.helloWorld = functions.https.onRequest((request, response) => { ... }) to create the function.
2
Add a configuration variable for greeting
Add a variable named greeting and set it to the string 'Hello from Firebase Functions!'. Use this variable inside the helloWorld function to send the response instead of the fixed string.
Firebase
Hint
Define const greeting = 'Hello from Firebase Functions!' before the function and use it in response.send(greeting).
3
Implement core logic to customize greeting with query parameter
Inside the helloWorld function, check if the HTTP request has a query parameter named name. If it exists, send back 'Hello, <name>!' where <name> is the parameter value. Otherwise, send the default greeting.
Firebase
Hint
Use const name = request.query.name and an if statement to check it.
4
Export and deploy the function properly
Ensure the helloWorld function is exported using exports.helloWorld so Firebase can deploy it. The full code should be ready for deployment.
Firebase
Hint
Make sure the function is exported with exports.helloWorld = ....
Practice
(1/5)
1. What is the main benefit of using serverless functions with Firebase?
easy
A. They only work on mobile devices.
B. You need to set up and maintain your own servers.
C. They replace the Firebase database completely.
D. You can run backend code without managing servers.
Solution
Step 1: Understand serverless functions
Serverless functions let you write backend code that runs automatically without you managing servers.
Step 2: Compare options
Serverless functions do not require server management, do not replace the database, and work on all platforms.
Final Answer:
You can run backend code without managing servers. -> Option D
Quick Check:
Serverless = no server management [OK]
Hint: Serverless means no servers to manage [OK]
Common Mistakes:
Thinking serverless means no backend code
Confusing serverless with database replacement
Believing serverless only works on mobile
2. Which of the following is the correct way to define a Firebase serverless function triggered by a database write?
D. functions.database.ref('/path').write((change, context) => { /* code */ });
Solution
Step 1: Recall Firebase function syntax
The correct syntax uses exports to define the function and functions.database.ref(path).onWrite() to trigger on database writes.
Step 2: Check each option
exports.myFunc = functions.database.ref('/path').onWrite((change, context) => { /* code */ }); matches the correct syntax. The other options have incorrect method calls or missing exports.
But it never logs anything when the database updates. What is the likely problem?
medium
A. onUpdate is not a valid trigger method.
B. The database path '/items/{id}' is invalid syntax.
C. The function is missing a return statement or promise.
D. console.log cannot be used in Firebase functions.
Solution
Step 1: Check function structure
Firebase functions should return a promise or value to signal completion. Missing return can cause silent failures.
Step 2: Validate other options
Path syntax is correct, console.log works for debugging, and onUpdate is valid trigger.
Final Answer:
The function is missing a return statement or promise. -> Option C
Quick Check:
Return missing causes no execution [OK]
Hint: Always return or await in Firebase functions [OK]
Common Mistakes:
Forgetting to return or await async work
Assuming console.log doesn't work
Misreading trigger method names
5. You want to extend your Firebase app to send a welcome email only when a new user signs up and their email is verified. Which approach best uses serverless functions to achieve this?
hard
A. Use a database trigger on user data write, ignoring email verification status.
B. Use an auth trigger on user creation, check if emailVerified is true, then send email.
C. Send emails directly from the client app after signup.
D. Use a scheduled function to send emails to all users daily.
Solution
Step 1: Identify correct trigger and condition
Auth triggers on user creation allow checking user properties like emailVerified immediately.
Step 2: Evaluate options for sending email
Use an auth trigger on user creation, check if emailVerified is true, then send email, which checks emailVerified before sending, ensuring only verified users get emails. The other options do not meet the requirement precisely.
Final Answer:
Use an auth trigger on user creation, check if emailVerified is true, then send email. -> Option B
Quick Check:
Auth trigger + emailVerified check = Use an auth trigger on user creation, check if emailVerified is true, then send email. [OK]
Hint: Check emailVerified in auth trigger before emailing [OK]
Common Mistakes:
Sending emails without verifying email status
Using database triggers for auth events
Sending emails from client risking security
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.