Refit

September 25, 2026 ยท View on GitHub

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

PackagePurpose
RefitGenerated HTTP clients, request attributes, serializers and response types. Includes the generator, interface analyzers and code fixes.
Refit.HttpClientFactoryRegister clients with dependency injection and IHttpClientFactory, including generated-only and keyed registrations.
Refit.TestingMatch outgoing requests, supply local replies, verify calls and simulate network faults. Release streamed replies chunk by chunk, choose how request bodies are captured, and control simulated time.
Refit.Newtonsoft.JsonSerialize content with Newtonsoft.Json.
Refit.XmlSerialize XML content.
Refit.ReflectionOptional 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

SurfaceDocumentation
HTTP methods, route placeholders and request inspectionRoutes
Scalar, object and collection queries; formatting and convertersQueries
Static and dynamic headers, authorization and request stateHeaders and request context
Serialized bodies, forms, buffering and request compressionBodies
Task<T>, ValueTask<T>, IObservable<T> and response wrappersReturn types and responses
Streamed JSON arrays, JSON Lines and server-sent eventsStreaming
Lazy paging over cursors, offsets and Link headers with PagedEnumerable<TPage, TItem>Pagination
JSON contexts registered at client creation and per-method JsonTypeInfo<T> parametersJSON
HTTP, transport and deserialization failuresErrors
Local replies, request verification and simulated faultsTesting
Deterministic tests of streamed replies, stalled or dropped bodies, streaming uploads and simulated timeTesting streams, uploads and time

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 groupBrief descriptionDocumentation
HTTP verb and route attributesDeclare the HTTP method, route template, base-path prefix and URL resolution behavior.Routes
Query attributes and formattersAdd scalar, object, collection, flag, encoded, formatted and converted query values.Queries, formatters and converters
Headers, authorization and request contextAdd shared or per-call headers, obtain tokens and attach local handler state.Headers and request context
Bodies, forms, multipart and compressionSerialize request bodies, send forms, files or streams, choose buffering and apply request compression.Bodies and multipart
Client creation and request buildersCreate generated or reflection clients, create owned HttpClient instances and supply request builders.Client creation and request builders
Settings and request metadataConfigure serializers, formatters, exception factories, transport behavior, request options, timeouts, versions and URL resolution.Settings, metadata and helpers
Return types, responses and adaptersChoose Task, ValueTask, IObservable<T>, IAsyncEnumerable<T>, response wrappers and custom return adapters.Return types, responses, streaming and adapters
PaginationReturn PagedEnumerable<TPage, TItem> with [Paged] and [PageToken], or build one with Create, FromCursor, FromOffset and FromLinks; limit, prefetch or observe pages.Pagination
Errors and problem detailsRead typed error content, create or transform API and transport exceptions, enforce success and redact sensitive details.Errors
Content serializersUse System.Text.Json, JSON Lines, synchronous or streaming serializer capabilities and custom serializers.JSON and content
JSON context registrationPass a JsonSerializerContext to RestService.ForGenerated<T>, AddRefitGeneratedClient<T> or RefitSettings.ForJsonContext, and declare JsonTypeInfo<T> method parameters. Reflection-based JSON stays off unless you allow it.Register a context and pass metadata to a method
Alternate serializersConfigure Newtonsoft.Json and XML serializers with their settings.Newtonsoft.Json and XML
HTTP client factory and dependency injectionRegister generated, reflection or keyed clients, settings holders and authorization providers.Dependency injection
Testing routes, replies, faults and verificationMatch requests, create replies, inject failures or delays, inspect bodies and verify calls.Testing
Testing streams, uploads and timeRelease streamed items with StreamSource and Reply.Stream, send fixed Reply.JsonLines or Reply.ServerSentEvents bodies, drop or stall a body, choose StubHttp.RequestCapture (Full, None or Bounded) and set StubHttp.TimeProvider to a fake clock.Testing streams, uploads and time
Advanced generated request helpersExtend 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 gives Refit's maintainers licences for its tools through its open source support programme. Anthropic supports them with Claude through Claude for Open Source. OpenAI supports them with Codex through Codex for Open Source.

JetBrains Claude by Anthropic OpenAI

See our sponsors for more information. JetBrains, Claude, Anthropic, OpenAI and Codex names and logos are trademarks of their respective owners.

Refit is licensed under the MIT license.