Convert Figma logo to code with AI

firebase logofirebase-admin-python

Firebase Admin Python SDK

1,014
309
1,014
119

Top Related Projects

A simple python wrapper for the Firebase API.

Google Cloud Client Library for Python

4,483

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.

Quick Overview

Firebase Admin Python SDK is an official library that enables server-side integration with Firebase services. It provides a set of tools for managing and interacting with Firebase projects, including authentication, database operations, and cloud messaging, all from within Python applications.

Pros

  • Seamless integration with Firebase services
  • Comprehensive documentation and official support from Google
  • Supports multiple Firebase features like Realtime Database, Firestore, and Authentication
  • Easy to use with a clean and intuitive API

Cons

  • Limited to server-side applications only
  • Requires Firebase project setup and configuration
  • May have performance overhead for very large-scale applications
  • Learning curve for developers new to Firebase ecosystem

Code Examples

  1. Initializing the Firebase Admin SDK:
import firebase_admin
from firebase_admin import credentials

cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
  1. Authenticating a user with a custom token:
from firebase_admin import auth

uid = "some-uid"
custom_token = auth.create_custom_token(uid)
print(f"Custom token: {custom_token.decode()}")
  1. Reading data from Firestore:
from firebase_admin import firestore

db = firestore.client()
doc_ref = db.collection('users').document('alovelace')
doc = doc_ref.get()
if doc.exists:
    print(f'Document data: {doc.to_dict()}')
else:
    print('No such document!')
  1. Sending a Firebase Cloud Message:
from firebase_admin import messaging

message = messaging.Message(
    notification=messaging.Notification(
        title='New message',
        body='You have a new message!'
    ),
    token='user_device_token'
)
response = messaging.send(message)
print(f'Successfully sent message: {response}')

Getting Started

  1. Install the Firebase Admin Python SDK:

    pip install firebase-admin
    
  2. Set up a Firebase project and download the service account key JSON file.

  3. Initialize the SDK in your Python script:

    import firebase_admin
    from firebase_admin import credentials
    
    cred = credentials.Certificate("path/to/serviceAccountKey.json")
    firebase_admin.initialize_app(cred)
    
  4. You can now use the various Firebase services in your application. Refer to the official documentation for specific usage instructions for each service.

Competitor Comparisons

A simple python wrapper for the Firebase API.

Pros of Pyrebase

  • Simpler API and easier to use for beginners
  • Supports both Firebase Realtime Database and Firebase Authentication
  • Lightweight and focused on common Firebase operations

Cons of Pyrebase

  • Not officially supported by Firebase/Google
  • Less comprehensive feature set compared to the official SDK
  • May not receive updates as frequently as the official SDK

Code Comparison

Firebase Admin Python:

import firebase_admin
from firebase_admin import credentials, db

cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred, {'databaseURL': 'https://your-db.firebaseio.com'})

ref = db.reference('users')
ref.set({'name': 'John Doe'})

Pyrebase:

import pyrebase

config = {
    "apiKey": "your-api-key",
    "databaseURL": "https://your-db.firebaseio.com",
    "authDomain": "your-project.firebaseapp.com",
    "storageBucket": "your-project.appspot.com"
}

firebase = pyrebase.initialize_app(config)
db = firebase.database()
db.child("users").set({"name": "John Doe"})

Both libraries provide ways to interact with Firebase, but Pyrebase offers a more straightforward approach for basic operations, while Firebase Admin Python provides more comprehensive features and official support.

Google Cloud Client Library for Python

Pros of google-cloud-python

  • Broader scope, covering multiple Google Cloud services beyond Firebase
  • More comprehensive documentation and examples
  • Active community with frequent updates and contributions

Cons of google-cloud-python

  • Larger library size, potentially increasing project complexity
  • Steeper learning curve due to the wide range of services covered
  • May include unnecessary dependencies for projects focused solely on Firebase

Code Comparison

firebase-admin-python:

import firebase_admin
from firebase_admin import credentials, firestore

cred = credentials.Certificate("path/to/serviceAccount.json")
firebase_admin.initialize_app(cred)
db = firestore.client()

google-cloud-python:

from google.cloud import firestore

db = firestore.Client()
doc_ref = db.collection('users').document('alovelace')
doc_ref.set({
    'first': 'Ada',
    'last': 'Lovelace'
})

Both libraries provide similar functionality for Firestore operations, but google-cloud-python offers a more direct approach without the need for explicit initialization. However, firebase-admin-python provides additional Firebase-specific features and may be more suitable for projects exclusively using Firebase services.

4,483

The ultimate Python library in building OAuth, OpenID Connect clients and servers. JWS,JWE,JWK,JWA,JWT included.

Pros of Authlib

  • More flexible and supports multiple authentication protocols (OAuth 1.0, OAuth 2.0, OpenID Connect)
  • Can be used with various web frameworks and databases
  • Lightweight and has fewer dependencies

Cons of Authlib

  • Requires more manual setup and configuration
  • Less comprehensive documentation compared to Firebase Admin SDK
  • Smaller community and ecosystem

Code Comparison

Authlib (OAuth 2.0 Client):

from authlib.integrations.requests_client import OAuth2Session
client = OAuth2Session(client_id, client_secret, scope='profile')
token = client.fetch_token(token_endpoint, authorization_response=callback_url)

Firebase Admin SDK (Authentication):

import firebase_admin
from firebase_admin import auth

firebase_admin.initialize_app()
user = auth.get_user(uid)
custom_token = auth.create_custom_token(uid)

Both libraries offer authentication functionality, but Authlib provides a more generic OAuth implementation, while Firebase Admin SDK is tailored specifically for Firebase services. Authlib requires more manual configuration but offers greater flexibility across different authentication protocols. Firebase Admin SDK provides a more streamlined experience for Firebase-specific authentication tasks and integrates seamlessly with other Firebase services.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Build Status Python Version

Firebase Admin Python SDK

Table of Contents

Overview

Firebase provides the tools and infrastructure you need to develop apps, grow your user base, and earn money. The Firebase Admin Python SDK enables access to Firebase services from privileged environments (such as servers or cloud) in Python. Currently this SDK provides Firebase custom authentication support.

For more information, visit the Firebase Admin SDK setup guide.

Installation

To install Firebase Admin Python SDK, simply execute the following command in a terminal:

pip install firebase-admin

Contributing

Please refer to the CONTRIBUTING page for more information about how you can contribute to this project. We welcome bug reports, feature requests, code review feedback, and also pull requests.

Supported Python Versions

We currently support Python 3.7+. However, Python 3.7 support is deprecated, and developers are strongly advised to use Python 3.8 or higher. Firebase Admin Python SDK is also tested on PyPy and Google App Engine environments.

Documentation

License and Terms

Firebase Admin Python SDK is licensed under the Apache License, version 2.0.

Your use of Firebase is governed by the Terms of Service for Firebase Services.