Skip to content

Commit 90899a8

Browse files
committed
address review comments
1 parent 94fdcef commit 90899a8

File tree

6 files changed

+83
-95
lines changed

6 files changed

+83
-95
lines changed

.guides/config.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"description": "A set of guides for Cloud Functions for Firebase.",
2+
"description": "Use this library to build serverless functions for event triggers and HTTP using Firebase and Cloud Run Functions",
33
"mcpServers": {
44
"firebase": {
5-
"command": "firebase",
6-
"args": ["mcp:server"]
5+
"command": "npx",
6+
"args": ["-y", "firebase-tools@latest", "experimental:mcp"]
77
}
88
}
99
}

.guides/examples/index.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

.guides/setup.md

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,53 @@
1-
# Firebase Functions Setup Guide
1+
# Cloud Functions for Firebase setup guide
22

33
This guide provides a step-by-step process for setting up a new Firebase Functions project, tailored for a coding agent.
44

5-
### **Step 1: Initialize Firebase Project**
5+
## 1. Create a 2nd-gen HTTP function
66

7-
If you haven't already, initialize your Firebase project. The agent should have the `firebase-tools` CLI installed.
8-
9-
```bash
10-
# One-time setup for a new project
11-
firebase login
12-
firebase init functions
13-
```
14-
During initialization, select `TypeScript`.
15-
16-
### **Step 2: Install Dependencies**
17-
18-
Navigate to the `functions` directory and install the necessary NPM packages.
19-
20-
```bash
21-
cd functions
22-
npm install
23-
```
24-
25-
### **Step 3: Create a v2 HTTP Function**
26-
27-
Replace the contents of `src/index.ts` with the following code to create a simple, modern v2 HTTP endpoint.
7+
Replace the contents of `src/index.ts` (or `index.js`) with the following code to create a simple, modern v2 HTTP endpoint.
288

299
```typescript
30-
import {onRequest} from "firebase-functions/v2/https";
10+
import {onRequest} from "firebase-functions/https";
3111
import * as logger from "firebase-functions/logger";
3212
import {defineString} from "firebase-functions/params";
3313

3414
// Define a configuration parameter for a greeting
3515
const greeting = defineString("GREETING", {default: "Hello"});
3616

37-
export const helloWorld = onRequest((request, response) => {
38-
logger.info("Request received!", {structuredData: true});
39-
response.send(`${greeting.value()} from Firebase!`);
40-
});
17+
export const helloWorld = onRequest(
18+
{ maxInstances: 1 },
19+
async (request, response) => {
20+
logger.info("Request received!", { structuredData: true });
21+
response.send(`${greeting.value()} from Firebase!`);
22+
},
23+
);
4124
```
4225

4326
**Key points for the agent:**
44-
* Always import from `firebase-functions/v2/*` for new functions.
27+
* Always import from `firebase-functions/*` for new functions.
4528
* Use `logger` for structured logging.
4629
* Use `defineString`, `defineInt`, `defineSecret` for environment configuration instead of `functions.config()`.
4730

48-
### **Step 4: Build TypeScript**
31+
## 2. Build TypeScript
4932

5033
Compile your TypeScript code to JavaScript.
5134

5235
```bash
5336
npm run build
5437
```
5538

56-
### **Step 5: Local Development and Testing**
39+
## 3. Local Development and Testing
5740

5841
Use the Firebase Emulators to test your function locally before deploying.
5942

43+
Tell the user to run the following command in a separate terminal window to start the emulators:
6044
```bash
6145
# Start the functions emulator
6246
firebase emulators:start --only functions
6347
```
64-
The agent can then interact with the function at the local URL provided by the emulator.
48+
The user can then interact with the function at the local URL provided by the emulator.
6549

66-
### **Step 6: Deploy to Firebase**
50+
## 4. Deploy to Firebase
6751

6852
Once testing is complete, deploy the function to your Firebase project.
6953

.guides/upgrade.md

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,48 @@
1-
# Upgrading Firebase Functions to 2nd Gen
1+
# Upgrading a Firebase Function to 2nd Gen
22

3-
This guide summarizes the process of migrating Cloud Functions from 1st to 2nd generation. You can migrate functions incrementally, running both generations side-by-side.
3+
This guide provides a step-by-step process for migrating a single Cloud Function from 1st to 2nd generation. Migrate functions incrementally, running both generations side-by-side.
44

5-
## 1. Update Dependencies
5+
## 1. Identify a v1 function to upgrade
66

7-
Update your `firebase-functions` and `firebase-admin` SDKs, and ensure you are using a recent version of the Firebase CLI.
8-
9-
## 2. Modify Imports
10-
11-
Update your import statements to use the `v2` subpackage.
7+
Let's say you have a 1st gen HTTP function like this:
128

139
**Before (1st Gen):**
1410
```typescript
1511
import * as functions from "firebase-functions";
16-
```
1712

18-
**After (2nd Gen):**
19-
```typescript
20-
import * as functions from "firebase-functions/v2";
13+
export const webhook = functions.https.onRequest((request, response) => {
14+
// ...
15+
});
2116
```
2217

