gitmyhub

refit

C# ★ 9.5k updated 9h ago

The automatic type-safe REST library for .NET Core, Xamarin and .NET. Heavily inspired by Square's Retrofit library, Refit turns your REST API into a live interface.

A C# library that turns a simple interface definition into a fully working HTTP client, you describe your REST API endpoints as method signatures and Refit writes all the networking code for you.

C#.NETASP.NET CoreNuGetSystem.Text.Jsonsetup: easycomplexity 2/5

Refit is a C# library that lets developers define a web API as a simple interface with annotations, and then generates all the code needed to actually make HTTP requests. Instead of manually writing boilerplate for HTTP calls, headers, and response parsing, you describe the API endpoints as method signatures in an interface, and Refit handles the rest at compile time using source code generation. The result is that calling a remote API looks like calling a regular local method.

For example, you write a C# interface with an attribute like [Get("/users/{user}")] on a method, pass that interface to Refit, and you get back an object you can call directly. Refit takes care of building the URL, sending the request, handling the response body, and deserializing JSON. It supports GET, POST, PUT, DELETE, PATCH, and HEAD requests, along with query strings, request bodies, file uploads, and custom headers.

The library works across the .NET ecosystem: .NET 8, 9, and 10, .NET Framework 4.6.2 and above, Blazor web apps, WinUI desktop apps, and Uno Platform. It integrates with the standard .NET dependency injection pattern via HttpClientFactory, making it straightforward to register API clients in ASP.NET Core applications. JSON serialization defaults to System.Text.Json in current versions, with an optional add-on package available for projects that need Newtonsoft.Json compatibility.

Refit is available as a NuGet package. The README covers many configuration scenarios: dynamic and static headers, bearer authentication, multipart file uploads, error handling, interface inheritance, and integration with Polly for retry and circuit-breaker policies. It also has a separate package for XML content if that is needed.

The project is maintained under the ReactiveUI organization on GitHub and is sponsored by several companies. It has been in active development for many years and is widely used in .NET projects that consume REST APIs. The full README is longer than what was shown.

Where it fits