# Migrate Firebase Functions from 1st to 2nd Gen

Yes. An agent rewrites your 1st gen Cloud Functions to the 2nd gen SDK, renames them so deploy does not collide, checks lint and types, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-to-v2-functions/firebase-functions/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the codebase before it rewrites a trigger. In a repository shaped like firebase/functions-samples, the 1st gen functions live under `Node-1st-gen/`, one directory per sample, each with a `functions/package.json` pinning `firebase-functions ^7.4.0` and `firebase-admin ^14.5.0`, `engines.node` of 22, an `eslint.config.js` and a `pnpm-lock.yaml`; `Node/` holds the 2nd gen versions of many of the same samples on Node 24. The root `package.json` drives both sets with pnpm: `bootstrap-1st-gen`, `lint-1st-gen`, `compile-1st-gen` (which copies `tsconfig.template.json`, `allowJs` and `checkJs` on, and runs `tsc --noEmit`) and `test-1st-gen`. `.github/workflows/test_node_1st_gen.yml` runs those four scripts on Node 22 for any change under `Node-1st-gen/`.

Reading a pair shows what changes. `Node-1st-gen/quickstarts/uppercase-firestore/functions/index.js` does `require('firebase-functions/v1')`, `functions.https.onRequest` and `functions.firestore.document('/messages/{documentId}').onCreate((snap, context) => ...)` with `context.params.documentId` and `functions.logger`. The 2nd gen file beside it in `Node/` imports `onRequest` from `firebase-functions/https`, `onDocumentCreated` from `firebase-functions/firestore` and `logger` from `firebase-functions`, takes a single `event`, reads `event.data.data()` and `event.params.documentId`, and the exports are lowercase, `addmessage` and `makeuppercase`, because a function cannot change generation under the same name: the Firebase CLI refuses to deploy a 2nd gen function over a 1st gen one, so the name changes or the old function is deleted first. The agent applies the same mapping across triggers: `functions.database.ref(...).onCreate` to `onValueCreated` with `event.data.val()`, `functions.pubsub.topic(...).onPublish` to `onMessagePublished`, and chained `runWith({...}).region(...)` into the options object that is the trigger's first argument, as `Node/quickstarts/https-time-server` does with `{timeoutSeconds: 1200, region: [...]}`.

For each function the agent picks the import path the CLI uses to decide the generation, rewrites the handler to the `event` shape, moves runtime options into the options argument or a `setGlobalOptions` call when every function in the codebase shares a region, and keeps the Admin SDK calls as they are. Where the 1st gen file uses the `cors` Express middleware by hand, as `https-time-server` does, the 2nd gen `onRequest` takes `cors: true` in its options instead and the dependency goes. It names each rewritten export as the `Node/` convention does and writes the old and new names into the pull request, because clients of an HTTP or callable function must switch URLs, and background triggers need both generations running until the old one is deleted.

Each rewritten directory then gets the three checks the workflow runs: `pnpm install` from the directory's lockfile, `npm run lint` with the local `eslint.config.js`, and `npm run compile`, which type-checks the plain JavaScript against the SDK's definitions and catches a handler still reading `snap.data()` or `context.params`. Where a `test` script exists, as in `Node/test-functions-jest` with `firebase-functions-test`, it runs that too. The agent does not deploy: there is no project or credential in the sandbox, and the pull request says so.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Migrate the 1st gen Cloud Functions in this repository to 2nd gen and open a pull request. Read the root package.json and its pnpm scripts, tsconfig.template.json, the 1st gen workflow, and every functions/index.js and functions/package.json under the 1st gen directory first; where a 2nd gen version of the same sample already exists, read it and match its conventions. For each function: switch the import from firebase-functions/v1 to the 2nd gen subpath the trigger belongs to (https, firestore, database, pubsub, scheduler, storage), rewrite the handler to the single event argument (event.data, event.params), move runWith and region settings into the options object or setGlobalOptions, replace hand-rolled cors middleware with the cors option on onRequest, and rename the export so it does not collide with the deployed 1st gen function. Keep Admin SDK calls unchanged. For every directory you change, run pnpm install, the lint script and the compile script, and any test script, and paste the output. Do not deploy. List old and new function names in the pull request and say which background triggers must run side by side before the old ones are deleted.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that changes `functions/index.js` in each migrated directory, trims `functions/package.json` where the `cors` dependency goes, and leaves the lockfiles consistent with `pnpm install`. The description has a table of old and new export names per directory, marks the HTTP and callable functions whose URL changes, and marks the Firestore, Realtime Database and Pub/Sub triggers that must run alongside their 1st gen versions until those are deleted. It quotes the `lint` and `compile` output per directory.

