About this project
Refit is an automatic type-safe REST library for modern .NET, heavily inspired by Square's Retrofit. It turns REST API definitions into live interfaces, allowing developers to define API contracts as C# interfaces with HTTP method attributes and have Refit generate the implementation that uses HttpClient to make calls.
The library supports multiple platforms including WinUI, .NET Framework 4.6.2+, .NET 8/9/10/11, Blazor, and Uno Platform. It ships with Roslyn source generators that produce clients at build time, reducing runtime reflection and enabling trimmed and Native AOT applications.
Key features include:
- Source generation with automatic client registration on .NET 5+
- Support for Get, Post, Put, Delete, Patch, and Head HTTP methods
- Dynamic querystring parameters, including object flattening and collection formatting
- Body content support for JSON, JSON Lines, XML, and form posts
- Static and dynamic request headers, including Bearer authentication
- Multipart uploads with various part types
- Per-request timeouts and cancellation token support
- Interface inheritance and composition of multiple APIs
- Default interface methods
- HttpClientFactory integration for dependency injection
- Custom HttpClient support
- Comprehensive exception handling with ApiException and custom exception factories
- Testing support via Refit.Testing package with declarative route tables
The source generator covers most request shapes including path parameters, query parameters with aliases and formatting, body content, headers, multipart uploads, properties, and various response types including IAsyncEnumerable for streamed responses. For shapes the generator cannot emit, methods fall back to the runtime reflection request builder.
Refit also ships analyzers that flag common interface issues at compile time, such as invalid route templates, multiple CancellationToken parameters, and incorrect HeaderCollection types, with code fixes for mechanical corrections.
The library provides multiple NuGet packages: Refit (core), Refit.HttpClientFactory (DI integration), Refit.Newtonsoft.Json (JSON.NET support), Refit.Testing (test utilities), and Refit.Reflection (runtime reflection fallback for older build tools).
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.