Initialize firebase-admin (Python)

Oct 12, 2019

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.

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.