Here we will see how to create a wrapper around Firebase REST API with C#, so .Net devlopers can consume it quickly.
Section 1 Create App in Firebase Console
Log into your Firebase account and click on "GO TO CONSOLE" to access the console.

Click on "Add Project" to add a new Firebase project.

Give "Project Name" of your choice, then select your Country/Region and hit "CREATE PROJECT".

Now, you will be navigated to your newly created App Dashboard with all the Firebase features listed. Click on "Database" from the left pane.

You can see your service plan as Spark which is Free. All the data stored is represented as JSON tree. You have database URL along with the root of JSON tree, which is null as of now. This database URL will be used to make HTTP request later in the article.
Section 2 Explore Firebase REST API with Postman
Before making wrapper, let’s get comfortable with Firebase REST API with Postman. You can get postman from here (https://www.getpostman.com/apps).
Firebase read/write access is secured by default as you can see in RULES.

For the sake of simplicity in making HTTP requests, we will remove authentication for this article. In upcoming articles, we will continue with authentication.

If you are already familiar with Firebase REST API, you can directly jump to Section 3.
Writing Data with PUT
I have some data about a team and its members as follows. We are writing this data to root node of our database JSON tree.
- {
- "Team-Awesome": {
- "Members": {
- "M1": {
- "City": "Hyderabad",
- "Name": "Ashish"
- },
- "M2": {
- "City": "Cyberabad",
- "Name": "Vivek"
- },
- "M3": {
- "City": "Secunderabad",
- "Name": "Pradeep"
- }
- }
- }
- }
In Postman window, set method as PUT; in URL, enter your database URL appended with .json as we are writing to root node.
The .json suffix is always required if we are making REST calls. In Body, select raw and paste your JSON data there, hit "Send" now to make the request.

You can see response code "200 OK" in Postman instead of 201 Created, but this is how Firebase returns it. Every request will return "200 OK" if successful, irrespective of the Request method.

Created data will immediately be reflected to your app console. You can verify it.

Reading Data with GET
To read data, we can give path to specific node which we wish to read. Here, I just want the details of Member M1 so I have given specific path in URL. Set method as GET and hit "Send" to retrieve the data.

It would return the details of a specific member.

Pushing Data with POST
In Firebase, POST pushes data to the specific node with auto-generated key; it never replaces existing data.
Here, we are adding scores for all team members, so our node is Scores here which is not already available in JSON tree. Firebase will automatically create it.

You can see in console Scores node is created and data is added along with random keys.

Updating Data with PATCH
To update data in a node we have to use PATCH, it can be used to add new fields also. Here we will update city of a member.

You can observe in console whether value is updated.

Removing Data with DELETE
To remove a resource DELETE is used. Let’s delete Member M3.

Member M3 is removed, can be seen in console.

Section 3 Building C# wrapper
Add new project of type Class Library with name FirebaseNet, or name of your choice.
I have taken .Net Standard here for broader compatibility, you can take .Net framework class library if it’s not available.

Create folder Database and add 4 classes as follows.
- FirebaseDB.cs
- FirebaseRequest.cs
- FirebaseResponse.cs
- UtilityHelper.cs
Let’s look into each file one by one.
FirebaseDB.cs
- namespace FirebaseNet.Database
- {
- using System;
- using System.Net.Http;
- /// <summary>
- /// FirebasDB Class is reference of a Firebase Database
- /// </summary>
- public class FirebaseDB
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="FirebaseDB"/> class with base url of Firebase Database
- /// </summary>
- /// <param name="baseURL">Firebase Database URL</param>
- public FirebaseDB(string baseURL)
- {
- this.RootNode = baseURL;
- }
- /// <summary>
- /// Gets or sets Represents current full path of a Firebase Database resource
- /// </summary>
- private string RootNode { get; set; }
- /// <summary>
- /// Adds more node to base URL
- /// </summary>
- /// <param name="node">Single node of Firebase DB</param>
- /// <returns>Instance of FirebaseDB</returns>
- public FirebaseDB Node(string node)
- {
- if (node.Contains("/"))
- {
- throw new FormatException("Node must not contain '/', use NodePath instead.");
- }
- return new FirebaseDB(this.RootNode + '/' + node);
- }
- /// <summary>
- /// Adds more nodes to base URL
- /// </summary>
- /// <param name="nodePath">Nodepath of Firebase DB</param>
- /// <returns>Instance of FirebaseDB</returns>
- public FirebaseDB NodePath(string nodePath)
- {
- return new FirebaseDB(this.RootNode + '/' + nodePath);
- }
- /// <summary>
- /// Make Get request
- /// </summary>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Get()
- {
- return new FirebaseRequest(HttpMethod.Get, this.RootNode).Execute();
- }
- /// <summary>
- /// Make Put request
- /// </summary>
- /// <param name="jsonData">JSON string to PUT</param>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Put(string jsonData)
- {
- return new FirebaseRequest(HttpMethod.Put, this.RootNode, jsonData).Execute();
- }
- /// <summary>
- /// Make Post request
- /// </summary>
- /// <param name="jsonData">JSON string to POST</param>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Post(string jsonData)
- {
- return new FirebaseRequest(HttpMethod.Post, this.RootNode, jsonData).Execute();
- }
- /// <summary>
- /// Make Patch request
- /// </summary>
- /// <param name="jsonData">JSON sting to PATCH</param>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Patch(string jsonData)
- {
- return new FirebaseRequest(new HttpMethod("PATCH"), this.RootNode, jsonData).Execute();
- }
- /// <summary>
- /// Make Delete request
- /// </summary>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Delete()
- {
- return new FirebaseRequest(HttpMethod.Delete, this.RootNode).Execute();
- }
- /// <summary>
- /// To String
- /// </summary>
- /// <returns>Current resource URL as string</returns>
- public override string ToString()
- {
- return this.RootNode;
- }
- }
- }
- FirebaseDB is main class user interacts with, it exposes the API to be consumed by user of library.
- It’s constructor takes baseURI of Firebase Database as parameter and assigns to RootNode private property.
- Class is using Method Chaining to maintain resource URI with the help of Node() & NodePath() They are taking string as input and appending to RootNode and returns FirebaseDB object with combined URI. Here Node can take only single node whether NodePath is for multiple Nodes.
- Class has methods for each Firebase supported HTTP method which are returning FirebaseResponse Put, Post & Patch methods are having parameter to accept JSON data as string, whether Get & Delete don’t need it. They all are instantiating FirebaseRequest with HttpMethod object according to request type, resource URI as RootNode and optional parameter jsonData as string.
- ToString() is overridden here to return current resource URI.
FirebaseRequest.cs