23-
## 3. Update Trigger Definitions
18+
Now, let's upgrade it to 2nd gen.
2419

25-
The SDK is now more modular. Update your trigger definitions accordingly.
20+
## 2. Update Dependencies
2621

27-
### HTTP Triggers
22+
Ensure your `firebase-functions` and `firebase-admin` SDKs are up-to-date, and you are using a recent version of the Firebase CLI.
2823

29-
**Before (1st Gen):**
24+
## 3. Modify Imports
25+
26+
Update your import statements to use the top-level modules.
27+
28+
**After (2nd Gen):**
3029
```typescript
31-
export const webhook = functions.https.onRequest((request, response) => {
32-
// ...
33-
});
30+
import {onRequest} from "firebase-functions/https";
3431
```
3532

33+
## 4. Update Trigger Definition
34+
35+
The SDK is now more modular. Update your trigger definition accordingly.
36+
3637
**After (2nd Gen):**
3738
```typescript
38-
import {onRequest} from "firebase-functions/v2/https";
39-
4039
export const webhook = onRequest((request, response) => {
4140
// ...
4241
});
4342
```
4443

44+
Here are other examples of trigger changes:
45+
4546
### Callable Triggers
4647

4748
**Before (1st Gen):**
@@ -53,7 +54,7 @@ export const getprofile = functions.https.onCall((data, context) => {
5354

5455
**After (2nd Gen):**
5556
```typescript
56-
import {onCall} from "firebase-functions/v2/https";
57+
import {onCall} from "firebase-functions/https";
5758

5859
export const getprofile = onCall((request) => {
5960
// ...
@@ -64,23 +65,23 @@ export const getprofile = onCall((request) => {
6465

6566
**Before (1st Gen):**
6667
```typescript
67-
export consthellopubsub = functions.pubsub.topic("topic-name").onPublish((message) => {
68+
export const hellopubsub = functions.pubsub.topic("topic-name").onPublish((message) => {
6869
// ...
6970
});
7071
```
7172

7273
**After (2nd Gen):**
7374
```typescript
74-
import {onMessagePublished} from "firebase-functions/v2/pubsub";
75+
import {onMessagePublished} from "firebase-functions/pubsub";
7576

76-
export consthellopubsub = onMessagePublished("topic-name", (event) => {
77+
export const hellopubsub = onMessagePublished("topic-name", (event) => {
7778
// ...
7879
});
7980
```
8081

81-
## 4. Use Parameterized Configuration
82+
## 5. Use Parameterized Configuration
8283

83-
Migrate from `functions.config()` to the new `params` module for environment configuration. This provides strong typing and validation.
84+
Migrate from `functions.config()` to the new `params` module for environment configuration.
8485

8586
**Before (`.runtimeconfig.json`):**
8687
```json
@@ -114,7 +115,7 @@ const SOMESERVICE_KEY = defineSecret("SOMESERVICE_KEY");
114115
```
115116
You will be prompted to set the value on deployment, which is stored securely in Cloud Secret Manager.
116117

117-
## 5. Update Runtime Options
118+
## 6. Update Runtime Options
118119

119120
Runtime options are now set directly within the function definition.
120121

@@ -132,7 +133,7 @@ export const func = functions
132133

133134
**After (2nd Gen):**
134135
```typescript
135-
import {onRequest} from "firebase-functions/v2/https";
136+
import {onRequest} from "firebase-functions/https";
136137

137138
export const func = onRequest(
138139
{
@@ -145,10 +146,12 @@ export const func = onRequest(
145146
);
146147
```
147148

148-
## 6. Traffic Migration
149+
## 7. Traffic Migration
149150

150151
To migrate traffic safely:
151152
1. Rename your new 2nd gen function with a different name.
152-
2. Deploy it alongside the old 1st gen function.
153-
3. Gradually introduce traffic to the new function (e.g., via client-side changes or by calling it from the 1st gen function).
154-
4. Once you are confident, you can delete the 1st gen function.
153+
2. Comment out any existing `minInstances` or `maxInstances` config and instead set `maxInstances` to `1` while testing.
154+
3. Deploy it alongside the old 1st gen function.
155+
4. Gradually introduce traffic to the new function (e.g., via client-side changes or by calling it from the 1st gen function).
156+
5. Add back the original `minInstances` and `maxInstances` settings to the 2nd gen function.
157+
6. Once you are confident, you can delete the 1st gen function.

.guides/usage.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
1-
This guide covers Firebase Functions SDK v6.0.0+.
1+
## Key Guidelines
22

3-
**Key Guidelines**
4-
* Always use v2 functions for new development.
3+
* Always use 2nd-gen functions for new development.
54
* Use v1 functions *only* for Analytics, basic Auth, and Test Lab triggers.
6-
* For SDK versions before 6.0.0, add `/v2` to import paths (e.g., `firebase-functions/v2/https`).
5+
* Use `firebase-functions` SDK version 6.0.0 and above
6+
* Use top-level imports (e.g., `firebase-functions/https`). These are 2nd gen by default.
7+
8+
## Configuration: Use Secret Params for API Keys
79

8-
**Configuration: Use Secret Params for API Keys**
910
For sensitive information like API keys (e.g., for LLMs, payment providers, etc.), **always** use `defineSecret`. This stores the value securely in Cloud Secret Manager.
1011

1112
```typescript
12-
import {onRequest} from 'firebase-functions/v2/https';
13+
import {onRequest} from 'firebase-functions/https';
1314
import {logger} from 'firebase-functions/logger';
1415
import {defineString, defineSecret} from 'firebase-functions/params';
1516

1617
// Securely define an LLM API key
1718
const LLM_API_KEY = defineSecret('LLM_API_KEY');
1819

1920
// Example function that uses the secret
20-
export const callLlm = onRequest({ secrets: [LLM_API_KEY] }, (req, res) => {
21+
export const callLlm = onRequest({ secrets: [LLM_API_KEY] }, async (req, res) => {
2122
const apiKey = LLM_API_KEY.value();
2223

2324
// Use the apiKey to make a call to the LLM service
2425
logger.info('Calling LLM with API key.');
26+
27+
// insert code here to call LLM...
2528

2629
res.send('LLM API call initiated.');
2730
});
2831
```
2932
When you deploy a function with `secrets`, the CLI will prompt you to enter the secret's value.
3033

31-
**Initializing the Firebase Admin SDK**
32-
To interact with Firebase services like Firestore, Auth, or RTDB from within your functions, you need to initialize the Firebase Admin SDK.
34+
## Use the Firebase Admin SDK
35+
To interact with Firebase services like Firestore, Auth, or RTDB from within your functions, you need to initialize the Firebase Admin SDK. Call `initializeApp` without any arguments so that Application Default Credentials are used.
3336

3437
1. **Install the SDK:**
3538
```bash
@@ -44,7 +47,7 @@ To interact with Firebase services like Firestore, Auth, or RTDB from within you
4447
```
4548
This should be done once at the top level of your `index.ts` file.
4649

47-
**Common Imports**
50+
## Common Imports
4851
```typescript
4952
// HTTPS, Firestore, RTDB, Scheduled, Storage, Pub/Sub, Auth, Logging, Config
5053
import {onRequest} from 'firebase-functions/https';
@@ -54,27 +57,27 @@ import {onSchedule} from 'firebase-functions/scheduler';
5457
import {onObjectFinalized} from 'firebase-functions/storage';
5558
import {onMessagePublished} from 'firebase-functions/pubsub';
5659
import {beforeUserSignedIn} from 'firebase-functions/identity';
57-
import {logger} from 'firebase-functions';
60+
import {logger, onInit} from 'firebase-functions';
5861
import {defineString, defineSecret} from 'firebase-functions/params';
5962
```
6063

61-
**v1 Functions (Legacy Triggers)**
62-
Use the `firebase-functions/v1` import for Analytics and basic Auth triggers.
64+
## 1st-gen Functions (Legacy Triggers)**
65+
Use the `firebase-functions/v1` import for Analytics and basic Auth triggers. These aren't supported in 2nd gen.
6366
```typescript
6467
import * as functionsV1 from 'firebase-functions/v1';
6568
6669
// v1 Analytics trigger
67-
export const onPurchase = functionsV1.analytics.event('purchase').onLog((event) => {
70+
export const onPurchase = functionsV1.analytics.event('purchase').onLog(async (event) => {
6871
logger.info('Purchase event', { value: event.params?.value });
6972
});
7073
7174
// v1 Auth trigger
72-
export const onUserCreate = functionsV1.auth.user().onCreate((user) => {
75+
export const onUserCreate = functionsV1.auth.user().onCreate(async (user) => {
7376
logger.info('User created', { uid: user.uid });
7477
});
7578
```
7679
77-
**Development Commands**
80+
## Development Commands
7881
```bash
7982
# Install dependencies
8083
npm install
@@ -83,7 +86,10 @@ npm install
8386
npm run build
8487
8588
# Run emulators for local development
89+
# Tell the user to run the following command to start the emulators:
90+
```bash
8691
firebase emulators:start --only functions
92+
```
8793
8894
# Deploy functions
8995
firebase deploy --only functions

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ Learn more about the Firebase SDK for Cloud Functions in the [Firebase documenta
1010

1111
Here are some resources to get help:
1212

13-
- Start with the quickstart: https://firebase.google.com/docs/functions/write-firebase-functions
14-
- Go through the guide: https://firebase.google.com/docs/functions/
15-
- Read the full API reference: https://firebase.google.com/docs/reference/functions/
16-
- Browse some examples: https://github.com/firebase/functions-samples
13+
- [Start with the quickstart](https://firebase.google.com/docs/functions/write-firebase-functions)
14+
- [Go through the guide](https://firebase.google.com/docs/functions/)
15+
- [Read the full API reference](https://firebase.google.com/docs/reference/functions/)
16+
- [Browse some examples](https://github.com/firebase/functions-samples)
1717

18-
If the official documentation doesn't help, try asking through our official support channels: https://firebase.google.com/support/
18+
If the official documentation doesn't help, try asking through our [official support channels](https://firebase.google.com/support/)
1919

2020
_Please avoid double posting across multiple channels!_
2121

0 commit comments

Comments
 (0)