How do use `defineSecret` with express and `onRequest` in firebase function V”?

Ted James
3 min readApr 13, 2024

I am upgrading my firebase functions to V2 and I want to use the new way of managing secrets via defineSecret described here: https://firebase.google.com/docs/functions/config-env?hl=en&gen=2nd

It is not clear to me how this works with onRequest to host a function via express that can be accessed from outside the firebase framework.

Here is a minimal version of my setups (which worked with firebase functions V1):

import express from "express"
import cors from "cors"
import { onRequest } from "firebase-functions/v2/https"

const app = express()

app.use(
cors({
origin: function (origin: string | undefined, callback: (err: Error | null, allow?: boolean) => void) {
if (!origin) return callback(null, true)
return callback(null, true)
},
}),
)

import { handleNewProfile } from "./handlers/profiles" // needs a secret key for external service like sendgrid


app.post("/profiles/", handleNewProfile)
export const api = onRequest({ region: "europe-west1" }, app) // how to pass secret to app?

I can add the secrets to the first argument like this:

import { defineSecret } from "firebase-functions/params"
{ region: DEFAULT_REGION, secrets: [secretDiscordToken] }

But how do I hand it down to handleNewProfile?

I have not found this case in the docs and ChatGPT also does not know! :)

--

--

Ted James

The world is my office, and every destination is my inspiration