I was recently asked to build an API that contacts two other APIs. My senior engineer casually dropped:
“Use Refit.”
At first, I was like — “what is this magic word?”
Then I used it. And I was pleasantly shocked.
🧠 What is Refit?
Think of Refit as your tiny API Copilot.
Refit is a REST library for .NET that turns your OpenAPI/Swagger spec into strongly-typed, auto-generated client code. Instead of manually writing a bunch of HttpClient logic, you describe your API once, and Refit generates the rest.
💥 Why It Blew My Mind
Here’s my flow:
I downloaded the Swagger JSON of the external Book API I needed to consume.
I created a refitter.yaml file with the configs.
Ran one CLI command.
And boom — Refit auto-generated:
- The interface with methods like GetBookByIdAsync
- The models like Book, Author, etc.
- The entire client logic, ready to use
Now I can just call:
var book = await _bookApi.GetBookByIdAsync(42);
No plumbing. No extra mapping. Clean.
🛠️ Installing & Using Refit (Quickstart)
Install Refit:
Install-Package Refit
Register the client in Program.cs:
services.AddRefitClient<IBookApi>()
.ConfigureHttpClient(c => c.BaseAddress = new Uri("https://books.example.com"));
Use it like this:
public class LibraryService(IBook bookApi)
{
public async Task<Book> FetchBook(int id)
{
return await bookApi.GetBookByIdAsync(id);
}
}
🤔 Swagger: Your New Best Friend
Refit only works as well as your Swagger spec. A clean spec = clean client.
That realization made me treat Swagger not just as documentation, but as code — the contract between services.
When I polished the Swagger for my own API, downstream teams could just plug it into Refit and boom, done. That’s what future-proof API dev looks like.
🧩 Final Thoughts
We’re building an ecosystem where APIs and Kafka keep passing messages like trained athletes in a relay race. My role? Build one of those runners. But with Refit, I didn’t just build a runner — I gave it shoes, training, and a coach.
Whether you're a beginner dev or a product owner — Refit makes integrations faster, safer, and cleaner. All it asks for?
A good Swagger spec.
Top comments (3)
Refit really feels like a superpower when the Swagger spec's solid - I’ve wasted so much time on messy hand-written clients in the past. Have you ever tried pairing Refit with NSwag or other spec generators?
I have seen people use it, thanks for reminding me, I am gonna try using it to see how they bring value! Thanks for the rec..
Refit is rock-solid and works great in practice.
I’ve put together a sample repo to demonstrate how it integrates with Blazor:
🔗 github.com/stevsharp/BlazorAppRefit
Thanks for sharing the discussion—hope this helps others looking to explore Refit in a clean Blazor setup!