Day 4 of 5
⏱ ~60 minutes
Firebase in 5 Days — Day 4

Cloud Functions

Write Node.js functions that trigger on Firestore events, auth events, and HTTP requests.

Cloud Functions

Cloud Functions run Node.js on Google's servers in response to events. No server to manage, scales automatically, only pay when they run.

Setup
npm install -g firebase-tools
firebase login
firebase init functions
# Choose: JavaScript or TypeScript
functions/index.js
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

// Firestore trigger: runs when a new post is created
exports.onPostCreated = functions.firestore
  .document('posts/{postId}')
  .onCreate(async (snap, context) => {
    const post = snap.data();
    // Notify the author's followers
    const followers = await db.collection('followers')
      .where('userId', '==', post.userId)
      .get();
    // Send notifications...
    console.log(`New post by ${post.userId}: ${post.title}`);
  });

// Auth trigger: runs on new user registration
exports.onUserCreated = functions.auth.user().onCreate(async (user) => {
  await db.collection('users').doc(user.uid).set({
    email: user.email,
    name: user.displayName || '',
    createdAt: admin.firestore.FieldValue.serverTimestamp()
  });
});

// HTTP function
exports.api = functions.https.onRequest((req, res) => {
  res.json({ message: 'Hello from Firebase!' });
});
Deploy and test
firebase deploy --only functions

# Test locally with emulator
firebase emulators:start
📝 Day 4 Exercise
Write a Cloud Function
  1. W
  2. r
  3. i
  4. t
  5. e
  6. a
  7. F
  8. i
  9. r
  10. e
  11. s
  12. t
  13. o
  14. r
  15. e
  16. t
  17. r
  18. i
  19. g
  20. g
  21. e
  22. r
  23. t
  24. h
  25. a
  26. t
  27. f
  28. i
  29. r
  30. e
  31. s
  32. w
  33. h
  34. e
  35. n
  36. a
  37. n
  38. e
  39. w
  40. p
  41. o
  42. s
  43. t
  44. i
  45. s
  46. c
  47. r
  48. e
  49. a
  50. t
  51. e
  52. d
  53. a
  54. n
  55. d
  56. l
  57. o
  58. g
  59. s
  60. i
  61. t
  62. .
  63. W
  64. r
  65. i
  66. t
  67. e
  68. a
  69. n
  70. a
  71. u
  72. t
  73. h
  74. t
  75. r
  76. i
  77. g
  78. g
  79. e
  80. r
  81. t
  82. h
  83. a
  84. t
  85. c
  86. r
  87. e
  88. a
  89. t
  90. e
  91. s
  92. a
  93. u
  94. s
  95. e
  96. r
  97. p
  98. r
  99. o
  100. f
  101. i
  102. l
  103. e
  104. d
  105. o
  106. c
  107. u
  108. m
  109. e
  110. n
  111. t
  112. i
  113. n
  114. F
  115. i
  116. r
  117. e
  118. s
  119. t
  120. o
  121. r
  122. e
  123. w
  124. h
  125. e
  126. n
  127. s
  128. o
  129. m
  130. e
  131. o
  132. n
  133. e
  134. s
  135. i
  136. g
  137. n
  138. s
  139. u
  140. p
  141. .
  142. T
  143. e
  144. s
  145. t
  146. w
  147. i
  148. t
  149. h
  150. t
  151. h
  152. e
  153. e
  154. m
  155. u
  156. l
  157. a
  158. t
  159. o
  160. r
  161. .

Day 4 Summary

  • Cloud Functions trigger on Firestore writes, auth events, HTTP requests, and more.
  • admin.firestore() in Cloud Functions has full database access — bypasses security rules.
  • Use the Firebase emulator for local testing. Deploy when it works.
  • admin.firestore.FieldValue.serverTimestamp() for server-side timestamps.
Finished this lesson?