Dependencies
pip install firebase-admin
or edit requirements.txt (for Cloud Functions)
# https://pypi.org/project/firebase-admin/
firebase-admin==3.0.0
Initialization
# check ifIS_GOOGLE = ... # is current environment Cloud Functions or external serverPROJECT_ID = ...default_app = Nonedef init_firebase(): import firebase_admin from firebase_admin import credentials global default_app if default_app: return default_app if not IS_GOOGLE: # no credential needed for Cloud Functions cred = credentials.ApplicationDefault() else: cred = credentials.Certificate('keys/firebase-adminsdk.json') default_app = firebase_admin.initialize_app(cred, { 'projectId': PROJECT_ID, }) return default_app
NOTE: Goto Google Cloud Console -> Service Accounts, find firebase-adminsdk and click Create key and download the json file as firebase-adminsdk.json
.