|
| 1 | +This guide covers Firebase Functions SDK v6.0.0+. |
| 2 | + |
| 3 | +**Key Guidelines** |
| 4 | +* Always use v2 functions for new development. |
| 5 | +* Use v1 functions *only* for Analytics, basic Auth, and Test Lab triggers. |
| 6 | +* For SDK versions before 6.0.0, add `/v2` to import paths (e.g., `firebase-functions/v2/https`). |
| 7 | + |
| 8 | +**Configuration: Use Secret Params for API Keys** |
| 9 | +For sensitive information like API keys (e.g., for LLMs, payment providers, etc.), **always** use `defineSecret`. This stores the value securely in Cloud Secret Manager. |
| 10 | + |
| 11 | +```typescript |
| 12 | +import {onRequest} from 'firebase-functions/v2/https'; |
| 13 | +import {logger} from 'firebase-functions/logger'; |
| 14 | +import {defineString, defineSecret} from 'firebase-functions/params'; |
| 15 | + |
| 16 | +// Securely define an LLM API key |
| 17 | +const LLM_API_KEY = defineSecret('LLM_API_KEY'); |
| 18 | + |
| 19 | +// Example function that uses the secret |
| 20 | +export const callLlm = onRequest({ secrets: [LLM_API_KEY] }, (req, res) => { |
| 21 | + const apiKey = LLM_API_KEY.value(); |
| 22 | + |
| 23 | + // Use the apiKey to make a call to the LLM service |
| 24 | + logger.info('Calling LLM with API key.'); |
| 25 | + |
| 26 | + res.send('LLM API call initiated.'); |
| 27 | +}); |
| 28 | +``` |
| 29 | +When you deploy a function with `secrets`, the CLI will prompt you to enter the secret's value. |
| 30 | + |
| 31 | +**Initializing the Firebase Admin SDK** |
| 32 | +To interact with Firebase services like Firestore, Auth, or RTDB from within your functions, you need to initialize the Firebase Admin SDK. |
| 33 | + |
| 34 | +1. **Install the SDK:** |
| 35 | + ```bash |
| 36 | + npm i firebase-admin |
| 37 | + ``` |
| 38 | + |
| 39 | +2. **Initialize in your code:** |
| 40 | + ```typescript |
| 41 | + import * as admin from 'firebase-admin'; |
| 42 | +
|
| 43 | + admin.initializeApp(); |
| 44 | + ``` |
| 45 | + This should be done once at the top level of your `index.ts` file. |
| 46 | + |
| 47 | +**Common Imports** |
| 48 | +```typescript |
| 49 | +// HTTPS, Firestore, RTDB, Scheduled, Storage, Pub/Sub, Auth, Logging, Config |
| 50 | +import {onRequest} from 'firebase-functions/https'; |
| 51 | +import {onDocumentUpdated} from 'firebase-functions/firestore'; |
| 52 | +import {onValueWritten} from 'firebase-functions/database'; |
| 53 | +import {onSchedule} from 'firebase-functions/scheduler'; |
| 54 | +import {onObjectFinalized} from 'firebase-functions/storage'; |
| 55 | +import {onMessagePublished} from 'firebase-functions/pubsub'; |
| 56 | +import {beforeUserSignedIn} from 'firebase-functions/identity'; |
| 57 | +import {logger} from 'firebase-functions'; |
| 58 | +import {defineString, defineSecret} from 'firebase-functions/params'; |
| 59 | +``` |
| 60 | + |
| 61 | +**v1 Functions (Legacy Triggers)** |
| 62 | +Use the `firebase-functions/v1` import for Analytics and basic Auth triggers. |
| 63 | +```typescript |
| 64 | +import * as functionsV1 from 'firebase-functions/v1'; |
| 65 | +
|
| 66 | +// v1 Analytics trigger |
| 67 | +export const onPurchase = functionsV1.analytics.event('purchase').onLog((event) => { |
| 68 | + logger.info('Purchase event', { value: event.params?.value }); |
| 69 | +}); |
| 70 | +
|
| 71 | +// v1 Auth trigger |
| 72 | +export const onUserCreate = functionsV1.auth.user().onCreate((user) => { |
| 73 | + logger.info('User created', { uid: user.uid }); |
| 74 | +}); |
| 75 | +``` |
| 76 | +
|
| 77 | +**Development Commands** |
| 78 | +```bash |
| 79 | +# Install dependencies |
| 80 | +npm install |
| 81 | +
|
| 82 | +# Compile TypeScript |
| 83 | +npm run build |
| 84 | +
|
| 85 | +# Run emulators for local development |
| 86 | +firebase emulators:start --only functions |
| 87 | +
|
| 88 | +# Deploy functions |
| 89 | +firebase deploy --only functions |
| 90 | +``` |
0 commit comments