Learn how to set up and access Firebase config variables in Cloud Functions using the Firebase CLI, Admin SDK, and environment configuration.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Firebase Project and Install Firebase CLI
To access Firebase configuration variables in your backend functions, you first need to make sure that your Firebase project is set up correctly and that you have the Firebase CLI installed. If you haven't set up Firebase in your project yet, follow these steps:
Once your project is set up, install the Firebase CLI by running the following command in your terminal:
npm install -g firebase-tools
Step 2: Initialize Firebase Functions
Next, you need to initialize Firebase Functions in your project. Use the Firebase CLI to bootstrap a new Cloud Functions directory:
firebase init functions
During initialization, you'll be prompted to select the project you just created on Firebase. Also, choose the language you prefer for writing the functions (JavaScript or TypeScript).
Step 3: Install Firebase Admin SDK
To access Firebase configurations from within your functions, you'll need the Firebase Admin SDK:
npm install firebase-admin
Step 4: Environment Configuration
To use environment configuration variables in Firebase Functions, you have to use the Firebase Functions configuration options. These variables can be set using the firebase functions:config:set command. For example:
firebase functions:config:set someservice.key="YOUR_API_KEY" someservice.id="YOUR\_ID"
These commands set configuration variables that can be accessed within your functions code.
Step 5: Writing a Function to Access Config Variables
Now that you have your environment configuration set up, you can access these variables in your Cloud Functions. Here's how to write a function that does this:
index.js or index.ts inside your functions directory.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.myFunction = functions.https.onRequest((req, res) => {
const apiKey = functions.config().someservice.key;
const id = functions.config().someservice.id;
res.send(`API Key: ${apiKey} and ID: ${id}`);
});
This function demonstrates how to access and return configuration variables when the function is triggered.
Step 6: Deploy the Functions
To apply your changes and deploy the functions to Firebase, follow these steps:
firebase deploy --only functions
This command deploys your functions to Firebase, making them ready to use. Your configuration variables will now be accessible within your deployed functions.
Step 7: Test Your Function
Finally, it’s time to test if everything is set up correctly. You can do this by triggering the function you deployed:
curl or Postman to make HTTP requests to your function's URL (provided in the terminal after deployment).If set up correctly, your function should respond with the configured API Key and ID.
Now you have comprehensive access to Firebase configuration variables within your Cloud Functions!
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.