william
william

Reputation: 635

how to get/set firebase cloud functions v1 environment variables

I was using the environment variables from Cloud Functions package the old way by using the functions.config() command, but since I updated to v1.0.2 I cannot use the env variables even through JSON.parse(process.env.FIREBASE_CONFIG) like the documentation tells to do, and I couldn't find how to set or get other information. This command only gives me information about the project but not the information I have set using the old approach. How can I get/set this information using the new approach?
Thanks in advance.

Upvotes: 3

Views: 2637

Answers (3)

repo
repo

Reputation: 756

What I do is basically have

env.prod.ts
env.dev.ts
env.ts

and in env.ts I do

export const env = require(`./env.${functions.config().env.name}').default;

and in package.json i set

"dev-prod": firebase use prod && functions:config:set env.name=prod

etc so I can have both fb keys and my own prod keys and the whole app is config-agnostic

when i need env var I just use

import environment from '../env.ts'

works with json etc(dotenv doesnt)

Upvotes: 0

Doug Stevenson
Doug Stevenson

Reputation: 317372

The documentation for the environment changes in 1.0 can be a little confusing. functions.config() isn't going away - you still use that to access environment variables that you set using the Firebase CLI with firebase functions:config:set. The things that changed with respect to configuration are the following:

  • functions.config().firebase is no longer used for project configuration. That information is now stored in the process environment as process.env.FIREBASE_CONFIG.
  • You can now initialize the Admin SDK with no arguments as admin.initializeApp(). The configuration will be picked up from the Cloud Functions runtime automatically.

Upvotes: 3

rassa45
rassa45

Reputation: 3550

So check out this answer here https://stackoverflow.com/a/45064266/4871483. What I understand is that the new cloud functions will work from a local .runtimeconfig.json. What I would recommend is temporarily downgrade and write your old config to that file. Then you can use these docs https://firebase.google.com/docs/functions/local-emulator for the code on how to access it once you upgrade again.

Upvotes: 0

Related Questions