Search code examples
firebasegoogle-cloud-functionsgcloudenvironment

How to pass secure parameters to onSchedule firebase function?


I am writing a firebase function using the onSchedule handlers from the firebase sdk. The thing is that the function I am writing requires the use of sensitive information, and I don't want this into the codebase.

Looking at the firebase functions documentation for Configuring Your Environment, I need to be able to use the runWith parameters from the v2 firebase functions api, because those environment / parametrized variables needs to be bound to the function that is going to use them.

It seems the onSchedule handler cannot be used when using the runWith parameter from firebase-functions, I can only use the https, pubsub etc... handlers with it.

Is this not yet supported or is there another way to achieve the same thing?

Thanks.


Solution

  • You can create a .env file and put all of your sensitive information there. Then access the environment variables as shown below.

    const { onSchedule } = require("firebase-functions/v2/scheduler");
    
    const PRIVATE_CREDENTIAL = process.env.PRIVATE_CREDENTIAL
    
    exports.scheduledFunction = onSchedule("5 11 * * *", async (event) => {
    // ...
    });
    

    or if you prefer using secrets:

    const { onSchedule } = require("firebase-functions/v2/scheduler");
    const { defineSecret } = require('firebase-functions/params');
    const ApiKey = defineSecret('API_KEY');
    
    exports.schedule = onSchedule({secrets:[ApiKey], schedule:"5 11 * * *" },
    async (event) => {
    // ...
    })
    

    Check out the documentation for further information.