Hương ThuPosted Dec 22, 2020, 9:56 AM
"jsonContent": "{\n \"error\" : \"Permission denied\"\n}\n", "errorMessage": "Unauthorized : Unauthorized",
Hương ThuPosted Dec 22, 2020, 9:56 AM
What should I do if I would want to use auth not null in rules to firebase?
Akash OmrePosted Jul 5, 2019, 2:13 AM
Thanks Ashish Your solution works for me. it was really helpful while i am looking for Third party library to integrate this.
Former memberPosted Jun 26, 2019, 8:22 AM
Dear Ashish.There are angular app + web api crud operations.it works fine on localhost.But when deploy to firebase web api crud operation parts not working.not fetching data from mssql.But it works fine on localhost.Do we need to set something web api connection setting on firebase console?
Uday OletiPosted Nov 2, 2018, 11:28 PM
Dear ashish it is very helpful artical.I have one requirement, can we use firebase as real time database likequery style. I have a huge data in my firebase db instead of getting hole data through get method i want to get CHILD ELEMENTS of specific range.
Leandro SouzaPosted Jul 23, 2018, 2:56 PM
Hello, I have a problem running this way when published in localhost, apparently an address conversion occurs to an ip that is unrelated to my project, but despite this it seems to belong to firebase, I wonder what I can do to which when run published, it also make the connection normally, follows google drive images for visualization of the error stack.Thank you, Leandro https://drive.google.com/open?id=1vU9dV617pwIDZndZUd4obFcdn0Ne8Clo https://drive.google.com/open?id=1_DPAUiDo9TjPOwXIai_QTKDztGKcQUrY
Anas AlSadiPosted May 4, 2018, 1:57 PM
Hi sir I have error { "error" : "No data supplied." } in PUT method In Postman why?? can help me please
Tajinder DhaliwalPosted Nov 27, 2017, 10:05 AM
In yours code you describe very well like how data updated automatically in fire-base database .Can you please share any example code for updating client on update in database.Like if i update fire-base database by post request then client (.net) will also get updated record so that i can update it on UI too. I know it using fire-base (Google friendly chat official tutorial) .I want to know how to achieve this in c# .Net. Thank you.
Faheem KathradaPosted Sep 5, 2017, 8:11 AM
What should I do if I would want to use authentication to firebase?
Kashif AsifPosted Jul 18, 2017, 12:44 AM
I appreciate you. nice article keep i up (y)