Refit maps C# interfaces to HTTP calls using attributes for routes, queries, headers, and bodies. A compile-time source generator builds the client while you control HttpClient and serialization.
Overview
Refit lets .NET applications describe REST APIs as C# interfaces. Method attributes declare HTTP verbs, URL templates, query parameters, headers, and request bodies; the generated implementation is created at build time and invokes HttpClient with your chosen serializers.
Typical usage registers a JSON context for System.Text.Json source generation, reuses a configured HttpClient with a base address, and obtains a typed client through RestService.ForGenerated. You keep cancellation tokens, error handling, and shared settings such as authorization in your application code.
The library covers streaming responses, pagination helpers, multipart and form posts, pluggable Newtonsoft.Json or XML serializers, dependency injection registration, resilience and retry handlers, OpenTelemetry hooks, and test doubles for local HTTP replies.
Documentation targets recent .NET and C# releases, notes AOT constraints, and lists packages for HTTP-aware retries and deterministic testing. Refit is positioned as an alternative to hand-written HttpClient call sites with a contract-first interface.
Key features
- Interface-driven HTTP clients
- Compile-time source generation
- System.Text.Json metadata support
- HttpClientFactory integration
- Streaming and pagination APIs
- Multipart and form bodies
- Local HTTP testing helpers
- OpenTelemetry instrumentation
Use cases
- Typed third-party API wrappers
- Microservice client contracts
- DI-registered HTTP clients
- Native AOT friendly JSON clients
- Testable API boundary layers