Firebase REST APIs: Loading & Querying Data Simplified 101

on API, Firebase, REST API, Tutorials • November 3rd, 2021 • Write for Hevo

Firebase REST API - Featured Image

One of the major tools that can help businesses grow much faster is through their business app. Everything is going cloud and mobile, thereby owning an app is an essential strategy to reach a wider audience and boost sales. Though, there is always a challenge to develop an app, maintain it regularly, store the data securely with real-time sync, fix the bugs as soon as possible, and analyse the app’s data for deeper insights into your firm’s performance. All of this becomes fluid by using a brilliant platform like Firebase.

Firebase is an elegant solution offered by Google that can assist you in seamlessly building and running successful apps. Firebase also provides a Realtime Database to safely store and sync your data between users in real-time. Using the Firebase REST APIs, you can effectively load and query your data from the database. As the flow of data to and fro from the Firebase Database is smooth and quick, Firebase REST APIs are effective for mobile apps such as live streaming, chat messages, etc.

In this article, you will learn how to precisely use the Firebase REST APIs for Loading and Querying data from your Firebase Database.

Table of Contents

Introduction to Firebase

Firebase REST API - Firebase Logo
Image Source

Firebase is the one-stop solution for developing and maintaining apps that accelerate your business growth. Simplifying the app development process, this Backend as a Service(BaaS) allows you to create and run iOS, Android, and Web Apps. From App development and testing to analysing your app performance and engaging with your audience, Firebase provides a diverse set of tools to smoothly handle all these tasks for you.

Features such as Firebase Database allow you to store and sync your data in a Real-Time NoSQL database program in JSON-like documents between your users. With tools such as Firebase CrashAnalytics and Google Analytics, you can continuously monitor the performance and stability of your app with real-time analysis. It also assists in optimizing your apps for the best user experience via Rich Analytics, A/B testing, and Messaging Campaigns. Availability of Open-source codes to automate general Development Tasks, Web Hosting, Testing features, etc, allows developers to focus more on enhancing the user experience.

Key Features of Firebase

Since its inception in 2011, Firebase has evolved into a package of attractive attributes that takes care of common app development challenges for you. Some of its most eye-catching features are as follows:

  • Realtime Database: Firebase offers a real-time NoSQL Database hosted on a cloud. Any change made by a Client will be reflected instantly for the other connected clients allowing your users to access real-time data from anywhere, anytime, or any device. Firebase Database completely eliminates the need for servers by providing mobile and web SDKs for building your apps. You can also work offline as the data is stored safely in the local cache that is automatically synced to your database whenever you come back online.
  • Accelerated Analytics: Firebase is seamlessly integrated with Google Analytics allowing reporting of 500 unique events for free. It is a unified platform for monitoring all your important metrics like demographics, app engagement, retention to average in-app purchase revenues, etc. This provides deeper insights into your app’s performance allowing you to make the necessary tweaks such as optimizing your Advt campaigns. Another brilliant feature is the Firebase Crash Analytics providing you with real-time crash reports. Firebase effectively segments the errors into manageable groups allowing you to comfortably understand the issues and take the required actions to solve them.
  • Machine Learning Capabilities: Firebase is equipped with a Machine learning Kit and ready-to-use ML APIs that you can integrate with your mobile apps. This can be essentially utilised to perform basic functions like identifying the faces and text, scanning barcodes, labelling images, and recognizing location landmarks. Using the Firebase ML Model Deployment you can distribute your custom models to your users on air.
  • User Engagement: Using Google Adwords, you can design, launch and analyse marketing campaigns for your apps. App Indexing is another technique to check customer engagement by surfacing in-app content within Google Search results. This can also help you rank higher in the Search Engine Results Pages(SERPs). You can send referrals and Invites via Email or SMS. To stay connected with your customers, you can send in Notifications by scheduling messages in order to engage users at the right times of the day.
  • Firebase Authentication: With the backend services, easy-to-use SDKs, and pre-designed UI libraries, you can authenticate users to your app. Along with the email id Password and phone authentication, you can also sign in via popular platforms such as Twitter, Google, Facebook, GitHub, etc. 

Introduction to REST APIs

Firebase REST API - REST API logo
Image Source

An Application programming interface(API) is a set of rules that describe how devices or applications connect and interact with each other. The API establishes an accurate method for you to write a program that requests services from an operating system or other application. 

A REST API is an API that follows the design protocols of REST (Representational State Transfer Architectural Style). This is generally employed for Web APIs that use HTTP requests to access and use data.

Firebase REST APIs allow you to make requests to the Firebase Database for reading, writing, updating, or removing data. You can define any Firebase Realtime Database URL as a REST endpoint by adding .json in the end. Keeping your data safe, you can use the Firebase REST API to send requests via an HTTPS client.

