90 words
1 minute
Configure Refit
  1. Install Refit
    Terminal window
    dotnet add package Refit
    dotnet add package Refit.HttpClientFactory
  2. Create interface
    IGithubApi.cs
    public interface IGithubApi
    {
    [Get("/users/{userName}")]
    Task<User?> GetUserAsync(string username, CancellationToken cancellationToken = default);
    }
    9 collapsed lines
    public class User
    {
    [JsonPropertyName("id")]
    public long Id { get; set; }
    [JsonPropertyName("login")]
    public string Login { get; set; } = string.Empty;
    [JsonPropertyName("avatar_url")]
    public string AvatarUrl { get; set; } = string.Empty;
    }
  3. Configure refit client
    Program.cs
    builder.Services
    .AddRefitClient<IGithubApi>()
    .ConfigureHttpClient(c =>
    {
    c.BaseAddress = new Uri("https://api.github.com");
    c.DefaultRequestHeaders.UserAgent.TryParseAdd("request");
    });
  4. Map endpoint
    Program.cs
    app.MapGet("github/{userName}", async (
    IGithubApi githubApi,
    [FromRoute] string userName,
    CancellationToken cancellationToken) =>
    {
    var response = await githubApi.GetUserAsync(userName, cancellationToken);
    return Results.Ok(response);
    });

References#

Configure Refit
https://slimaeus.github.io/posts/refit-configuration/
Author
Slimaeus
Published at
2025-06-23
License
CC BY-NC-SA 4.0