JavaScript HTML CSS Objective-C Java TypeScript Ruby
Clone or download
Latest commit 73a749b Aug 10, 2018
Permalink
Failed to load latest commit information.
assistant-say-number Update dependencies Aug 7, 2018
authenticated-json-api Fixed all ESLint issues Aug 9, 2018
authorized-https-endpoint Fixed all ESLint issues Aug 9, 2018
bigquery-import Update dependencies Aug 7, 2018
child-count Fixed all ESLint issues Aug 9, 2018
convert-images Fixed all ESLint issues Aug 9, 2018
coupon-on-purchase Fixed all ESLint issues Aug 9, 2018
crashlytics-integration add tags Aug 10, 2018
delete-old-child-nodes Update dependencies Aug 7, 2018
delete-unused-accounts-cron Fixed all ESLint issues Aug 9, 2018
developer-motivator Update dependencies Aug 7, 2018
email-confirmation Fixed all ESLint issues Aug 9, 2018
exif-images Fixed all ESLint issues Aug 9, 2018
fcm-notifications Update dependencies Aug 7, 2018
ffmpeg-convert-audio Fixed all ESLint issues Aug 9, 2018
fulltext-search-firestore Fixed all ESLint issues Aug 9, 2018
fulltext-search Update dependencies Aug 7, 2018
generate-thumbnail Fixed all ESLint issues Aug 9, 2018
github-to-slack Fixed all ESLint issues Aug 9, 2018
google-sheet-sync Fixed all ESLint issues Aug 9, 2018
image-maker Update dependencies Aug 7, 2018
image-sharp Update dependencies Aug 7, 2018
instagram-auth Fixed all ESLint issues Aug 9, 2018
isomorphic-react-app Update dependencies Aug 7, 2018
lastmodified-tracking Update dependencies Aug 7, 2018
limit-children Fixed all ESLint issues Aug 9, 2018
line-auth Fixed all ESLint issues Aug 9, 2018
linkedin-auth Fixed all ESLint issues Aug 9, 2018
message-translation Update dependencies Aug 7, 2018
minimal-webhook Update dependencies Aug 7, 2018
moderate-images Fixed all ESLint issues Aug 9, 2018
nextjs-with-firebase-hosting Fixed all ESLint issues Aug 9, 2018
paypal Fixed all ESLint issues Aug 9, 2018
presence-firestore Update dependencies Aug 7, 2018
quickstarts Fixed all ESLint issues Aug 9, 2018
spotify-auth Update dependencies Aug 7, 2018
stripe Update dependencies Aug 7, 2018
survey-app-update Update dependencies Aug 7, 2018
template-handlebars Fixed all ESLint issues Aug 9, 2018
text-moderation Update dependencies Aug 7, 2018
typescript-getting-started Update dependencies Aug 7, 2018
url-shortener Update dependencies Aug 7, 2018
user-data-cleanup Fix link. Oct 19, 2017
username-password-auth Fixed all ESLint issues Aug 9, 2018
.gitignore Changing all existing samples for v1.0.0 launch Mar 26, 2018
.travis.yml Fixed travis testing Aug 9, 2018
CONTRIBUTING.md Update `github` variable to the current repo name (#247) Oct 6, 2017
ISSUE_TEMPLATE.md Update ISSUE_TEMPLATE.md Mar 31, 2017
LICENSE Update LICENSE Jan 13, 2018
README.md Update README with a pointer to older samples on master Aug 7, 2018
lerna.json Enable tests, ignore compiled files Mar 2, 2018
package.json Fixed travis testing Aug 9, 2018

README.md

All the samples on this branch are using the Node 8 runtime which is in beta and use ES2017 syntax. If you'd like to use ES2015 syntax because you're using the Node 6 runtime have a look at the master branch.

Cloud Functions for Firebase Sample Library

This repository contains a collection of samples showcasing some typical uses of Cloud Functions for Firebase.

What's Cloud Functions for Firebase?

Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code. Cloud Functions for Firebase integrates the Firebase platform by letting you write code that responds to events and invokes functionality exposed by other Firebase features.

Prerequisites

To learn how to get started with Cloud Functions for Firebase by having a look at our Getting Started Guide, trying the quickstart samples and looking at the documentation.

Samples Overview

This repository contains a few categories of samples:

  1. Quickstarts are minimal examples for each types of triggers.
  2. Development Environment Samples and Boilerplates illustrates how to get started with different, commonly used JavaScript development patterns such as Typescript, React SSR, ES2017 etc...
  3. Image Processing shows a few ways where you can process and transform images using Cloud Functions such as generating thumbnails, converting images extracting metadata...
  4. Firebase Realtime Database Data Consistency shows how to implement automatic data consistency such as keeping a count of children, having a max amount of node childs, cleaning up old data etc... for your Realtime Database.
  5. Other common usecases a set of other common usecases for Cloud Functions.

Quickstarts

Here is a set of minimal samples for each Cloud Functions trigger types.

Quickstart: Uppercaser for Realtime Database or Cloud Firestore

This quickstart sample demonstrates using Cloud Functions triggered by Firebase Realtime Database or Cloud Firestore events. The function transforms message text written to Firestore to uppercase.

HTTPS trigger quickstart: Time Server

This quickstart sample demonstrates using Cloud Functions triggered by HTTPS requests. The function returns the current server time and allows for date time formatting.

Hosting triggered HTTPS function quickstart: Big Ben

This quickstart demonstrates using Cloud Functions with an HTTPS trigger that's triggered through a Firebase Hosting URL. The function will display a repeated number of "BONG"s depending on the hour of the day.

Cloud Storage trigger quickstart: Thumbnail generator

This quickstart sample demonstrates using Cloud Functions triggered by Firebase Storage events. The function generates a thumbnail of uploaded images.

Auth trigger quickstart: Welcome Email

This quickstart sample demonstrates using Cloud Functions triggered by Firebase Auth events. The function sends a Welcome Email when user accounts are created (or when users sign-in using an Identity Provider for the first time) and sends a Goodbye Email when user accounts are deleted.

PubSub trigger quickstart: Hello World

This quickstart sample demonstrates using Cloud Functions triggered by PubSub events. The functions log the PubSub payload in a Hello world message.

Crashlytics trigger quickstart: Crashlytics Email Notifier

This quickstart sample demonstrates using Cloud Functions triggered by Crashlytics. The functions send an email when there is a new issue in an important conversion flow.

Development Environment Samples and Boilerplates

The Firebase CLI generates sample code for Cloud Functions using JavaScript that is natively supported in NodeJS. Most developers use more modern JavaScript or TypeScript which requires additional tools.

Write Cloud Functions using TypeScript

This sample shows how you can write your Cloud Functions code using Typescript. This requires a build step, included as an npm script, which transpiles the source code to a supported version of JavaScript. Uses an HTTPS request.

Server-side generated pages w/ Handlebars templating and user sessions

This sample shows how to serve server-side generated HTML pages using the HandlebarsJs templating system and serve user-specific content by always passing the Firebase ID token in a __session cookie.

Isomorphic React App

This sample demonstrates how to create an isomorphic React application with server-side data fetching. This sample uses Cloud Functions, Hosting, and the Realtime Database.

Image Processing

Here is a few sample that shows how you can process or analyze images using Cloud Functions.

Image Maker

This sample demonstrates how to create various customized images such as sparkline or sphere charts through Cloud Functions and Hosting and serve it to the client. Uses an HTTP trigger.

Convert images after upload

Demonstrates how to automatically convert images that are uploaded to Firebase Storage to JPEG using ImageMagick. Uses a Firebase Storage trigger.

Generate image thumbnails using ImageMagick

Demonstrates how to automatically generate a thumbnail for images that are uploaded to Firebase Storage using ImageMagick and generate a public download link for the images. Uses a Firebase Storage trigger.

Generate image thumbnails using Node.js Stream & Sharp

Demonstrates how to use Node.js Stream to read image from Cloud Storage, generate a thumbnail image using Sharp and upload it back to Cloud Storage. Uses a Firebase Storage trigger.

Moderate offensive images

Demonstrates how to automatically moderate offensive images that are uploaded to Firebase Storage by using the Google Cloud Vision API to detect offensive images and ImageMagick to blur these images. Uses a Firebase Storage trigger.

Extract image metadata

Demonstrates how to automatically extract image's metadata using ImageMagick for images that are uploaded to Firebase Storage. Uses a Firebase Storage trigger.

Firebase Realtime Database Data Consistency

These samples show how to implement automatic data consistency such as keeping a count of children, having a max amount of node childs, cleaning up old data etc...

LastModified Firebase Database tracking

Tracking when the Firebase Database (or a subset) was last modified. Uses a Realtime Database trigger.

Firebase Database child nodes count

Keeps track of the number of child nodes of a Firebase Database element allowing clients to filter or order results using the child count. This can be useful to keep track of the number of "likes" or "followers" of something shared through social media. Uses a Realtime Database trigger.

Limit number of child nodes

Makes sure that the number of child nodes stays below a certain threshold. This can be useful to limit the number of lines of logs or chat history below a given number. Uses a Realtime Database trigger.

Removing old items from a list

This sample shows how to remove child nodes older than 2 hours from a Firebase Database list. This can be useful for removing outdated items from a collection. Uses a Realtime Database trigger.

Other common usecases

Send FCM notifications

This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification from a Realtime Database triggered Function when users get new followers. The sample also features a Web UI to experience the FCM notification. Uses a Realtime Database trigger.

Google Assistant says ordinal of given number

This sample shows how to create an action for the Google Home/Assistant using the Actions SDK hosted on Cloud Functions. The sample action asks users to say a number and reads out the ordinal of that number. Uses an HTTP trigger.

Authenticated JSON API

This sample shows how to authenticate access to a JSON API to only allow access to data for a specific Firebase user. Uses an HTTP trigger.

Authorized HTTP endpoint

This sample shows how to restrict an HTTPS Function to only the Firebase users of your app. Only users who pass a valid Firebase ID token as a Bearer token in the Authorization header of the HTTP request or in a __session cookie are authorized to use the function. Checking the ID token is done with an ExpressJs middleware that also passes the decoded ID token in the Express request object. Uses an HTTP trigger.

Authorize with LinkedIn, Spotify, Instagram, LINE or Basic Auth

Demonstrates how to authorize with a 3rd party sign-in mechanism, create a Firebase custom auth token, update the user's profile and authorize Firebase. Uses an HTTP trigger.

Post GitHub commits to Slack channel

Demonstrates how to automatically post GitHub commits to a Slack channel using an HTTPS triggered Function.

Create and charge customers with Stripe or Paypal

Demonstrates hows to integrate Firebase Auth and the Realtime database with Stripe via the Stripe Node.js library and whows how to create HTTP endpoints to charge customers via Paypal.

Text moderation

Demonstrates how to moderate user input text for bad words. This can be used to moderate usernames, chat or forum messages. Uses a Realtime Database trigger.

Email confirmation

Sends email confirmation after users subscribed to a mailing list. Uses a Realtime Database trigger.

Automatic message translation

Integrates the Google Translate API to perform automatic text translation across any number of languages. Language codes can be stored in Firebase for on the fly changes. Uses a Realtime Database trigger.

Automatic URL shortener

Integrates the Bit.ly API to shorten URLs automatically as they are added to the database. Uses a Realtime Database trigger.

Full-text search via Algolia for Realtime Database or Cloud Firestore

Enable full-text search on Firebase Database data or Cloud Firestore documents by using an Algolia hosted search service. Uses a Realtime Database or Cloud Firestore trigger.

User data cleanup

Deletes all associated user data in the Realtime database when a user deletes his Firebase account. Uses an Auth trigger. This code has moved to its own repo at https://github.com/firebase/user-data-protection

Export your data to a Google Spreadsheet

This sample demonstrates how to sync new data written to a Firebase database to a Google Sheet. It includes a method for obtaining, storing, and using Oauth2 tokens for Google API access. Uses HTTPS triggers and Realtime Database triggers.

Export your data to Big Query

Copies Firebase Database elements into BigQuery automatically. This can be useful for instance for further logs analysis. Uses a Realtime Database trigger.

Webhook upon Firebase Database writes

Writing to the Firebase Database triggers a request to a callback URL (a Webhook). The content of the modified Data is sent to the Webhook. Uses a Realtime Database trigger.

Send a survey when users update your app

This sample shows how to send a survey to your users who have updated your app. App Update is detected using a Firebase Analytics event. Uses an Analytics trigger.

Send a coupon to user who have completed a purchase

This sample shows how to send a coupon to your users who have just purchased something. 10% off on your next purchase! Uses an Analytics trigger.

Delete inactive users accounts via cron

Periodically deletes the accounts of users who have not signed in in the last month. Uses an HTTPS trigger.

Developer Motivator

This sample demonstrates how to send a Firebase Cloud Messaging (FCM) notification to the developer device each time your app gains or loses a user. Uses an Analytics trigger.

Audio Files Conversion

This sample uses ffmpeg / fluent-ffmpeg and automatically converts audio files that are uploaded to Cloud Storage to FLAC file format with mono-channel audio @ 16000hz. Uses a Storage trigger.

Presence for Cloud Firestore

Build a simple online / offline status indicator for your users by leveraging Cloud Firestore and Realtime Database together. Uses a Realtime Database trigger.

Create Jira Issue upon new Crashlytics issue

Automatically creates a new issue in your Jira project when a new issue is reported in Crashlytics. Uses a Crashlytics trigger.

Notify a Slack channel upon new Crashlytics issue

Automatically sends a Slack notification to a specific Slack channel when a new issue is reported in Crashlytics. Uses a Crashlytics trigger.

Contributing

We'd love that you contribute to the project. Before doing so please read our Contributor guide.

License

© Google, 2015-2017. Licensed under an Apache-2 license.

Build Status

Build Status