Search code examples
firebasegofirebase-admin

Initialize Firebase Admin SDK with only environment variables in Golang


I'm trying to initialize the Firebase Admin SDK, in go, using only environment variables (no access to a filesystem).

I have the service-account-file.json file, and can get it working locally via:

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/service-account-file.json"

...

myfile.go:
app, err := firebase.NewApp(context.Background(), nil)

However, I'd like to put the contents of service-account-file.json into an env var, and init the firebase Admin SDK with its value.

There is an open issue that claims you can do it via CredentialsFromJSON, however I can't quite figure it out.

Does anyone have this working? If so can you please provide an example?


Solution

  • try:

    export JSON_CREDS="{\"type\":\"service_account\",\"project_id\":\"project-id\",\"private_key_id\":\"some_number\",\"private_key\":\"\",\"client_email\":\"<api-name>api@project-id.iam.gserviceaccount.com\",\"client_id\":\"...\",\"auth_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/auth\",\"token_uri\":\"https:\/\/accounts.google.com\/o\/oauth2\/token\",\"auth_provider_x509_cert_url\":\"https:\/\/www.googleapis.com\/oauth2\/v1\/certs\",\"client_x509_cert_url\":\"https:\/\/www.googleapis.com\/...<api-name>api%40project-id.iam.gserviceaccount.com\"}"
    
    ...
    
    import "google.golang.org/api/option"
    
    ...
    
    app, err := firebase.NewApp(context.Background(), nil, option.WithCredentialsJSON([]byte(os.Getenv("JSON_CREDS"))))