a year ago
how to add firebase service account key on railway and handle in python code
4 Replies
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_JSONvariable
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') ```
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
a year ago
Here is the documentation for how to get it from firebase https://firebase.google.com/docs/admin/setup#initialize%5Fthe%5Fsdk%5Fin%5Fnon-google%5Fenvironments
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') ```
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