Access Firestore From Google Cloud Functions (Python)

requirements.txt

# https://pypi.org/project/firebase-admin/
firebase-admin==2.16.0

main.py

from flask import jsonifyimport firebase_adminfrom firebase_admin import credentialsfrom firebase_admin import firestoreimport google.cloud.exceptions# this should solve the "The default Firebase app already exists." exceptioncred = credentials.ApplicationDefault()default_app = firebase_admin.initialize_app(cred, {  'projectId': '...',})db = firestore.client()def test_firestore(request):    doc_ref = db.collection('test').document('5pSFOxspzl75hYZzjnut')    try:        doc = doc_ref.get()        data = doc.to_dict()        return jsonify(data)    except google.cloud.exceptions.NotFound:        return 'Not found'

❤️ 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.