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
- namespace FirebaseNet.Database
- {
- using System;
- using System.Net.Http;
- /// <summary>
- /// Firebase Request
- /// </summary>
- class FirebaseRequest
- {
- /// <summary>
- /// Suffix to be added in each resource URI
- /// </summary>
- private const string JSON_SUFFIX = ".json";
- /// <summary>
- /// Initializes a new instance of the <see cref="FirebaseRequest"/> class
- /// </summary>
- /// <param name="method">HTTP Method</param>
- /// <param name="uri">URI of resource</param>
- /// <param name="jsonString">JSON string</param>
- public FirebaseRequest(HttpMethod method, string uri, string jsonString = null)
- {
- this.Method = method;
- this.JSON = jsonString;
- if (uri.Replace("/", string.Empty).EndsWith("firebaseio.com"))
- {
- this.Uri = uri + '/' + JSON_SUFFIX;
- }
- else
- {
- this.Uri = uri + JSON_SUFFIX;
- }
- }
- /// <summary>
- /// Gets or sets HTTP Method
- /// </summary>
- private HttpMethod Method { get; set; }
- /// <summary>
- /// Gets or sets JSON string
- /// </summary>
- private string JSON { get; set; }
- /// <summary>
- /// Gets or sets URI
- /// </summary>
- private string Uri { get; set; }
- /// <summary>
- /// Executes a HTTP requests
- /// </summary>
- /// <returns>Firebase Response</returns>
- public FirebaseResponse Execute()
- {
- Uri requestURI;
- if (UtilityHelper.ValidateURI(this.Uri))
- {
- requestURI = new Uri(this.Uri);
- }
- else
- {
- return new FirebaseResponse(false, "Proided Firebase path is not a valid HTTP/S URL");
- }
- string json = null;
- if (this.JSON != null)
- {
- if (!UtilityHelper.TryParseJSON(this.JSON, out json))
- {
- return new FirebaseResponse(false, string.Format("Invalid JSON : {0}", json));
- }
- }
- var response = UtilityHelper.RequestHelper(this.Method, requestURI, json);
- response.Wait();
- var result = response.Result;
- var firebaseResponse = new FirebaseResponse()
- {
- HttpResponse = result,
- ErrorMessage = result.StatusCode.ToString() + " : " + result.ReasonPhrase,
- Success = response.Result.IsSuccessStatusCode
- };
- if (this.Method.Equals(HttpMethod.Get))
- {
- var content = result.Content.ReadAsStringAsync();
- content.Wait();
- firebaseResponse.JSONContent = content.Result;
- }
- return firebaseResponse;
- }
- }
- }
- FirebaseRequet class is where actual magic happens. Constructor takes Http Method, Firebase URI and optional JSON data as parameter. It also appends .json to URI which is necessary to make REST calls, In base base URI it should be /.json while URI with any Node needs only .json to be appended.
- Execute() is responsible for making HTTP calls with help of RequestHelper() of UtilityHelper It validates URI and JSON data, if invalid then it returns FirebaseResponse with Success=False and proper ErrorMessage.
- If all parameters are valid, it makes HTTP calls, and forms FirebaseResponse object and returns it. If Request if of type GET then it parses response content to string and appends it to FirebaseResponse object to be returned to caller.
- RequestHelper() is an Asynchronous method, which is returns Task<HttpResponseMessage>, but in Execute method with use of Wait() and Result we are handling it synchronously. In further enhancement Asynchronous API will also be exposed to consumer.
FirebaseResponse.cs
- namespace FirebaseNet.Database
- {
- using System.Net.Http;
- /// <summary>
- /// Firebase Response
- /// </summary>
- public class FirebaseResponse
- {
- /// <summary>
- /// Initializes a new instance of the <see cref="FirebaseResponse"/> class
- /// </summary>
- public FirebaseResponse()
- {
- }
- /// <summary>
- /// Initializes a new instance of the <see cref="FirebaseResponse"/> class
- /// </summary>
- /// <param name="success">If Success</param>
- /// <param name="errorMessage">Error Message</param>
- /// <param name="httpResponse">HTTP Response</param>
- /// <param name="jsonContent">JSON Content</param>
- public FirebaseResponse(bool success, string errorMessage, HttpResponseMessage httpResponse = null, string jsonContent = null)
- {
- this.Success = success;
- this.JSONContent = jsonContent;
- this.ErrorMessage = errorMessage;
- this.HttpResponse = httpResponse;
- }
- /// <summary>
- /// Gets or sets Boolean status of Success
- /// </summary>
- public bool Success { get; set; }
- /// <summary>
- /// Gets or sets JSON content returned by the Request
- /// </summary>
- public string JSONContent { get; set; }
- /// <summary>
- /// Gets or sets Error Message if Any
- /// </summary>
- public string ErrorMessage { get; set; }
- /// <summary>
- /// Gets or sets full Http Response
- /// </summary>
- public HttpResponseMessage HttpResponse { get; set; }
- }
- }
FirebaseResponse represents output of API calls against FirebaseDB. It contains 4 public properties as follows.
- Success
- JSONContent
- ErrorMessage
- HttpResponse
- In Firebase.Net I have tried to not surprise the consumer by throwing any unexpected error, so FirebaseResponse has boolean property Success, which tells you whether call is successful or not, along with proper message in ErrorMessage
- Error will not always be a Http Error, it could be related to invalid JSON or improper URI also, they both are handled here.
- If HTTP call is successful Success will have True, ErrorMessage will be null, HttpResponse will contain complete HttpResponseMessage and if request type is GET then JSONContent will contain the response JSON as string.
UtilityHelper.cs
- namespace FirebaseNet.Database
- {
- using System;
- using System.Net.Http;
- using System.Text;
- using System.Threading.Tasks;
- using Newtonsoft.Json.Linq;
- /// <summary>
- /// Utility Helper Class
- /// </summary>
- class UtilityHelper
- {
- /// <summary>
- /// User Agent Header in HTTP Request
- /// </summary>
- private const string USER_AGENT = "firebase-net/1.0";
- /// <summary>
- /// Validates a URI
- /// </summary>
- /// <param name="url">URI as string</param>
- /// <returns>True if valid</returns>
- public static bool ValidateURI(string url)
- {
- Uri locurl;
- if (System.Uri.TryCreate(url, UriKind.RelativeOrAbsolute, out locurl))
- {
- if (
- !(locurl.IsAbsoluteUri &&
- (locurl.Scheme == "http" || locurl.Scheme == "https")) ||
- !locurl.IsAbsoluteUri)
- {
- return false;
- }
- }
- else
- {
- return false;
- }
- return true;
- }
- /// <summary>
- /// Validates JSON string
- /// </summary>
- /// <param name="inJSON">JSON to be validatedd</param>
- /// <param name="output">Valid JSON or Error Message</param>
- /// <returns>True if valid</returns>
- public static bool TryParseJSON(string inJSON, out string output)
- {
- try
- {
- JToken parsedJSON = JToken.Parse(inJSON);
- output = parsedJSON.ToString();
- return true;
- }
- catch (Exception ex)
- {
- output = ex.Message;
- return false;
- }
- }
- /// <summary>
- /// Makes Asynchronus HTTP requests
- /// </summary>
- /// <param name="method">HTTP method</param>
- /// <param name="uri">URI of resource</param>
- /// <param name="json">JSON string</param>
- /// <returns>HTTP Responce as Task</returns>
- public static Task<HttpResponseMessage> RequestHelper(HttpMethod method, Uri uri, string json = null)
- {
- var client = new HttpClient();
- var msg = new HttpRequestMessage(method, uri);
- msg.Headers.Add("user-agent", USER_AGENT);
- if (json != null)
- {
- msg.Content = new StringContent(
- json,
- UnicodeEncoding.UTF8,
- "application/json");
- }
- return client.SendAsync(msg);
- }
- }
- }
- UtilityHelper contains 3 static methods to keep our keep running.
- ValidateURI() takes URI as string and validates it using TryCreate() of Uri class and returns Boolean.
- TryParseJSON() validates and format JSON string, Firebase does not allow JSON which uses single quotes instead of double quotes, but in C# most people will pass in single quotes because of multiline support, very few will use escape character for double quotes. That’s why I’m parsing it which validates JSON as well as calling ToString() on JToken object will returns me well formatted JSON in double quotes.
- If valid returns True and out parameter output contains Formatted JSON.
- If invalid then returns False and out parameter output contains Error Message.
- RequestHelper() is an asynchrnous method, which is responsibe for making all HTTP calls to Firebase Database.
Section 4 Library in Action
Let’s consume our newly created library and see how it works. Create New Console App and give name of your choice.

