Overview

Refit is a type-safe REST Client for .NET Core, Xamarin and .Net - developed by Paul Betts. It is inspired by Square's Retrofit library. Refit makes it relatively easy to make calls to REST API, without writing much of wrapping code.

In this article, we are going to explain how to use Refit, with focus on its common features.

Using Refit

To use Refit, you need basically three things:

  • Model class to map the JSON data
  • Interface which defines possible REST operations
  • RestService class to generate the implementation of the interface in which you defined the possible REST operations

Exercise: Using Refit to query stock trading data using iextrading API.

In this exercise, we are going to use refit to create a wrapper around iextrading API to fetch the stock data. The iextrading API is a free REST API to fetch the stock data.

If I want to fetch Apple's company information, iextrading has an endpoint for that:

https://api.iextrading.com/1.0/stock/AAPL/company

where AAPL is the stock symbol.

It gives you the company information in JSON format:

{
    "symbol": "AAPL",
    "companyName": "Apple Inc.",
    "exchange": "Nasdaq Global Select",
    "industry": "Computer Hardware",
    "website": "http://www.apple.com",
    "description": "Apple Inc is designs, manufactures and markets mobile communication and media devices and personal computers, and sells a variety of related software, services, accessories, networking solutions and third-party digital content and applications.",
    "CEO": "Timothy D. Cook",
    "issueType": "cs",
    "sector": "Technology",
    "tags": [
        "Technology",
        "Consumer Electronics",
        "Computer Hardware"
    ]
}

So, the task here is to call the company endpoint from C# and get the response back as C# entity without writing much of low-level code.

Let's start with adding Refit NuGet package. Go to manage NuGet packages, select Browse tab, search for refit and add the Refit package by Paul Betts.

 

Create a model for the company data being returned as JSON:

public class Company
    {
        [JsonProperty("symbol")]
        public string Symbol { get; set; }

        [JsonProperty("companyName")]
        public string CompanyName { get; set; }

        [JsonProperty("exchange")]
        public string Exchange { get; set; }

        [JsonProperty("industry")]
        public string Industry { get; set; }

        [JsonProperty("website")]
        public string Website { get; set; }

        [JsonProperty("description")]
        public string Description { get; set; }

        [JsonProperty("CEO")]
        public string Ceo { get; set; }

        [JsonProperty("issueType")]
        public string IssueType { get; set; }

        [JsonProperty("sector")]
        public string Sector { get; set; }

        [JsonProperty("tags")]
        public List<string> Tags { get; set; }
    }

 

Now, let's define the interface and the operation GetCompany:

public interface IIexTrading
{
    [Get("/1.0/stock/{symbol}/company")]
    Task<Company> GetCompanyAsync(string symbol);
}

Company symbol is the only variable we need to fetch the company information, the rest of the URL is constant.

Refit uses the metadata provided to generate the implementation.

All set, now let's call the API:

var iexTradingClient = RestService.For<IIexTrading>("https://api.iextrading.com");

var company = await iexTradingClient.GetCompanyAsync("AAPL");

That's it, pretty easy, right? Please share your thoughts in the comments section.

Here is the source code.