Simplify Firebase and REST API ETL Using Hevo’s No-code Data Pipeline

Hevo Data is a No-code Data Pipeline that offers a fully managed solution to set up Data Integration for 100+ Data Sources (Including 40+ Free Sources such as Firebase and REST APIs) and will let you directly load data from your sources to a Data Warehouse or the Destination of your choice. Hevo provides a Native REST API connector that allows loading data from non-native or custom sources for free automating your data flow in minutes without writing any line of code. Its fault-tolerant architecture makes sure that your data is secure and consistent. Hevo provides you with a truly efficient and fully automated solution to manage data in real-time and always have analysis-ready data.

Get Started with Hevo for Free

Let’s look at some of the salient features of Hevo:

  • Fully Managed: It requires no management and maintenance as Hevo is a fully automated platform.
  • Data Transformation: It provides a simple interface to perfect, modify, and enrich the data you want to transfer. 
  • Real-Time: Hevo offers real-time data migration. So, your data is always ready for analysis.
  • Schema Management: Hevo can automatically detect the schema of the incoming data and map it to the destination schema.
  • Connectors: Hevo supports 100+ Integrations (Including 40+ Free Sources such as Firebase and REST APIs) with SaaS platforms, Files, Databases, Analytics, and BI tools. It supports various destinations including Google BigQuery, Amazon Redshift, Snowflake Data Warehouses; Amazon S3 Data Lakes; and MySQL, SQL Server, TokuDB, DynamoDB, PostgreSQL Databases to name a few.  
  • Secure: Hevo has a fault-tolerant architecture that ensures that the data is handled in a secure, consistent manner with zero data loss.
  • Hevo Is Built To Scale: As the number of sources and the volume of your data grows, Hevo scales horizontally, handling millions of records per minute with very little latency.
  • Live Monitoring: Advanced monitoring gives you a one-stop view to watch all the activities that occur within pipelines.
  • Live Support: Hevo team is available round the clock to extend exceptional support to its customers through chat, email, and support calls.
Sign up here for a 14-Day Free Trial!

Critical Aspects of Firebase REST APIs

Firebase REST API - Client Database Relationship
Image Source

Firebase REST APIs can be used to query data by taking Firebase Realtime Database URL as a REST endpoint. You can append .json at the end of the URL and can send requests using the cURL commands. To completely understand the Firebase REST API, let’s discuss the following 2 Critical Aspects:

A) Basic Firebase REST APIs Operations

There 4 basic HTTPS requests you can send to an endpoint namely:

  • GET

To read your data from the Database, you can send the GET request via the Firebase REST API. In the example below, the Curl Command is used to retrieve the user’s name.

curl 'https://[PROJECT_ID].firebaseio.com/users/jack/name.json' 

Output

You will get a 200 OK HTTP status code for a successful request. The response will contain the desired user name.

{"first": "Jack", "last": "Sparrow"}
  • PUT

You can use PUT requests for writing data into the database via the Firebase REST API. In this example, the user’s name is being entered into the system.

curl -X PUT -d '{ "first": "Jack", "last": "Sparrow" }' 
 'https://[PROJECT_ID].firebaseio.com/users/jack/name.json'

Output

{"first": "Jack", "last": "Sparrow"}
  • POST

This provides the equivalent functionality of the JavaScript push() method. 

curl -X POST -d '{"user_id" : "jack", "text" : "Ahoy!"}' 
  'https://[PROJECT_ID].firebaseio.com/message_list.json'

Output

The data in response will contain the child name for the new data described in the POST request.

{"name": "-INOQPH-aV_psbk3ZXEX"}
  • PATCH

If you want to update specific children without overwriting the existing data, you can send in the PATCH request.

curl -X PATCH -d '{"last":"Jones"}' 
 'https://[PROJECT_ID].firebaseio.com/users/jack/name/.json'

Output

The response will contain the data specified in the PATCH request.

{"last": "Jones"} 
  • DELETE

Using the Firebase REST API, you can easily remove data by sending a DELETE request. If the request is successful, you will get a 200 OK HTTP response with JSON null.

curl -X DELETE 
  'https://[PROJECT_ID].firebaseio.com/users/jack/name/last.json'

B) Firebase REST APIs Query Parameters     

While querying your Database, you can use specific parameters for a more defined result. Firebase REST API accepts the following parameters:

  • Auth or access_token

The auth or access_token parameter guards your data according to the Firebase Realtime Database Rules. This is supported by all request types. In this example, the GET request is used with the auth parameter where CREDENTIAL is either your Firebase app’s secret or an authentication token.

