xxxxxxxxxx
from firebase_functions import firebase_functions
def hello_world(request):
return 'Hello, World!'
firebase_functions.https.on_request(hello_world)
xxxxxxxxxx
import firebase_admin
from firebase_admin import credentials, firestore, initialize_app
# Initialize Firebase
cred = credentials.Certificate('path/to/serviceAccountKey.json')
firebase_admin.initialize_app(cred)
# Get a reference to the Firestore database
db = firestore.client()
# Define the Firebase Function
def my_function(data, context):
# Access incoming data
name = data['name']
age = data['age']
# Perform some operations
result = name.upper() + " is " + str(age) + " years old."
# Update Firestore document
doc_ref = db.collection('users').document()
doc_ref.set({'result': result})
# Export the Firebase Function
exported_function = my_function
# Deploy the Firebase Function
# Run this command in your terminal: firebase deploy --only functions
xxxxxxxxxx
# Here is a sample from https://cloud.google.com/functions/docs/calling/cloud-firestore#functions_firebase_firestore-python
import json
def hello_firestore(data, context):
""" Triggered by a change to a Firestore document.
Args:
data (dict): The event payload.
context (google.cloud.functions.Context): Metadata for the event.
"""
trigger_resource = context.resource
print('Function triggered by change to: %s' % trigger_resource)
print('\nOld value:')
print(json.dumps(data["oldValue"]))
print('\nNew value:')
print(json.dumps(data["value"]))