79710292

Date: 2025-07-22 10:40:46
Score: 0.5
Natty:
Report link

I needed to pass my serviceAccount.json to Firebase Admin SDK in a serverless environment via an environment variable.

To solve this, I took the following steps:

  1. Copied the contents of serviceAccount.json

  2. Minified it to a single-line JSON string

  3. Set it as the value of the FIREBASE_ADMIN_CREDENTIALS environment variable

Then, I updated my code like this:

import { cert, initializeApp, getApps } from 'firebase-admin/app';
import { getAuth } from 'firebase-admin/auth';

const credsString = process.env.FIREBASE_ADMIN_CREDENTIALS!; 
const serviceAccount = JSON.parse(credsString);

const adminApp = getApps()[0] || initializeApp({
  credential: cert(serviceAccount),
});

export const adminAuth = getAuth(adminApp);

Build Success: This resolved the build-time issue because the cert() function expects a JavaScript object with camelCase keys like privateKey, clientEmail, etc.

⚠️ Runtime Note: It's important that the JSON string in FIREBASE_ADMIN_CREDENTIALS exactly matches the original serviceAccount.json format. Firebase internally maps the snake_case keys to the required camelCase format during cert().

Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Touseef Ahmed