DEV Community

orbistats
orbistats

Posted on

Consuming a Sports Data API in .NET Core (C# Example)

Most sports-data API tutorials out there are written for JavaScript, so here's the .NET Core version — useful if you're building a backend service, a Blazor app, or just prefer typed responses over parsing raw JSON by hand.

This walks through pulling live football scores using HttpClient and System.Text.Json.

Setup

Create a free API key first — no card required for the free tier: sign up here. If you want to see raw responses before writing any code, there's a public sandbox too: orbistats.com/developers/sandbox.html

Add your key to appsettings.json or user secrets rather than hardcoding it:

{
  "OrbistatsApiKey": "YOUR_API_KEY"
}
Enter fullscreen mode Exit fullscreen mode

Step 1: Create a typed client

public class OrbistatsClient
{
    private readonly HttpClient _httpClient;

    public OrbistatsClient(HttpClient httpClient, string apiKey)
    {
        _httpClient = httpClient;
        _httpClient.BaseAddress = new Uri("https://api.orbistats.com/v1/");
        _httpClient.DefaultRequestHeaders.Authorization =
            new AuthenticationHeaderValue("Bearer", apiKey);
    }

    public async Task<List<LiveMatch>> GetLiveFootballScoresAsync()
    {
        var response = await _httpClient.GetAsync("football/live");
        response.EnsureSuccessStatusCode();

        var stream = await response.Content.ReadAsStreamAsync();
        var matches = await JsonSerializer.DeserializeAsync<List<LiveMatch>>(stream);

        return matches ?? new List<LiveMatch>();
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Define the response model

public class LiveMatch
{
    [JsonPropertyName("match_id")]
    public string MatchId { get; set; } = string.Empty;

    public string Status { get; set; } = string.Empty;
    public int Minute { get; set; }

    public TeamScore Home { get; set; } = new();
    public TeamScore Away { get; set; } = new();
}

public class TeamScore
{
    public string Name { get; set; } = string.Empty;
    public int Score { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Register and use it

If you're using dependency injection (most ASP.NET Core apps are), register it in Program.cs:

builder.Services.AddHttpClient<OrbistatsClient>();
builder.Services.AddSingleton(sp =>
    new OrbistatsClient(
        sp.GetRequiredService<HttpClient>(),
        builder.Configuration["OrbistatsApiKey"]!
    )
);
Enter fullscreen mode Exit fullscreen mode

Then wherever you need it:

var matches = await orbistatsClient.GetLiveFootballScoresAsync();

foreach (var match in matches)
{
    Console.WriteLine($"{match.Home.Name} {match.Home.Score} - {match.Away.Score} {match.Away.Name} ({match.Minute}')");
}
Enter fullscreen mode Exit fullscreen mode

A note on polling vs WebSockets

For most side projects, polling this every 15-30 seconds on a background service (IHostedService or a simple timer) is enough and keeps you comfortably within free tier limits. If you need true real-time push updates instead — say, for a live trading dashboard — it's worth checking the WebSocket API instead of polling REST repeatedly: documentation here

Wrapping up

That covers the basic flow — auth, typed client, DI registration, and consuming the response. The same pattern works for the other endpoints (fixtures, results, odds, historical data), you'd just swap the model class and the endpoint path. Full list is in the API reference.

Would be curious if anyone here has a cleaner pattern for typed API clients in .NET — always looking to simplify this kind of boilerplate.

Top comments (0)