Create an account
The first thing you need to do to get started with Firebase is sign up for
a free account. A brand new Firebase app will automatically be created for you with its
own unique database URL ending in firebaseio.com. We'll use this database URL to
store and sync data.
Install Firebase
To include the Firebase client library in your website, add a script tag to the
<head> section of your HTML file. We recommend including the
library directly from our CDN:
<script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script>
Firebase is also available as a Bower dependency via "bower install firebase".
Node.js Setup
The Firebase JavaScript API and the Firebase Node.js API are exactly the same. Firebase clients run just as easily on your servers as they do on end-user devices. We offer a Node.js module which can be installed via npm from the command line:
$ npm install firebase --save
To use the Firebase Node.js module in your application, just require the
Firebase client library.
var Firebase = require("firebase");
Read & Write Data
To read and write data to and from a Firebase database, we need to first create a reference
to it. We do this by passing the URL of our Firebase app into the Firebase
constructor:
var myFirebaseRef = new Firebase("https://<YOUR-FIREBASE-APP>.firebaseio.com/");
There are several other ways to create a reference to a location in our database. These are all explained in our understanding data section.
Writing Data
Once we have a reference to our database, we can write any valid JSON object to it
using set(). Our guide on saving data
explains the different write methods the API offers and how to know when the data has been
successfully written.
myFirebaseRef.set({
title: "Hello World!",
author: "Firebase",
location: {
city: "San Francisco",
state: "California",
zip: 94103
}
});
Reading Data
Reading data from our database is accomplished by attaching callbacks and handling
the resulting events. Assuming we already wrote to myFirebaseRef above, we can
retrieve the city value by using the on() method:
myFirebaseRef.child("location/city").on("value", function(snapshot) {
alert(snapshot.val()); // Alerts "San Francisco"
});
DataSnapshot returned from the callback to
access a JSON object.
In the example above, the value event will fire once for the initial state of the
data, and then again every time the value of that data changes. You can learn more about the
various event types and how to handle event data in our
documentation on reading data.
Deploy to Firebase Hosting
Firebase Hosting lets us deploy our application's static files (HTML, CSS, JavaScript, etc.)
to the web with a single command. To get started, download firebase-tools via npm:
$ npm install -g firebase-tools
Read through our hosting quickstart to get up and running in minutes. Firebase Hosting is a production-grade service, with security, reliability, and scalability baked-in. We host content on a global CDN and even provision an SSL certificate automatically for you so you can be up-and-running with only the following command:
$ firebase deploy
Authenticate Your Users
Firebase provides full support for authenticating users with Email & Password, Facebook, Twitter, GitHub, Google, or your existing authentication system.
To get started with Email & Password auth, enable the Email & Password provider in the App Dashboard:
- Choose the Login & Auth tab.
- Select the Email & Password tab and enable authentication.
Now that the authentication provider is enabled you can create a new user:
myFirebaseRef.createUser({
email : "[email protected]",
password : "correcthorsebatterystaple"
}, function(error, userData) {
if (error) {
console.log("Error creating user:", error);
} else {
console.log("Successfully created user account with uid:", userData.uid);
}
});
Once you've created your first user, you can log them in using the authWithPassword method.
Learn how to authenticate via Facebook, Twitter, Google or your own custom system in our User Authentication guide.
Secure Your Data
Powerful expression-based Security and Firebase Rules provide fine-grained control over who has access to our data and allow us to validate writes to our database data:
{
"rules": {
".read": true,
".write": "auth.uid === 'admin'",
".validate": "newData.isString() && newData.val().length < 500"
}
}
Our Security and Firebase Rules are enforced consistently whenever data is accessed. The rules language is designed to be both powerful and flexible, so that you can maintain fine-grained control over your application's data.
What's Next?
- Read the Development Guide
- View the full JavaScript API
- Explore our code examples