The `test_node_1st_gen.yml` workflow runs on the pull request because files under the 1st gen directory changed, and `test_node.yml` runs if anything under `Node/` moved. Both appear as checks.

A reviewer checks each `event.data` access against the trigger type (`.data()` for Firestore, `.val()` for Realtime Database, `.message.json` for Pub/Sub), confirms every `region` and `memory` setting survived the move into options, and plans the deploy: `firebase deploy --only functions:<newname>`, update clients, then `firebase functions:delete <oldname>`. The pull request does not deploy anything.

## Run it on every codebase still on 1st gen

A team with several Firebase projects sends one `POST /o/{org}/sessions` per repository with this prompt and that repository in `resources`. Each run finds the `firebase-functions/v1` imports in its own tree, so a repository already half-migrated only gets the remaining functions rewritten, and each run opens its own pull request. One repository per run; send a `request_id` so a retried call does not start a second run.

Give the API key a `webhook_url` and Rush POSTs `run.completed` with the `session_url` when each turn ends, so the rename table from every pull request can be gathered into one deploy plan.

## Why run it on Rush?

A generation migration is mechanical but unforgiving: one handler still reading `context.params` compiles as JavaScript and fails at the first event, and a function renamed wrongly collides at deploy. In a Rush sandbox the agent installs each directory's own lockfile, runs the repository's lint and `tsc --noEmit` type-check against the real SDK definitions, and the output stays in the run's record beside the diff, so a reviewer sees the check that caught the mistake rather than reading forty handlers by hand.

The work scales with the number of functions, not with the difficulty of each. Sending one request per repository runs them all at once, each in its own sandbox, and the pull requests arrive with the same rename convention and the same deploy notes.

## Frequently asked questions

### Why do the function names change?

Because a deployed function cannot switch generation in place: `firebase deploy` refuses a 2nd gen function with the name of an existing 1st gen one. The samples repository lowercases the name, `addMessage` to `addmessage`; the agent follows that convention and lists every pair so clients and the delete step can be planned.

### Can 1st and 2nd gen functions share one codebase?

Yes. One `index.js` can export both, and the CLI deploys each by its import path. That is how background triggers are moved safely: the new function runs beside the old one until it is verified, then the old one is deleted. Each individual function is one generation only.

### Does it run the emulator or deploy?

No. The sandbox has no Firebase project or credential, so the checks are the repository's own lint, the `tsc` type-check of the JavaScript against the SDK types, and any Jest or Mocha tests using `firebase-functions-test`. Deploying and deleting are listed as steps for the reviewer.

### What about functions.config() values?

Where a handler reads `functions.config()`, the agent moves the value to `defineString` or `defineSecret` from `firebase-functions/params` and notes the `.env` or Secret Manager entry the project needs, because 2nd gen functions do not read the old runtime config.

## More Firebase Functions jobs

- [Reduce Cold Starts in Cloud Functions for Firebase](https://getrush.ai/run/reduce-cold-starts/firebase-functions/)
- [Upgrade firebase-functions and firebase-admin](https://getrush.ai/run/upgrade-firebase-functions/firebase-functions/)
- [Write Tests for Firebase Cloud Functions](https://getrush.ai/run/write-emulator-tests/firebase-functions/)

## More jobs like this: Hand off a long task

- [Migrate a Gradle Build to a Version Catalog](https://getrush.ai/run/migrate-to-version-catalog/gradle/)
- [Move a Koa Project from Travis CI to GitHub Actions](https://getrush.ai/run/migrate-travis-to-github-actions/koa/)
- [Move a Mongoose API from Travis CI to GitHub Actions](https://getrush.ai/run/migrate-travis-to-github-actions/mongoose/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
