Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended way to use FastAPI with Firebase (Pyrebase4 or firebase_admin) #2651

Closed
JuniorGunner opened this issue Jan 14, 2021 · 11 comments
Closed
Labels
question Question or problem question-migrate

Comments

@JuniorGunner
Copy link

JuniorGunner commented Jan 14, 2021

I want to know how to setup the comunication between FastAPI and the Firebase data storage (Firestore) through Pyrebase4. I didn't find any info or example of this case.

@JuniorGunner JuniorGunner added the question Question or problem label Jan 14, 2021
@JuniorGunner JuniorGunner changed the title Recomended way to use FastAPI with Firebase (Pyrebase4) [QUESTION] Recommended way to use FastAPI with Firebase (Pyrebase4) Jan 14, 2021
@JuniorGunner JuniorGunner changed the title [QUESTION] Recommended way to use FastAPI with Firebase (Pyrebase4) [QUESTION] Recommended way to use FastAPI with Firebase (Pyrebase4 or firebase_admin) Jan 14, 2021
@krishnardt
Copy link
Contributor

Pyrebase is pretty straight forward to use/understand than firebase-admin in implementation.
I tried both of them.
Pyrebase has realtime streaming/listening function.
In older versions of Firebase_admin there is no realtime listener for python SDK. It is implemented in the latest versions.

Currently in feature-wise, there is no much difference between both libraries.

@JuniorGunner
Copy link
Author

JuniorGunner commented Jan 14, 2021

Do you have some example of basic setup of FastAPI with Pyrebase4? I mean, some simple CRUD using it. I couldn't find any.

@krishnardt
Copy link
Contributor

You can check this sample..

import pyrebase

fireBaseConfig = {
        
    'apiKey': "******************************",
    'authDomain': "********************************.firebaseapp.com",
    'databaseURL': "https://**************.firebaseio.com",
    'projectId': "**************",
    'storageBucket': "**************.appspot.com",
    'messagingSenderId': "*****************",
    'appId': "**************************",
    'measurementId': "**************"
        }

we will have to get those firebaseConfig details by registering in firebase.

firebase = pyrebase.initialize_app(fireBaseConfig)
auth = firebase.auth()
db = firebase.database()

you can save above db config details in seperate file and can import them.

import json
from fastapi import FastAPI

app=FastAPI()

@app.get("/")
async def get_data():
	"""to get data from a given table"""
	some_data = db.child('any_table_name').get()
	print(some_data)
	return some_data

@app.get("/users")
async def get_users():
	"""to get users details"""
	return db.child('users').get()

@app.get("/info")
async def get_info():
	"""to get every detail in the database."""
	return db.child().get()

for reference
https://github.com/nhorvath/Pyrebase4

@JuniorGunner
Copy link
Author

I did the following, but for a reason that I didn't find yet, I can't reach the firebase data.

image

I got http 200 but without the Firebase data. Any tip about that?

@krishnardt
Copy link
Contributor

krishnardt commented Jan 18, 2021

have you created api_key etc., in firebase config details and used in api?

need more info

@JuniorGunner
Copy link
Author

My project structure:

my_api
|_app
|_config
| |_database.py
|_routes
| |_category.py
|_schemas
| |_category.py
|_main.py

My app/config/database.py:

image

My app/schemas/category.py:

image

My app/routes/category.py:

image

My app/main.py:

image

Am I missing something?

@JuniorGunner
Copy link
Author

image

Just for confirmation, I have one sigle data saved for test.

@krishnardt
Copy link
Contributor

Can you check in realtime database?
Currently pyrebase doesn't have support to cloud firestore. It has support for realtime database.
Whatever you do transactions with pyrebase, they will be stored and fetched from realtime database.

If you need to work on firestore,use firebase_admin sdk.

follow this link for reference...
thisbejim/Pyrebase#268 (comment)

@JuniorGunner
Copy link
Author

JuniorGunner commented Jan 19, 2021

Thank you for your help. Since it doesn't have support I'll change to realtime database or firebase_admin sdk.

@DotSyndicate
Copy link

@JuniorGunner, I hope you made it work. I am also in a similar situation. If your app worked, can you please share your code with me? Like, I need to understand how you made it work. Thank you. You can make it as an answer to this.

@tiangolo
Copy link
Member

Thanks for the help here everyone! 👏 🙇

Thanks for reporting back and closing the issue 👍

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@tiangolo tiangolo changed the title [QUESTION] Recommended way to use FastAPI with Firebase (Pyrebase4 or firebase_admin) Recommended way to use FastAPI with Firebase (Pyrebase4 or firebase_admin) Feb 24, 2023
@tiangolo tiangolo reopened this Feb 27, 2023
@fastapi fastapi locked and limited conversation to collaborators Feb 27, 2023
@tiangolo tiangolo converted this issue into discussion #6962 Feb 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Question or problem question-migrate
Projects
None yet
Development

No branches or pull requests

4 participants