How to Call RESTful APIs in C# (Demo using Zomato API)

Sena Kılıçarslan
C# Programming
Published in
6 min readMar 12, 2019

In this post, I will demonstrate how to make calls to a RESTful API from a C# application. I decided to use Zomato API in the demo as it is an official and well-documented API.

RESTful APIs conform to the REST architectural style. Before jumping to implementation, let’s learn about REST (or refresh what we know about that).

What is REST?

REST, or REpresentational State Transfer, is an architectural style for providing standards between computer systems on the web, making it easier for systems to communicate with each other.

REST relies on client-server relationship. This essentially means that client application and server application must be able to evolve separately without any dependency on each other.

REST is stateless. That means the communication between the client and the server always contains all the information needed to perform the request. There is no session state in the server, it is kept entirely on the client’s side.

REST provides a uniform interface between components. Resources expose directory structure-like URIs.

REST is not strictly related to HTTP, but it is most commonly associated with it. There are four basic HTTP verbs we use in requests to interact with resources in a REST system:

  • GET — retrieve a specific resource (by id) or a collection of resources
  • POST — create a new resource
  • PUT — update a specific resource (by id)
  • DELETE — remove a specific resource by id

In a REST system, representations transfer JSON or XML to represent data objects and attributes.

REST has had such a large impact on the Web that it has mostly displaced SOAP-based interface design because it’s a considerably simpler style to use.

In addition, I suggest this video for a visual representation of the concept.

Now that we have made a quick review about REST, we can continue to the implementation.

How to Call a RESTful API?

As I mentioned earlier, we will call Zomato’s API for this demo.

First, we go to the Zomato API page and get an API key.

In the documentation tab, we see how to use the API endpoints.

We will use /search endpoint to view the list of the restaurants in a city. But before that, we need the ID of the city to use in this endpoint. So we first use /cities endpoint as follows:

And we learn that city id is 59 for Istanbul.

To see the restaurants in Istanbul, we enter the following parameters to the /search endpoint as below:

After clicking Try it Out button we get the API URL as:

https://developers.zomato.com/api/v2.1/search?entity_id=59&entity_type=city

We append our API key to this URL :

https://developers.zomato.com/api/v2.1/search?entity_id=59&entity_type=city&apikey={Your API Key}

Then we paste it to the browser and get the JSON response as shown below:

We can use http://jsoneditoronline.org/ to view the tree structure of JSON data. I pasted the Raw data version of JSON to the site and got the following:

We will use this object tree structure in our code to form the JSON model classes that will be used in deserialization.

Now, let’s create a Console application in Visual Studio and add the necessary model classes.

For the outermost object we create RestaurantList class:

As you see in the picture it consists of an array called restaurants. We create the following classes to define array items:

And each restaurant object includes a location object so we create the following class:

I want to point out that it is enough for us to include the fields that we will need in the class definitions. In addition, please pay attention to the data annotation we used before the field definitions. That is JsonProperty and it is important to add in order to match the class fields to the JSON data fields.

Now, let’s use these classes to list restaurants in Istanbul.

Before writing the API call classes, we should install the Web API Client Libraries package. In the Package Manager Console, we type the following command:

Install-Package Microsoft.AspNet.WebApi.Client

After executing the command, necessary libraries are added to our project.

Next, we create the APICall class as below:

Here the main function is:

This function

· creates the HTTP client with the URL parameters

· sends a GET request to the specified URI an asynchronous operation

· serializes the HTTP content to a string as an asynchronous operation

· deserializes the JSON to a .NET object (of type T )

I want to point out that we use generics (type T ) here so that we can use this function to deserialize responses from different types of endpoints to their associated .NET objects.

Next, we add the ZomatoInfo class which will call RunAsync function of APICall class:

Please note that you should enter your API key in urlParameters.

Finally, we call this function from our Program class in our application:

And get the result:

Hopefully, you could follow me and got some result. Congrats then :)

Moving forward, I used /reviews endpoint to view reviews about Nusr-Et Steakhouse (we see that id = 5915530 for this restaurant from the above screenshot). After following the aforementioned steps for this endpoint and implementing these, I got the current reviews for Nusr-Et as shown below:

You can see the full project from this Github repository. In a nutshell, to show the reviews, I just added the necessary JSON model classes and implemented GetReviews function in ZomatoInfo class and then called this from the main program. Although I used a different model class for deserialization, I did not make any change in APICall class. This is the power of Generics :)

Lastly, I want to add two helpful videos here. You can watch them in case you want to find out more about the two concepts we used during the implementation which are Asynchronous Programming and JSON Deserialization.

This is where I end this post. I hope you found it easy to read and understand.

Bye!

P.S. I am not sponsored by Zomato or Nusr-Et :)

References

https://docs.microsoft.com/tr-tr/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client

https://www.codecademy.com/articles/what-is-rest

https://developer.ibm.com/articles/ws-restful/

https://spring.io/understanding/REST

https://ninenines.eu/docs/en/cowboy/2.6/guide/rest_principles/

https://restfulapi.net/rest-architectural-constraints/

--

--

Sena Kılıçarslan
C# Programming

A software developer who loves learning new things and sharing these..