Cloud Functions

This project uses Firebase Cloud Functions v2 (2nd generation) exclusively. V2 functions run on Cloud Run and provide better performance, concurrency, and importantly, native support for Secret Manager.

Why v2 Functions?

Basic Structure

All functions follow this pattern:

import { onRequest } from 'firebase-functions/v2/https'
import { optionsResponse } from './utilities'

export const myFunction = onRequest({}, async (req, res) => {
  // Handle CORS preflight
  if (optionsResponse(req, res)) {
    return
  }

  // Your logic here
  res.json({ result: 'success' })
})

Examples

Simple Endpoint: hello.ts

The simplest example of a v2 function:

import { onRequest } from 'firebase-functions/v2/https'
import compression from 'compression'
import { optionsResponse, getUser, getUserRoles, timestamp } from './utilities'

const compressResponse = compression()

export const hello = onRequest({}, async (req, res) => {
  if (optionsResponse(req, res)) {
    return
  }
  
  const user = await getUser(req)
  const userRoles = await getUserRoles(req)

  compressResponse(req, res, () => {
    res.json({
      result: `hello ${req.query.name || 'to you'} too!`,
      timestamp: timestamp(),
      user,
      userRoles,
    })
  })
})

Key points:

Using Secrets: gen.ts

For functions that need API keys or other sensitive data, use Secret Manager:

import { onRequest } from 'firebase-functions/v2/https'
import { defineSecret } from 'firebase-functions/params'
import { optionsResponse, getUserRoles } from './utilities'

// Define secrets at module level
const geminiApiKey = defineSecret('gemini-api-key')
const chatgptApiKey = defineSecret('chatgpt-api-key')

export const gen = onRequest(
  // Pass secrets in the options object
  { secrets: [geminiApiKey, chatgptApiKey] },
  async (req, res) => {
    if (optionsResponse(req, res)) {
      return
    }

    // Access secret values inside the handler
    const apiKey = geminiApiKey.value()
    
    // Use the secret...
  }
)

Key points:

Setting Up Secrets

# Create a new secret
firebase functions:secrets:set my-api-key

# View secret versions
firebase functions:secrets:get my-api-key

# Destroy a secret
firebase functions:secrets:destroy my-api-key

Secrets are stored in Google Cloud Secret Manager and automatically injected into your function at runtime.

Best Practices

Do

Don't

Exporting Functions

Add your function to functions/src/index.ts:

export { myFunction } from './my-function'

Routing

Configure URL routing in firebase.json:

{
  "hosting": {
    "rewrites": [
      {
        "source": "/my-endpoint/**",
        "function": "myFunction"
      }
    ]
  }
}

Deployment

# Deploy all functions
firebase deploy --only functions

# Deploy a specific function
firebase deploy --only functions:myFunction

Common Utilities

From functions/src/utilities.ts:

Function Purpose
optionsResponse(req, res) Handle CORS preflight, returns true if handled
getUser(req) Get authenticated user from token
getUserRoles(req) Get user's roles for access control
timestamp() Current ISO timestamp

See Also