How to get Firebase Project Name or ID from Cloud Function

14,323

Solution 1

Thank you @Frank. The answer is: process.env.GCLOUD_PROJECT.
I'm not sure where the variable process comes from, but this does work to get the project name.

Solution 2

Firebase admin SDK has access to that information for you.

const admin = require('firebase-admin');
const projectId = admin.instanceId().app.options.projectId

Solution 3

This worked for me. (node 10)

const FIREBASE_CONFIG = process.env.FIREBASE_CONFIG && JSON.parse(process.env.FIREBASE_CONFIG);
const projectId = FIREBASE_CONFIG.projectId;

Solution 4

try this :

const projectId = admin.instanceId().app['options_'].credential.projectId

Solution 5

For cloud functions running on newer versions of Node:

import firestore from "@google-cloud/firestore";

const client = new firestore.v1.FirestoreAdminClient();
const projectId = await client.getProjectId();
Share:
14,323
Coder1224
Author by

Coder1224

Updated on June 04, 2022

Comments

  • Coder1224
    Coder1224 almost 2 years

    I am using Cloud Functions and want to get the project name from within one of my Javascript server files. I know that value is stored in the .firebaserc, but I don't think that file is available on the server, right? I want to do something like this:

    const functions = require('firebase-functions');
    const admin = require('firebase-admin');
    
    admin.getProjectName();  // or getProjectID()
    

    or

    functions.getProjectName(); 
    
  • okhobb
    okhobb over 6 years
    process is a global object in node. See: nodejs.org/api/globals.html#globals_process
  • okhobb
    okhobb over 6 years
    functions.config().firebase.projectId is not defined for me. I'm only seeing fields: databaseURL, storageBucket, apiKey, authDomain, and credential on the functions.config().firebase object.
  • Pier
    Pier about 6 years
    functions.config() returns environment configurations set by the user, which is why you are getting downvoted.
  • Crazometer
    Crazometer over 5 years
    This may not exist depending on how admin was initialized
  • Joseph Lust
    Joseph Lust about 5 years
    Doesn't appear to work on the GCF Node 10 runtime. Don't see it when dumping process.env
  • torno
    torno about 5 years
    Might be better to use process.env.GCP_PROJECT now instead since GCLOUD_PROJECT has been marked as deprecated here: cloud.google.com/functions/docs/…
  • phatmann
    phatmann about 4 years
    Neither GCLOUD_PROJECT nor GCP_PROJECT are available in Node 10 runtime.
  • Ben Winding
    Ben Winding over 3 years
    GCLOUD_PROJECT seems to work now, it was fixed in the node10 runtime too github.com/firebase/firebase-functions/issues/437
  • Leblanc Meneses
    Leblanc Meneses over 2 years
    admin.instanceId() is deprecated firebase.google.com/docs/reference/admin/node/…
  • GorvGoyl
    GorvGoyl almost 2 years
    works as of July 2022