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 inter
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 inter
| Refit | Refit.HttpClientFactory | Refit.Newtonsoft.Json | Refit.Testing | |
|---|---|---|---|---|
| NuGet |
Refit is a library heavily inspired by Square's Retrofit library, and it turns your REST API into a live interface:
public interface IGitHubApi
{
[Get("/users/{user}")]
Task<User> GetUser(string user);
}The RestService class generates an implementation of IGitHubApi that uses
HttpClient to make its calls:
var gitHubApi = RestService.For<IGitHubApi>("https://api.github.com");
var octocat = await gitHubApi.GetUser("octocat");.NET supports registering Refit clients via HttpClientFactory:
services
.AddRefitClient<IGitHubApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.github.com"));To test the clients you build with Refit, the Refit.Testing package
lets you stub responses and verify requests with a declarative route table — see
Testing your Refit clients.
Refit is sponsored by the following:
Refit currently supports the following platforms and modern .NET targets:
The source generator ships inside the Refit package as a Roslyn analyzer, so it is your build tools,
not your target framework, that decide whether you get generated clients:
| Requirement | Minimum |
|---|---|
| Roslyn (C# compiler) | 4.8 |
| Visual Studio | 2022 17.8 |
| .NET SDK | 8.0.100 |
| NuGet references | PackageReference |
These are build-time requirements only. The compiled output still runs on every platform listed above, down
to .NET Framework 4.6.2, and both SDK-style and legacy (non-SDK) .csproj projects are supported.
Older build tools do not silently produce an empty client — the build fails with REFIT001 naming the Roslyn
version it found. packages.config cannot load analyzers at all, so a source generator will never run there;
migrate to PackageReference
to use it.
If you cannot move to these build tools, set DisableRefitSourceGenerator to true and add the
Refit.Reflection package, which builds requests at runtime
and needs no source generator.
Breaking changes and the notable additions for each major version — including the V14 move of the reflection
request builder into the opt-in Refit.Reflection package —
are documented in Breaking changes and release notes.
The Refit package ships Roslyn source generators. A PackageReference to Refit gets you generated clients at build
time — no extra package.
The generated code targets C# 7.3. On C# 8 or newer, the generator also emits nullable directives and annotations.
You create generated clients with the normal APIs:
var api = RestService.For<IGitHubApi>("https://api.github.com");or through Refit.HttpClientFactory:
services
.AddRefitClient<IGitHubApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://api.github.com"));Generated clients use the same RefitSettings you pass to RestService.For<T> or AddRefitClient<T>, and honor settings
such as:
ContentSerializerUrlParameterFormatterUrlParameterKeyFormatterCollectionFormatAuthorizationHeaderValueGetterExceptionFactoryDeserializationExceptionFactoryTransportExceptionFactoryHttpRequestMessageOptionsVersion and VersionPolicyOn .NET 5 and newer the generator emits a module initializer
that registers every generated client factory at assembly load. RestService.ForGenerated<T> and
AddRefitGeneratedClient<T> then resolve the client with no runtime reflection. RestService.For<T> skips the reflection
request builder for fully generated interfaces. That keeps trimmed and Native AOT apps reflection-free.
.NET Framework (net462–net481) gets no automatic registration. ModuleInitializerAttribute arrived in .NET 5 and
isn't in the .NET Framework BCL, so Refit emits the initializer only for .NET 5+ targets. Raising a project's
<LangVersion> to 9 doesn't change that — the missing type is the blocker, not the language version. (It does switch on
modern generated syntax like nullable annotations.)
The generated inline code still runs. But RestService.For<T> finds the client by runtime type lookup and builds the
reflection request builder, so you must reference the opt-in Refit.Reflection
package. .NET Framework has no trimming or AOT, so this costs nothing there.
ForGenerated<T> and AddRefitGeneratedClient<T> need that registration. On .NET Framework they throw unless you register
the factory yourself with RegisterGeneratedFactory<T> / RegisterGeneratedSettingsFactory<T> at startup.
For Native AoT or trimmed apps, RestService.ForGenerated<T> creates a client only when the generator registered an
implementation for that interface:
using var client = new HttpClient
{
BaseAddress = new Uri("https://api.github.com")
};
var api = RestService.ForGenerated<IGitHubApi>(client);ForGenerated<T> never falls back to the reflection client. When the generated client builds every request directly,
Refit skips the reflection request builder too. If no generated implementation is registered, it throws an
InvalidOperationException pointing back to source generation setup.
By default the generator builds requests directly. Instead of a method body that calls the reflective pipeline through
BuildRestResultFuncForMethod, the generated client creates the HttpRequestMessage, applies headers, properties, and
body, then dispatches it through Refit's runtime helpers.
This cuts runtime reflection, metadata lookup, argument boxing, and delegate construction. It covers most request shapes:
[AliasAs], [Query] (including Format and CollectionFormat), scalar
collections, [QueryName] flags and [Encoded] values[Body] detection on POST/PUT/PATCH[Headers][Header] parameters[HeaderCollection] dictionaries[Body] content[Multipart] form uploads whose parts are StreamPart/ByteArrayPart/FileInfoPart (or a MultipartItem subclass),
Stream, string, FileInfo, byte[], HttpContent, a date/time or Guid value, or an enumerable of these (an
object part serialized through the content serializer still falls back to the runtime builder)[Property] parameters[Property] interface propertiesTask, Task<T>, `Task<ApiResponse<