Skip to content

How to Call GET API in C#

How to Call GET API in C#

How to Call GET API in C#: A Step-by-Step Guide

Making a GET request to an API is a common task when working with web services, and with C#, it’s easy to accomplish this task using the built-in JSON converter. In this article, we will create a simple project demonstrating How to Call GET API in C# and how to deserialize the JSON response to a C# object using the System.Text.Json namespace.

Creating a New Console Application in Visual Studio

First, we will start by creating a new console application in Visual Studio. We will name it “GetApiDemo”. This step is necessary to create the foundation for our project and have a place to add our code.

Become a C# PRO and land your first developer job!
  • Open Visual Studio and click on “Create a new project.”
How to Call GET API in C#
  • Select “Console App (.NET 6)” from the list of templates.
How to Call GET API in C#
  • Give your project a name, for example “GetApiDemo” and click on “Create.”
How to Call GET API in C#

Defining the Structure of the Data Received from the API

Next, we will create a class called “GetResponse” representing the data we want to receive from the API as a JSON object. This class will have properties such as “UserId”, “Id”, and “Title”. This step is necessary to define the structure of the data we will receive from the API.


Creating an HttpClient Object and Setting the API Base Address

Now we can move on to our Main method, where we will create a new HttpClient object, allowing us to make the GET request. We will set the base address of the API. This step is necessary to create the HttpClient object that handles the API’s connection.

We are using the JSON placeholder mock API, a free fake API for testing and prototyping. Evidently, if you need to post to a different URI, you can change this address to whichever you need. Just keep in mind that this API in particular, does not return any useful data apart from the fact that we can use it to test our logic. So if you only see “0” responses, don´t worry because it is expected.


Making the GET Request to the API

Next, we will make the actual GET request by calling the HttpClient’s “GetAsync” method. This method takes in the endpoint of the API as its parameter. This step is necessary to send the request to the API and retrieve the data we want.


Checking the Response and Deserializing the JSON Response

Once we have the response, we can check the status code to see if the request was successful. If it was, we can then read the response content and use the System.Text.Json namespace to deserialize it to a C# object. This step is necessary to check if the request was successful and convert the json response to a C# object that we can work with.

If we now start the application we should see the following output:


Conclusion: How to Call GET API in C#

That’s it for this article. We hope this was helpful in showing you How to Call GET API in C#.

If you’re new to C# and collections, it’s always a good idea to start with basic examples and then build on them as you gain more experience. And don’t forget that when in doubt, you can always refer to this article to help you understand how to Call GET API in C#. Also, if you are interested in more API related topics, check out this article about calling POST API in C#!

If you want to skyrocket your C# career, check out our powerful ASP.NET FULL-STACK WEB DEVELOPMENT COURSE, which also covers test-driven development and C# software architecture.

Leave a Reply

Your email address will not be published. Required fields are marked *