Home life · Updated
How to Access a Firebase Database From Your App
Quick answer You access a Firebase database by registering your app in the Firebase console, copying its config file or snippet into your project, and then using the Firebase SDK to read and write data. The steps differ slightly depending on whether you use Realtime Database or Cloud Firestore.
Most people searching this are building a web or mobile app and want their first read or write to work. The fastest route is the Firebase console plus the matching SDK for your platform, and that method covers both Firebase databases. The case where it differs: if you are trying to open a database from a server or script rather than an app, you authenticate with a service account instead of an app config, and that is covered in its own section below.
Connect Your App to Firebase in 8 Steps
- Create a Firebase project. Go to the Firebase console in a browser, sign in with a Google account, and click Add project. Give it a name and accept the defaults.
- Register your app. On the project overview page, click the web, Android or iOS icon that matches your platform, and follow the prompts. For Android you supply a package name; for iOS a bundle ID; for web a nickname is enough.
- Download or copy the config. Android gets a google-services.json file, iOS gets a GoogleService-Info.plist file, and web gets a JavaScript object with keys like apiKey and projectId. Place the file in the standard location for your platform (for Android, the app module’s root folder) or paste the snippet into your code.
- Install the SDK. For web, install the Firebase npm packages (firebase plus the modules you need) or add them via a script tag. For Android, add the Google services Gradle plugin and apply it in the build files. For iOS, add the Firebase pods with CocoaPods and run pod install.
- Initialize Firebase in code. Call the initializeApp function (web) or set up FirebaseApp (mobile) once at startup, passing the config. This must run before any database call.
- Create the database. Back in the console, open Build, then either Firestore Database or Realtime Database, and click Create database. Choose a location close to your users, then start in test mode or locked mode.
- Write a test record. Use the console’s data editor to add a small piece of data, such as a document with a field, or a JSON node. This gives your code something to read.
- Read the data from your app. Use the SDK’s read call, run your app, and confirm the test record appears in your logs or on screen. Menu labels can differ slightly by SDK version.
Realtime Database or Firestore: Which One You Are Accessing
Firebase runs two databases, and the access code is not interchangeable, so identify yours before writing code. Realtime Database stores everything as one large JSON tree, is often the one in older tutorials, and is reached through a database URL ending in firebaseio.com. Cloud Firestore stores documents inside collections, scales better for structured data, and is the default choice in the console today. If your search results show ref() and on() calls, you are looking at Realtime Database code; if they show collection() and getDocs(), that is Firestore. Both live under Build in the console’s left menu, and you can use both in the same project if needed.
Reading and Writing Once You Are Connected
With Firestore, a write is a set call on a document reference, and a one-time read is a get call on the same reference; a live read uses onSnapshot, which fires again whenever the data changes. With Realtime Database, a write is set on a database reference, a one-time read is get or once, and a live read is on with a value listener. The rule of thumb: use one-time reads for data that rarely changes, and listeners for anything the user should see update live, such as a chat message list. Every listener you attach stays active until you detach it, so remove listeners when a screen closes to avoid wasted reads.
Accessing Firebase From a Server or Script
The app-config method above assumes the code runs on a user’s device. For a backend server, a scheduled job or a plain script, you instead create a service account: in the console, open Project settings, then Service accounts, and click Generate new private key, which downloads a JSON credentials file. Load that file with the Admin SDK for your language and initialize the app with it. Keep the credentials file out of version control and never place it in a public web folder, because anyone holding that file has full read and write access to your project.
When Access Fails: Symptom, Cause and Fix
Almost every access problem comes from one of a small set of causes, and each shows a distinctive error.
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission denied on every read or write | Database rules block unauthenticated access | Set test-mode rules while developing, then write rules that require auth |
| App crashes on startup | Config file missing or in the wrong folder | Re-download the config from project settings and place it in the standard location |
| API key not valid error | Config keys copied from a different project | Re-copy the snippet from the same project that holds the database |
| Reads return nothing, no error | Wrong collection, document or node path | Compare the exact path in your code with the tree in the console’s data viewer |
| Long delays before data appears | Database region far from your users | Check the location shown in the console; a new project in a closer region is the only remedy |
Checklist Before You Start and How to Tell It Worked
Before writing any code, confirm you have a Google account, a Firebase project with the database created, the SDK installed, and the config file or snippet in place. A quick way to tell access is working: write a record from the console, read it from your app, then write a record from your app and refresh the console’s data viewer to watch it appear. If data flows both directions, your access setup is complete, and anything that fails afterwards is a rules or code issue rather than a connection issue.
One habit worth forming early: keep the console’s data viewer open in a browser tab while you test. It shows writes landing within a second or two, which makes it obvious whether a problem is in your write code or your read code.
Related
- How to Use Firebase for Authentication
- How to Set Up Firebase Authentication
- How to Use Firebase: Set Up, Auth, and Database
Sources: Google Firebase Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.