# Move Firebase Functions From process.env to Params and Secrets

Yes. An agent replaces bare process.env reads in your functions with defineSecret, defineString and defineInt, binds secrets to each trigger, tests it, in a PR.

[Run this on Rush](https://cloud.getrush.ai/run/migrate-to-params/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 every function that touches the environment. In a repository like firebase/functions-samples, the 2nd gen samples under `Node/` are a pnpm workspace whose root `package.json` lints, tests and compiles them with `pnpm --recursive --filter ./Node/**`. `Node/alerts-to-discord/functions/index.js` reads `process.env.DISCORD_WEBHOOK_URL` inside `postMessageToDiscord` and throws if it is empty, and its README tells the reader to put the URL in a `.env` file in `functions/`; its `package.json` is on `firebase-functions` 7.4.0 and `firebase-admin` 14.5.0 with Node 24 and a `lint` script that `firebase.json` runs before deploy. `Node/testlab-to-slack/functions/index.js` passes `process.env.SLACK_WEBHOOK_URL` straight to `fetch` with no check at all. `Node/taskqueues-backup-images/functions/index.js` parses `BACKUP_COUNT` and `HOURLY_BATCH_SIZE` with fallbacks of 100 and 500, reads `BACKUP_BUCKET` with no fallback, and sends `NASA_API_KEY` as a query parameter. The repository already shows the better pattern: `Node/quickstarts/email-users/functions/newUserWelcome.js` declares `defineSecret("EMAIL_API_KEY")` and binds it with `{ secrets: [emailApiKey] }` on `onUserCreated`, and `Node/app-distribution-feedback-to-jira/functions/index.js` uses `defineString` with descriptions and input validation, `defineInt` with defaults and `defineSecret` for the API token, bound on `onInAppFeedbackPublished`. The reads of `GCLOUD_PROJECT` in the Remote Config samples and of `FUNCTIONS_EMULATOR` in the OpenTelemetry sample are values the platform sets, not configuration, and the agent leaves them.

Webhook URLs grant the right to post, so they become secrets. In `alerts-to-discord` the agent declares `defineSecret("DISCORD_WEBHOOK_URL")` from `firebase-functions/params`, binds it with `secrets:` on all three triggers, `onNewFatalIssuePublished`, `onNewTesterIosDevicePublished` and `onThresholdAlertPublished`, and reads it with `.value()` inside the handler, since a param's value is only available at runtime and a read at module load throws during deploy analysis. The throw-if-missing branch goes, because a function that declares a secret cannot deploy without it. `testlab-to-slack` gets the same treatment for `SLACK_WEBHOOK_URL` on `onTestMatrixCompleted`. In `taskqueues-backup-images`, `BACKUP_COUNT` and `HOURLY_BATCH_SIZE` become `defineInt` with their existing defaults, `BACKUP_BUCKET` becomes `defineString` with a description, and `NASA_API_KEY` becomes `defineSecret`, all read with `.value()` where they are used and the secret bound on the task queue function.

Each sample's README changes with it: the Discord instructions replace the `.env` file with `firebase functions:secrets:set DISCORD_WEBHOOK_URL`, and the backup sample explains that `firebase deploy` prompts for the two integers and the bucket on first deploy and writes them to a `.env.<project>` file it keeps. The root README's descriptions are unchanged. No other sample is touched, and the `.gitignore` entries for `.env` files stay, because the deploy-written params files belong there too.

Each changed sample runs its own `lint` script through the workspace, which `firebase.json` would also run as `predeploy`. For behaviour, the agent adds a Jest test beside the Discord function in the shape of `Node/test-functions-jest`: it sets `DISCORD_WEBHOOK_URL` in `process.env`, which is where `.value()` reads at runtime, wraps `postfatalissuetodiscord` with `firebase-functions-test`, stubs the global `fetch`, calls the wrapped function with a fatal issue event, and asserts the URL and the message body. A second case unsets the variable and asserts the handler rejects, showing the secret is read at call time and not at import. The same test shape covers the Slack function.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Move the 2nd gen Firebase functions in this repository from bare process.env reads to the params API. Read the root package.json and pnpm workspace, every functions/index.js under Node/ that reads process.env, the two samples that already use defineSecret and defineString, the READMEs and firebase.json of the samples you change, and the Jest sample's test first. Turn webhook URLs and API keys into defineSecret, counts into defineInt with their existing defaults and a bucket name into defineString with a description; bind each secret on every trigger that uses it and read values with .value() inside handlers only. Leave the reads of platform-set variables such as GCLOUD_PROJECT and FUNCTIONS_EMULATOR alone. Update each changed sample's README to use firebase functions:secrets:set and to explain the deploy-time prompt for string and integer params. Run each changed sample's lint script, add Jest tests with firebase-functions-test and a stubbed fetch that assert the webhook URL and body and that a missing secret fails at call time, run them, and open a pull request listing every variable and its new param type.",
  "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 edits `index.js`, `package.json` and `README.md` in the three samples that read configuration from the environment, and adds a Jest test to the two webhook samples. The description has one table with every variable, the sample it belongs to, its new param type and whether it is bound as a secret, plus the `firebase functions:secrets:set` command per secret.

The run's record keeps the `lint` output for each changed sample and the Jest runs with each case named, including the one that shows a missing secret failing when the handler runs rather than when the module loads, so a reviewer can see that deploy analysis would not break.

Before merging, a reviewer sets the Discord secret with the CLI in a test project, deploys that one codebase, reads the function's configuration in the console to confirm the secret is bound, and triggers a Crashlytics alert to see the Discord message arrive.

## Run it on every function that reads process.env

The setup is one `POST /o/{org}/sessions` with this body and the repository in `resources`, started from a terminal; the run continues in its sandbox after the window closes, and a follow-up through `POST /o/{org}/sessions/{id}/messages` can extend the change to a sample the first pass skipped without starting over. A team with several Firebase projects sends one request per repository, each with a fresh UUID as `request_id`, so a retried script returns the run it already started.

Set a `webhook_url` on the API key and Rush posts `run.completed` or `run.failed` when a run's turn finishes, with `session_url` so your server can read the pull request link and the test output.

## Why run it on Rush?

The params API has one rule that a grep-and-replace misses: `.value()` works only inside a running function, so a read hoisted to module scope, where the old `process.env` lines sat, passes lint and fails at deploy. The agent reads each handler to move the read, binds the secret on every trigger in the file, and runs a test that calls the wrapped function with and without the variable, in its own sandbox, so the failure mode is exercised before a deploy sees it.

Every command and its output stay with the run, so the pull request can point at the lint result and at the test that asserts the Discord body, and a reviewer who has not deployed the sample can still check the claim against the record.

## Frequently asked questions

### Why are the webhook URLs secrets and not strings?

Anyone holding a Discord or Slack webhook URL can post to the channel, so it is a credential. `defineSecret` keeps it in Secret Manager and mounts it only on the functions that list it in `secrets`, while `defineString` would write it to a `.env` file and the function's environment.

### What happens on the first deploy after this change?

The CLI refuses to deploy a function whose secret is not set, so `firebase functions:secrets:set` comes first. For `defineString` and `defineInt` params with no value, `firebase deploy` prompts once and saves the answers to a per-project `.env` file, and the integer params use their defaults if accepted.

### Does the emulator still work?

Yes. The emulator reads params from `.env` and `.secret.local` files in the functions directory, and the README explains the local file for the secret. The `FUNCTIONS_EMULATOR` check in the OpenTelemetry sample is untouched because the emulator sets it.

### Why leave GCLOUD_PROJECT as it is?

The runtime sets it, along with other platform variables, and a param would only duplicate it with a value the reader has to type. The pull request draws the line at values a person configures: credentials, counts and bucket names.

## More Firebase Functions jobs

- [Migrate Firebase Functions from 1st to 2nd Gen](https://getrush.ai/run/migrate-to-v2-functions/firebase-functions/)
- [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/)

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

- [Migrate a Python Library from Pydantic v1 to v2](https://getrush.ai/run/migrate-to-pydantic-v2/pydantic/)
- [Migrate an Angular App to Signal Inputs and Outputs](https://getrush.ai/run/migrate-to-signals/angular/)
- [Migrate from drf-jwt to SimpleJWT in a Django REST Framework App](https://getrush.ai/run/migrate-to-simplejwt/django-rest-framework/)

## 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/)