curl 'https://docs-examples.firebaseio.com/auth-example.json?auth=CREDENTIAL
  • Shallow

This parameter is specially made for dealing with large datasets without any need of downloading everything. Using the Firebase REST API, you can set it as true to limit the data that is returned. The values will be reduced to True for a JSON Object whereas for JSON primitive data all the values are simply returned.

curl 'https://[PROJECT_ID].firebaseio/.json?shallow=true'
  • print

You can set the format in which you receive the response data via the Firebase REST API. Setting print as pretty will present the request data in human-readable form whereas print=silent returns a 204 No Content on success. 

  • Print = pretty
curl 'https://[PROJECT_ID].firebaseio.com/users/jack/name.json?print=pretty'
  • Print = silent
curl -X PUT -d '{ "first": "Jack", "last": "Sparrow" }' 
  'https://[PROJECT_ID].firebaseio.com/users/jack/name.json?print=silent'
  • callback

While retrieving data via the GET request, you can use JSONP to wrap the response in a JavaScript callback function. 

<script>
  function gotData(data) {
    console.log(data);
  }
</script>
<script src="https://[PROJECT_ID].firebaseio.com/.json?callback=gotData"></script>

Here, the callback function is added to have the Firebase REST API wrap the returned data in the callback function you specify.

  • download

By adding download =, you can initiate a file download from the web browser via the GET requests. This allows the REST service to include the necessary headers so that browsers know to save the data to a file.

curl 'https://[PROJECT_ID].firebaseio/.json?download=myfilename.txt'
  • timeout

You can also set a time limit on how long the read takes on the server-side. If the read time exceeds the timeout value, it will terminate with an HTTPS 400 error. You specify the time in ms, s, or min with the default time being 15 min if no value is entered.

curl 'https://docs-examples.firebaseio.com/rest/retrieving-data.json?timeout=10s'
  • Order by

You can get sorted data in your query response by using the Order by statement. There are 4 variations in the Order by command namely Child Key, Key, Value, and Priority. For information on the ordering rules, you can visit the Firebase Database Order by Section.

  • Filter

You can filter your queries by using a combination of orderby and the following factors namely limitToFirst, limitToLast, startAt, endAt, and equalTo.

In this example, the child key “height” is passed to orderby by setting a filter using startAT to retrieve data with a height of more than 3.

curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="height"&startAt=3&print=pretty'

 For more information, you can visit the Firebase Database Filtering Data section. 

Conclusion

In this article, you have learned how to accurately use the Firebase REST APIs for querying the Firebase Realtime Database. Firebase provides an extensive set of tools that assists in the app development, launching, testing, crash reports, etc. To grow your business at a faster pace, Firebase offers solutions like App Performance Analysis, Predictive Analytics, App Indexing, Dynamic links, etc. You can also enjoy the seamless integration with various platforms in the Google Ecosystem such as Google Analytics, AdWords, Data Studio, etc. Getting Data for custom analysis can be done comfortably by Querying your Database via Firebase REST APIs.

As you build and run your App using Firebase, you will gradually experience the astronomical amount of data that will be generated. Apart from Firebase, you will be using numerous other applications in your business across several departments for Accounting, Human Resources, Customer Relationship Management, etc. Extracting data from applications outside the Google Environment can be a resource-extensive job as each application has an individually complex and ever-evolving API. To handle all this efficiently, you would require to invest a portion of your Engineering Bandwidth to Integrate, Clean, Transform, and Load your data into your data warehouse for further analysis. All of this can be comfortably automated by a Cloud-Based ETL Tool like Hevo Data.

Visit our Website to Explore Hevo

Hevo Data, a No-code Data Pipeline can seamlessly transfer your data from a collection of sources like Firebase and REST APIs into your Data Warehouse or a destination of your choice for free. It is a reliable, secure, and fully automated service that does not require you to write any code! 

If you are using Firebase and REST APIs in your business and looking for a No-fuss alternative to Manual Data Integration, then Hevo can effortlessly automate this for you. Hevo provides a Native REST API connector that allows loading data from non-native or custom sources for free, without writing any code. Hevo with its strong integration with 100+ Sources & BI tools (Including 40+ Free Sources such as Firebase and REST APIs), allows you to not only export & load Data but also transform & enrich your Data & make it analysis-ready. 

Want to take Hevo for a ride? Sign Up for a 14-day free trial and simplify your Data Integration process. Check out the pricing details to get a better understanding of which plan suits you the most.

Share with us your experience of using Firebase REST APIs to query your Firebase Database. Let us know in the comments section below!  

No-code Data Pipeline for Firebase & REST APIs