Skip to content

add firebase-functions-basics#15

Open
jhuleatt wants to merge 13 commits intofirebase:mainfrom
jhuleatt:jhuleatt-functions
Open

add firebase-functions-basics#15
jhuleatt wants to merge 13 commits intofirebase:mainfrom
jhuleatt:jhuleatt-functions

Conversation

@jhuleatt
Copy link
Copy Markdown

@jhuleatt jhuleatt commented Feb 20, 2026

Add skills for Cloud Functions for Firebase

Fixes #9

@inlined
Copy link
Copy Markdown
Member

inlined commented Feb 24, 2026

I have an entirely different skill on migrating from configs to params. Maybe we should not even mention config except as instructions to always use params instead? Can you ping me tomorrow about this?

@jhuleatt
Copy link
Copy Markdown
Author

I have an entirely different skill on migrating from configs to params. Maybe we should not even mention config except as instructions to always use params instead? Can you ping me tomorrow about this?

@inlined, in the interest of getting this out in the hands of developers, want to either (1) keep upgrade.md for now, and then overwrite it with your own PR or update it to reference a new separate skill, or (2) delete upgrade.md and merge the rest of the skill?

@jhuleatt
Copy link
Copy Markdown
Author

@inlined I've removed upgrade.md and all references to functions.config, and renamed the skill to firebase-functions-basic. LGTY?

@jhuleatt jhuleatt changed the title Add Cloud Functions for Firebase add firebase-functions-basics Feb 26, 2026
Copy link
Copy Markdown

@egilmorez egilmorez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some thoughts for you Jeff, thanks!

Copy link
Copy Markdown
Member

@inlined inlined left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we teach the model that --functions is used when only emulating functions? A lot of functions depend on event sources and I almost never run only --functions.

We might also want to add the genkit UI in the npx concurrently call

Copy link
Copy Markdown
Member

@lahirumaramba lahirumaramba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, Jeff! Admin SDK sections LGTM!

@jhuleatt
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a new skill for Cloud Functions for Firebase, including guides for Node.js and Python. The documentation is a good start, but there are several critical and high-severity issues in the provided code examples and setup instructions. The Node.js example incorrectly mixes v1 and v2 function APIs, which will not work. The Python example has a bug in how it sets runtime options and also contains confusing, unused code. Additionally, some of the general guidance on SDK versions and import paths is inaccurate. I've provided suggestions to correct these issues to ensure the guides are accurate and functional.

Comment on lines +20 to +47
import { setGlobalOptions, onInit } from "firebase-functions";
import { onRequest } from "firebase-functions/https";
import { onDocumentCreated } from "firebase-functions/firestore";
import * as logger from "firebase-functions/logger";
import { defineString, defineInt } from "firebase-functions/params";

import { initializeApp } from "firebase-admin/app";

// Configurable parameters
const scaleLimit = defineInt("MAX_INSTANCES", { default: 1 });
const greeting = defineString("GREETING", { default: "Hello" });

onInit(() => {
initializeApp();
setGlobalOptions({ maxInstances: scaleLimit });
});

export const helloWorld = onRequest(async (request, response) => {
logger.info("Request received!", request);
response.send(`${greeting.value()} from Firebase!`);
});

export const newDoc = onDocumentCreated(
"/words/{wordId}",
async (event) => {
logger.info("New word!", event.data?.data());
}
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The provided code snippet for a 2nd-gen function is incorrect as it mixes APIs from both v1 and v2 of Cloud Functions, which will lead to errors. Specifically:

  • Imports for onRequest and onDocumentCreated should be from firebase-functions/v2/... subpaths for 2nd-gen functions.
  • The onInit lifecycle hook is for v1 functions and should not be used.
  • setGlobalOptions should be imported from firebase-functions/v2 and called at the top level.
  • Logging the entire request object can be a security risk and produce noisy logs. It's better to log specific, non-sensitive properties.

Here is a corrected version of the snippet that properly implements 2nd-gen functions:

import { onRequest } from "firebase-functions/v2/https";
import { onDocumentCreated } from "firebase-functions/v2/firestore";
import * as logger from "firebase-functions/logger";
import { defineString, defineInt } from "firebase-functions/params";
import { initializeApp } from "firebase-admin/app";
import { setGlobalOptions } from "firebase-functions/v2";

initializeApp();

// Configurable parameters
const scaleLimit = defineInt("MAX_INSTANCES", { default: 1 });
const greeting = defineString("GREETING", { default: "Hello" });

setGlobalOptions({ maxInstances: scaleLimit });

export const helloWorld = onRequest(async (request, response) => {
  logger.info("Request received!", { body: request.body });
  response.send(`${greeting.value()} from Firebase!`);
});

export const newDoc = onDocumentCreated(
  "/words/{wordId}",
  async (event) => {
    logger.info("New word!", event.data?.data());
  }
);

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is your source for that information? The default import has been 2nd gen for a while now, so I want to correct the source you're using.

Comment on lines +18 to +25
admin_app = None

@init
def initialize():
options.set_global_options(max_instances=SCALE_LIMIT)

global admin_app
admin_app = initialize_app()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

There are a couple of issues in the initialize function:

  1. The admin_app global variable is assigned but never used, which is confusing. Calling initialize_app() is sufficient as it sets up the default app instance.
  2. The SCALE_LIMIT parameter is an IntParam object. To use its integer value for max_instances, you must call the .value() method. Passing the object directly will cause a runtime error.
Suggested change
admin_app = None
@init
def initialize():
options.set_global_options(max_instances=SCALE_LIMIT)
global admin_app
admin_app = initialize_app()
@init
def initialize():
options.set_global_options(max_instances=SCALE_LIMIT.value())
initialize_app()

admin_app = initialize_app()

@https_fn.on_request(
cors=options.CorsOptions(cors_origins="*", cors_methods=["get", "post"])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using a wildcard * for cors_origins allows any origin to access your function, which can be a security risk. For production environments, it is strongly recommended to restrict this to a specific list of allowed domains. For a guide, it's better to show a more secure example or add a note about this.

Suggested change
cors=options.CorsOptions(cors_origins="*", cors_methods=["get", "post"])
cors=options.CorsOptions(cors_origins=["https://your-web-app.com"], cors_methods=["get", "post"])

Copy link
Copy Markdown
Member

@joehan joehan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nits, but this looks very good to me!

);
```

**Key points for the agent:**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Key points for the agent:**
**Key points:**

Its all for the agent

);
```

**Key points for the agent:**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
**Key points for the agent:**
**Key points:**

Its all for the agent

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skills for Cloud Functions

5 participants