Now update your Program.cs with following code.
Program.cs
- namespace Examples.NetCore
- {
- using FirebaseNet.Database;
- using static System.Console;
- /// <summary>
- /// Examples
- /// </summary>
- public class Program
- {
- /// <summary>
- /// Main Method
- /// </summary>
- public static void Main()
- {
- // Instanciating with base URL
- FirebaseDB firebaseDB = new FirebaseDB("https://c-sharpcorner-2d7ae.firebaseio.com");
- // Referring to Node with name "Teams"
- FirebaseDB firebaseDBTeams = firebaseDB.Node("Teams");
- var data = @"{
- 'Team-Awesome': {
- 'Members': {
- 'M1': {
- 'City': 'Hyderabad',
- 'Name': 'Ashish'
- },
- 'M2': {
- 'City': 'Cyberabad',
- 'Name': 'Vivek'
- },
- 'M3': {
- 'City': 'Secunderabad',
- 'Name': 'Pradeep'
- }
- }
- }
- }";
- WriteLine("GET Request");
- FirebaseResponse getResponse = firebaseDBTeams.Get();
- WriteLine(getResponse.Success);
- if(getResponse.Success)
- WriteLine(getResponse.JSONContent);
- WriteLine();
- WriteLine("PUT Request");
- FirebaseResponse putResponse = firebaseDBTeams.Put(data);
- WriteLine(putResponse.Success);
- WriteLine();
- WriteLine("POST Request");
- FirebaseResponse postResponse = firebaseDBTeams.Post(data);
- WriteLine(postResponse.Success);
- WriteLine();
- WriteLine("PATCH Request");
- FirebaseResponse patchResponse = firebaseDBTeams
- // Use of NodePath to refer path lnager than a single Node
- .NodePath("Team-Awesome/Members/M1")
- .Patch("{\"Designation\":\"CRM Consultant\"}");
- WriteLine(patchResponse.Success);
- WriteLine();
- WriteLine("DELETE Request");
- FirebaseResponse deleteResponse = firebaseDBTeams.Delete();
- WriteLine(deleteResponse.Success);
- WriteLine();
- WriteLine(firebaseDBTeams.ToString());
- ReadLine();
- }
- }
- }
Execution

- Here, I am calling all the methods in API. First I am instantiating FirebaseDB object with base URI of my Firebase App, you may replace it with your App URI. Then in next line Node(“Teams”) is used to point object to a different resource URI. “Teams” don’t need to be already available in your Firebase database.
- Then we have some JSON data in which is a multiline JSON string using single quotes, It is supported in Library but Firebase. So you can use it here.
- Next, we have "Get() call" which is returning FirebaseResponse in getResponse. It is always a good idea to verify success before calling getResponse.JSONContent, like I am doing here.
- Now we are calling Put() & Post() with given data, behaviour of these HTTP calls is already described in Section 2.
- Patch() is interesting here because of 2 reasons, First it is used along with NodePath() which is accessing 3 nodes depth. And second JSON data is passed using escape characters for double quotes, which is how Firebase actually accepts data.
- Now "Delete()" called a method which deletes complete JSON tree at given URI.
- Finally ToString() method prints current resource URI of Firebase Database.
I will keep adding new features to this library, you can track in further articles and repository. You can also contribute here.
Current stage of code can be found here.

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)