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.
- Open Visual Studio and click on “Create a new project.”

- Select “Console App (.NET 6)” from the list of templates.

- Give your project a name, for example “GetApiDemo” and click on “Create.”

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.
class GetResponse { public int UserId { get; set; } public int Id { get; set; } public string Title { get; set; } }
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.
var client = new HttpClient(); client.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/");
We are using the JSON placeholder mock API https://jsonplaceholder.typicode.com/, 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
var response = client.GetAsync("todos/1").Result;
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 (response.IsSuccessStatusCode) { var responseContent = response.Content.ReadAsStringAsync().Result; var getResponse = System.Text.Json.JsonSerializer.Deserialize<GetResponse>(responseContent); Console.WriteLine("Get successful!"); Console.WriteLine("UserId: " + getResponse.UserId); Console.WriteLine("Id: " + getResponse.Id); Console.WriteLine("Title: " + getResponse.Title); } else { Console.WriteLine("Error: " + response.StatusCode); }
If we now start the application we should see the following output:
Get successful! UserId: 0 Id: 0 Title:
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.