/firebase-tutorials

How to access Firebase config variables in functions?

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

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to access Firebase config variables in functions?

 

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:

  • Go to the Firebase Console and click on 'Add project' to create a new project.
  • Follow the steps to configure your project name, Google Analytics (optional), and other settings.

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:

  • In your terminal, navigate to the root of your project directory.
  • Run the following command to initialize Firebase Functions:

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:

  • Open the functions directory that was created during initialization.
  • Install the Firebase Admin SDK by running the following command in that directory:

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:

  • In your terminal, run:

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:

  • Open index.js or index.ts inside your functions directory.
  • Add the following code to access and use the config variables:

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:

  • Run the command to deploy your functions:

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:

  • You can use a tool like 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!

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022