how to add firebase service account key on railway and handle in python code
suraj-bhatt1217
HOBBYOP

a year ago

how to add firebase service account key on railway and handle in python code

$10 Bounty

4 Replies

alexwebgr
HOBBY

a year ago

Step 1

  • In the Firebase console, navigate to your project's settings by clicking the gear icon next to "Project Overview."
  • Select Project settings, then go to the Service accounts tab
  • Click the Generate new private key button. A confirmation pop-up will appear
  • Click Generate Key to download a JSON file containing your service account credentials
  • Store this JSON file securely and do not commit it to a public code repository, as it provides administrative access to your Firebase project

Step 2

  • Open your project in the Railway dashboard and navigate to the service where you want to add the key, the one hosting the python code.
  • Go to the Variables tab.
  • Click on New Variable. It is recommended to add the key via the Raw Editor to ensure correct formatting.
  • Create a variable named FIREBASE_CREDENTIALS_JSON.
  • Open the JSON file you downloaded from Firebase, copy its entire content, and paste it as the value for the FIREBASE_CREDENTIALS_JSON variable

Step 3

Use it! You might need to adjust the dependencies you import according to your use case

import os
import json

# Retrieve the JSON credentials from the environment variable
firebase_creds_json_str = os.environ.get('FIREBASE_CREDENTIALS_JSON')

alexwebgr

Step 1 * In the Firebase console, navigate to your project's settings by clicking the gear icon next to "Project Overview." * Select **Project settings**, then go to the **Service accounts** tab * Click the **Generate new private key** button. A confirmation pop-up will appear * Click **Generate Key** to download a JSON file containing your service account credentials * Store this JSON file securely and do not commit it to a public code repository, as it provides administrative access to your Firebase project Step 2 * Open your project in the Railway dashboard and navigate to the service where you want to add the key, the one hosting the python code. * Go to the **Variables** tab. * Click on **New Variable**. It is recommended to add the key via the **Raw Editor** to ensure correct formatting. * Create a variable named `FIREBASE_CREDENTIALS_JSON`. * Open the JSON file you downloaded from Firebase, copy its entire content, and paste it as the value for the `FIREBASE_CREDENTIALS_JSON` variable Step 3 Use it! You might need to adjust the dependencies you import according to your use case ``` import os import json # Retrieve the JSON credentials from the environment variable firebase_creds_json_str = os.environ.get('FIREBASE_CREDENTIALS_JSON') ```

sim
FREE

a year ago

This is a very detailed reply and it will help you. I do not wan to take the bounty but here is the documentation for environment variables https://docs.railway.com/guides/variables#using-variables-in-your-services


sim
FREE

a year ago


alexwebgr

Step 1 * In the Firebase console, navigate to your project's settings by clicking the gear icon next to "Project Overview." * Select **Project settings**, then go to the **Service accounts** tab * Click the **Generate new private key** button. A confirmation pop-up will appear * Click **Generate Key** to download a JSON file containing your service account credentials * Store this JSON file securely and do not commit it to a public code repository, as it provides administrative access to your Firebase project Step 2 * Open your project in the Railway dashboard and navigate to the service where you want to add the key, the one hosting the python code. * Go to the **Variables** tab. * Click on **New Variable**. It is recommended to add the key via the **Raw Editor** to ensure correct formatting. * Create a variable named `FIREBASE_CREDENTIALS_JSON`. * Open the JSON file you downloaded from Firebase, copy its entire content, and paste it as the value for the `FIREBASE_CREDENTIALS_JSON` variable Step 3 Use it! You might need to adjust the dependencies you import according to your use case ``` import os import json # Retrieve the JSON credentials from the environment variable firebase_creds_json_str = os.environ.get('FIREBASE_CREDENTIALS_JSON') ```

elliottmurray
HOBBY

2 months ago

You can also encode it, store that and then use entrypoint script in your docker file to write it out:

base64 < firebase_credentials.json| tr -d '\n'. # to encode

  if [ -n "$FIREBASE_CREDENTIALS_JSON" ]; then
    echo "$FIREBASE_CREDENTIALS_JSON" | base64 -d > firebase_credentials.json
    echo "Firebase credentials written to firebase_credentials.json"
  fi

Welcome!

Sign in to your Railway account to join the conversation.

Loading...