This site has moved.
This Google Apps Script library provides access to the Firebase Realtime Database, a NoSQL cloud database provided by Google for storing and sync your app's data.
Users can read & write data in any database they have access to. This library is based on the Firebase REST API.
INSTALLATION - REFERENCE - TUTORIALS - SOURCE
getDatabaseByUrl(url, optSecret)
Retrieves a database by url.
function myFunction() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
Logger.log(database.getData());
}
Parameters
Return
Database - the Database found at the given URL
Class Database
Returns the data at this path.
function readDataFromFirebase() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
Logger.log(database.getData("users/jack/name"));
}
Parameters
Return
Object - the data found at the given path or null if no data
Returns an array of data from each paths.
This method is using UrlFetchApp.fetchAll() method to perform a batch of HTTPS requests.
It is way faster than making multiple getData() calls as calls are parallelized (eg: 2s to retrieve info from 1500 different locations / paths compared to 75s with a loop of getData()).
This method is mostly useful when you need to retrieve several items from a list (eg: several user profiles from your list of user profiles) without using queries (either because of performance issues or because the items you want to retrieve can't be aggregated via a single query).
function myFunction() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
// paths of 3 different user profiles
var path1 = "users/jack";
var path2 = "users/bob";
var path3 = "users/jeane";
Logger.log(database.getAllData([path1, path2, path3]));
}
Parameters
Name
requests
Type
Array
Description
an array composed of:
paths (as Strings),
and / or JavaScript objects specifying each request path & optQueryParameters
Return
Array[] - an array of data from each paths (and null values when no data in specific paths)
Generates a new child location using a unique key.
function addDataInFirebase() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
var data = {"user_id" : "jack", "text" : "Ahoy!"};
Logger.log(database.pushData("message_list", data));
}
Parameters
Return
String - the child name of the new data that was added
Write data at the specified path.
function writeDataInFirebase() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
var data = {"first":"Jack","last":"Sparrow"};
database.setData("users/jack/name", data);
}
Parameters
Update specific children at the specified path without overwriting existing data.
function updateDataInFirebase() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
var data = { "last": "Jones" };
Logger.log(database.updateData("users/jack/name", data));
}
Parameters
Delete data at the specified path.
function removeDataInFirebase() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
Logger.log(database.removeData("users/jack"));
}
Parameters
Return
null
Generates an authorization token to Firebase.
https://firebase.google.com/docs/auth/server/create-custom-tokens
To be used with signInWithCustomToken() method on client side.
Since v3 of Firebase SDK, you need a service account and its private key to generate a valid auth token instead of the Firebase app secret.
To get it, open your Firebase project & click on Settings > Permissions, then Service accounts. Create a new service account & download the private key.
function createFirebaseAuthToken() {
var baseUrl = "https://samplechat.firebaseio-demo.com/";
var secret = "rl42VVo4jRX8dND7G2xoI";
var database = FirebaseApp.getDatabaseByUrl(baseUrl, secret);
var serviceAccountEmail = "auth-token-service-account@firebase-asfds.iam.gserviceaccount.com";
var privateKey = "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCPt236u6qZw9WtsZaCjuGVqA1fhUedaCNTWSudDg==\n-----END PRIVATE KEY-----\n";
var token = database.createAuthToken(
Session.getEffectiveUser().getEmail(),
null,
serviceAccountEmail,
privateKey)
};
Logger.log(token);
}
Parameters
Return
String - the auth token granting access to firebase