Refit – Conquer all the APIs


It is no news that REST APIs are the de facto standard for cross-tier communications between most data-driven applications. Also, there`s a big chance that you have the need to consume some of these APIs from either an internal or external service.

Creating clients to consume APIs is one of the classic scenarios in which people keep reinventing the wheel. Why do we keep reinventing the wheel? My theory is that it`s due to a mix of these reasons:

  1. Lack of awareness. They simply ignore the fact that there are libraries that do what they need to do.
  2. A policy against adding dependencies. Some teams take this too far.
  3. An underestimation of the problem: “I’m a brave coder and this is just a silly API client. I don’t need a library to do it!”

The result is almost always the same:

Well… this must end, once and for all. No excuses!

Know Refit

Refit is the library that will do all the heavy lifting for you.

Its usage cannot be simpler: You tell Refit the structure of the API and it generates the client for you.

The funny thing is it does it by using an interface as the skeleton of the API. This interface is the only piece of code we need to provide. Pure glory.


First of all, we need to define an interface that resembles the API we want to consume. We will also need to provide some additional data as attributes, to make Refit know exactly how to generate the client.

For example, imagine that we want to consume the GitHub API. We can go with an interface like this:

public interface IGitHubApi
Task<User> GetUser(string user);

Notice the attribute above the method definition. It’s given the extra information about how the GetUser method maps to the API endpoint related to that call, so Refit can associate it to the API itself. It means that GetUser is mapped to base_url + “/users/{user}” and that the HTTP method to use is GET.

Next step is to make Refit generate our client and to use it:

var gitHubApi = RestService.For<IGitHubApi>("");

var octocat = await gitHubApi.GetUser("octocat");

With the first line, we’re telling Refit to generate a client using the interface we created before as a template. The result of the operation is the client itself, ready to consume the API.

The second line is where we invoke the GetUser method. The outcome will be the result of the invocation.

Easy peasy.

As you see, we had to write almost no code to generate a robust client.

This is a good starting point to learn more and you will be delighted that almost all complex operations are already handled for you by using clean elegant solutions.

You can customize everything, from the usage complex types, to security, file handling and more.

In summary  

Refit is something that can save you and your team a lot of effort and time, it’s beautifully designed and covers almost any usage you can imagine. Take a look to the project page in GitHub. The description contains very valuable documentation!


Written by José Manuel Nieto


Stay up to date!

Leave a comment