---
title: "How to use ASP.NET Core APIs in the class library?"  
description: "How to use ASP.NET Core APIs in the class library?"  
author: "Ashutosh Patel"  
published: 2023-03-06  
updated: 2023-07-08  
canonical: https://www.mindstick.com/forum/157445/how-to-use-asp-dot-net-core-apis-in-the-class-library  
category: "asp.net core"  
tags: ["asp.net", "asp.net core"]  
reading_time: 3 minutes  

---

# How to use ASP.NET Core APIs in the class library?

How to use [ASP.NET Core](https://www.mindstick.com/articles/12946/get-started-with-asp-dot-net-core-mvc-and-visual-studio) [APIs](https://www.mindstick.com/articles/338302/types-of-apis-a-comprehensive-guide) in the [class library](https://www.mindstick.com/forum/158577/what-is-the-significance-of-the-base-class-library-bcl-in-the-dot-net-framework)?

## Replies

### Reply by Aryan Kumar

To use an [ASP.NET](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Core API in a [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) [library](https://www.mindstick.com/forum/34615/software-framework-vs-library), you can use the `HttpClient` class. The `HttpClient` class provides a way to send HTTP requests to an API.

To use the `HttpClient` class, you first need to create an instance of the class. You can do this by calling the `CreateHttpClient()` method.

Once you have created an instance of the `HttpClient` class, you can use it to send a request to the API. You can do this by calling the `GetAsync()` method. The `GetAsync()` method takes the URL of the API as its parameter.

The `GetAsync()` method will return a `Task` object. The `Task` object will contain the response from the API. You can then get the response from the `Task` object by calling the `Result()` method.

The response from the API will be a `HttpResponseMessage` object. The `HttpResponseMessage` object contains the status code of the response, the headers of the response, and the body of the response.

You can then use the status code, headers, and body of the response to do whatever you need to do with the response.

The following is an example of how to use the `HttpClient` class to use an ASP.NET Core API in a class library:

C#

```plaintext
using System;
using System.Net.Http;

namespace MyClassLibrary
{
    public class Class1
    {
        public static void Main()
        {
            // Create an instance of the HttpClient class
            var client = new HttpClient();

            // Send a request to the API
            var response = await client.GetAsync("https://api.example.com/");

            // Get the response from the request
            var result = await response.Result();

            // Check the status code of the response
            int statusCode = result.StatusCode;

            // Check the headers of the response
            string[] headers = result.Headers.Keys;

            // Get the body of the response
            string body = await result.Content.ReadAsStringAsync();

            // Do something with the response
        }
    }
}
```

In this example, we first create an instance of the `HttpClient` class. We then send a request to the API by calling the `GetAsync()` method. The `GetAsync()` method takes the URL of the API as its parameter.

The `GetAsync()` method will return a `Task` object. The `Task` object will contain the response from the API. We then get the response from the `Task` object by calling the `Result()` method.

The response from the API will be a `HttpResponseMessage` object. We then check the status code of the response, the headers of the response, and the body of the response.

Finally, we do something with the response.


---

Original Source: https://www.mindstick.com/forum/157445/how-to-use-asp-dot-net-core-apis-in-the-class-library

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
