Skip to content

Repository files navigation

Refit

Refit

Build NuGet

Refit turns a C# interface into an HTTP client. Attributes describe the request method, URL, headers and body. The source generator builds its implementation when your app compiles. Your app keeps control of HttpClient, cancellation and serialization.

Start with the Refit documentation. It explains the concepts, walks through a first request, and links each topic to runnable source examples. Examples use .NET 10 and C# 14 unless they state another target. Website code blocks can show a subset of a complete example and omit its test setup.

Packages

Package Purpose
Refit Generated HTTP clients, request attributes, serializers and response types. Includes the generator, interface analyzers and code fixes.
Refit.HttpClientFactory Register clients with dependency injection and IHttpClientFactory, including generated-only and keyed registrations.
Refit.Testing Match outgoing requests, supply local replies, verify calls and simulate network faults.
Refit.Newtonsoft.Json Serialize content with Newtonsoft.Json.
Refit.Xml Serialize XML content.
Refit.Reflection Optional request building through runtime reflection. Use generated clients for trimmed and Native AOT apps.

System.Text.Json with generated metadata is the preferred JSON setup. See JSON configuration and AOT guidance.

What you can build

Surface Documentation
HTTP methods, route placeholders and request inspection Routes
Scalar, object and collection queries; formatting and converters Queries
Static and dynamic headers, authorization and request state Headers and request context
Serialized bodies, forms, buffering and request compression Bodies
Task<T>, ValueTask<T>, IObservable<T> and response wrappers Return types and responses
Streamed JSON arrays, JSON Lines and server-sent events Streaming
HTTP, transport and deserialization failures Errors
Local replies, request verification and simulated faults Testing

See why use Refit for equivalent manual HTTP code and trade-offs. The website records known behavior discrepancies beside the affected APIs.

Public API map

Use the full API reference to find types, overloads, parameters and return values across all topics on one page.

Public method group Brief description Documentation
HTTP verb and route attributes Declare the HTTP method, route template, base-path prefix and URL resolution behavior. Routes
Query attributes and formatters Add scalar, object, collection, flag, encoded, formatted and converted query values. Queries, formatters and converters
Headers, authorization and request context Add shared or per-call headers, obtain tokens and attach local handler state. Headers and request context
Bodies, forms, multipart and compression Serialize request bodies, send forms, files or streams, choose buffering and apply request compression. Bodies and multipart
Client creation and request builders Create generated or reflection clients, create owned HttpClient instances and supply request builders. Client creation and request builders
Settings and request metadata Configure serializers, formatters, exception factories, transport behavior, request options, timeouts, versions and URL resolution. Settings, metadata and helpers
Return types, responses and adapters Choose Task, ValueTask, IObservable<T>, IAsyncEnumerable<T>, response wrappers and custom return adapters. Return types, responses, streaming and adapters
Errors and problem details Read typed error content, create or transform API and transport exceptions, enforce success and redact sensitive details. Errors
Content serializers Use System.Text.Json, JSON Lines, synchronous or streaming serializer capabilities and custom serializers. JSON and content
Alternate serializers Configure Newtonsoft.Json and XML serializers with their settings. Newtonsoft.Json and XML
HTTP client factory and dependency injection Register generated, reflection or keyed clients, settings holders and authorization providers. Dependency injection
Testing routes, replies, faults and verification Match requests, create replies, inject failures or delays, inspect bodies and verify calls. Testing
Advanced generated request helpers Extend or inspect request construction through query builders, parameter providers and helper contracts. Query builder and request helpers

Build requirements and targets

The source generator requires Roslyn 4.8 or newer and PackageReference. .NET SDK 8.0.100 and Visual Studio 2022 17.8 provide that compiler baseline. These build requirements are separate from the application's target framework.

The source projects target .NET 8, 9, 10 and 11, plus .NET Framework 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 and 4.8.1. Availability of individual framework features can differ. Read the AOT guide before choosing reflection-dependent APIs for a trimmed or native app.

Examples and changes

Build every complete documentation example from src:

dotnet build Refit.slnx -c Release

Each website page links to its own project under examples/Documentation/Pages. Run a page project, such as examples/Documentation/Pages/requests-routes/requests-routes.csproj, to check its local reply assertions without contacting a live service. The documentation examples README lists the source layout and independent projects.

See breaking changes, GitHub releases, and contributing.

Sponsors

JetBrains Claude

Refit is licensed under the MIT license.

About

The automatic type-safe REST library for .NET. Refit turns a REST API into a C# interface and generates the HttpClient implementation, with support for HttpClientFactory, pluggable serializers and a testing package.

Topics

Resources

Code of conduct

Security policy

Stars

9.6k stars

Watchers

165 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages