Chapter 2: Making Your First API Call: Retrieving Fixtures.
For the first exercise we shall retrieve fixtures from a particular date range and convert the JSON data into a Microsoft Excel file. Head over to the ID finder on your dashboard to select the ID of the Scottish Premier League (501), which comes as a free plan on your account.
We shall select Scottish Premiership fixtures played between the 20th and 30th of December 2024. However, you are at liberty to widen the date range should you wish to do so.
Here below is our base URL, which you may place in your web browser. Don’t forget to replace the api_token with the API Token that you created earlier in 1.3.
https://api.sportmonks.com/v3/football/fixtures/between/2024-11-20/2024-11-30?api_token=API_Token&filters=fixtureLeagues:501
Fire up Visual Studio Code and create a new console application which comes with the legendary “Hello World” example. You will replace the example with the piece of code here. Visit the official tutorial page for help on how to create a Console application on VSC.
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program {
static async Task Main()
{
// URL of the API
string url = "https://api.sportmonks.com/v3/football/fixtures/between/2024-11-20/2024-11-30?api_token=API_Token&filters=fixtureLeagues:501";
try
{
// Fetch JSON data
string jsonResult = await GetJsonFromUrl(url);
// Output the result
Console.WriteLine("JSON Result:");
Console.WriteLine(jsonResult);
}
catch (Exception ex)
{
Console.WriteLine($"Error fetching data: {ex.Message}");
}
}
static async Task <string> GetJsonFromUrl(string url)
{
using (HttpClient client = new HttpClient())
{
// Send GET request
HttpResponseMessage response = await client.GetAsync(url);
// Ensure the request succeeded
response.EnsureSuccessStatusCode();
// Read and return the response content as a string
return await response.Content.ReadAsStringAsync();
}
}
}
JSON Result:
{"data":[{"id":19146772,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":8908,"name":"Ross County vs Motherwell","starting_at":"2024-11-23 15:15:00","result_info":"Ross County won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732374900},{"id":19146773,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":219,"name":"St. Johnstone vs Kilmarnock","starting_at":"2024-11-23 15:15:00","result_info":"St. Johnstone won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732374900},{"id":19146771,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":8914,"name":"Rangers vs Dundee United","starting_at":"2024-11-23 15:45:00","result_info":"Game ended in draw.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732376700},{"id":19146774,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":8879,"name":"St. Mirren vs Aberdeen","starting_at":"2024-11-23 16:00:00","result_info":"St. Mirren won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732377600},{"id":19146769,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":284597,"name":"Dundee vs Hibernian","starting_at":"2024-11-23 17:55:00","result_info":"Dundee won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732384500},{"id":19146770,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340585,"state_id":5,"venue_id":336296,"name":"Hearts vs Celtic","starting_at":"2024-11-23 19:45:00","result_info":"Celtic won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732391100},{"id":19146759,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340583,"state_id":5,"venue_id":8946,"name":"Hibernian vs Aberdeen","starting_at":"2024-11-26 19:45:00","result_info":"Game ended in draw.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732650300},{"id":19146775,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340586,"state_id":5,"venue_id":8909,"name":"Celtic vs Ross County","starting_at":"2024-11-30 15:00:00","result_info":"Celtic won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732978800},{"id":19146776,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340586,"state_id":5,"venue_id":8947,"name":"Dundee United vs St. Mirren","starting_at":"2024-11-30 15:00:00","result_info":"Dundee United won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732978800},{"id":19146778,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340586,"state_id":5,"venue_id":8906,"name":"Kilmarnock vs Dundee","starting_at":"2024-11-30 15:00:00","result_info":"Game ended in draw.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732978800},{"id":19146779,"sport_id":1,"league_id":501,"season_id":23690,"stage_id":77471570,"group_id":null,"aggregate_id":null,"round_id":340586,"state_id":5,"venue_id":8922,"name":"Motherwell vs Hibernian","starting_at":"2024-11-30 15:00:00","result_info":"Hibernian won after full-time.","leg":"1\/1","details":null,"length":90,"placeholder":false,"has_odds":true,"has_premium_odds":true,"starting_at_timestamp":1732978800}],"pagination":{"count":11,"per_page":25,"current_page":1,"next_page":null,"has_more":false},"subscription":[{"meta":{"trial_ends_at":"2024-10-16 14:50:45","ends_at":null,"current_timestamp":1735439529},"plans":[{"plan":"Enterprise plan (loyal)","sport":"Football","category":"Advanced"},{"plan":"Enterprise Plan","sport":"Cricket","category":"Standard"},{"plan":"Formula One","sport":"Formula One","category":"Standard"}],"add_ons":[{"add_on":"All-in News API","sport":"Football","category":"News"},{"add_on":"pressure index add-on","sport":"Football","category":"Default"},{"add_on":"Enterprise Plan Predictions","sport":"Football","category":"Predictions"},{"add_on":"xG Advanced","sport":"Football","category":"Expected"}],"widgets":[{"widget":"Sportmonks Widgets","sport":"Football"}]}],"rate_limit":{"resets_in_seconds":3600,"remaining":2999,"requested_entity":"Fixture"},"timezone":"UTC"}
Explanation
As you can see, we defined the complete URL along with the required parameters and API token. We then used HttpClient to send the GET request asynchronously.
We checked if the response is successful using EnsureSuccessStatusCode() before we went on to read the response body as a string containing JSON data.
A try-catch block was placed to handle network or API errors. We then displayed the fetched JSON data in the console or an error message if something went wrong.
2.2 Making our JSON response readable.
As you can see, it is difficult to make out the response. However, in this section we shall make our response readable by using System.Text.Json namespace.
using System;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
class Program
{
static async Task Main()
{
// URL of the API
string url = "https://api.sportmonks.com/v3/football/fixtures/between/2024-11-20/2024-11-30?api_token=API_Token&filters=fixtureLeagues:501";
try
{
// Fetch JSON data
string jsonResult = await GetJsonFromUrl(url);
// Pretty print the JSON
string prettyJson = FormatJson(jsonResult);
// Output the result
Console.WriteLine("Pretty Printed JSON Result:");
Console.WriteLine(prettyJson);
}
catch (Exception ex)
{
Console.WriteLine($"Error fetching data: {ex.Message}");
}
}
static async Task<string> GetJsonFromUrl(string url)
{
using (HttpClient client = new HttpClient())
{
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
return await response.Content.ReadAsStringAsync();
}
}
static string FormatJson(string json)
{
using (JsonDocument doc = JsonDocument.Parse(json))
{
return JsonSerializer.Serialize(doc.RootElement, new JsonSerializerOptions { WriteIndented = true });
}
}
}
Pretty Printed JSON Result:
{
"data": [
{
"id": 19146772,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 8908,
"name": "Ross County vs Motherwell",
"starting_at": "2024-11-23 15:15:00",
"result_info": "Ross County won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732374900
},
{
"id": 19146773,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 219,
"name": "St. Johnstone vs Kilmarnock",
"starting_at": "2024-11-23 15:15:00",
"result_info": "St. Johnstone won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732374900
},
{
"id": 19146771,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 8914,
"name": "Rangers vs Dundee United",
"starting_at": "2024-11-23 15:45:00",
"result_info": "Game ended in draw.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732376700
},
{
"id": 19146774,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 8879,
"name": "St. Mirren vs Aberdeen",
"starting_at": "2024-11-23 16:00:00",
"result_info": "St. Mirren won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732377600
},
{
"id": 19146769,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 284597,
"name": "Dundee vs Hibernian",
"starting_at": "2024-11-23 17:55:00",
"result_info": "Dundee won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732384500
},
{
"id": 19146770,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340585,
"state_id": 5,
"venue_id": 336296,
"name": "Hearts vs Celtic",
"starting_at": "2024-11-23 19:45:00",
"result_info": "Celtic won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732391100
},
{
"id": 19146759,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340583,
"state_id": 5,
"venue_id": 8946,
"name": "Hibernian vs Aberdeen",
"starting_at": "2024-11-26 19:45:00",
"result_info": "Game ended in draw.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732650300
},
{
"id": 19146775,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340586,
"state_id": 5,
"venue_id": 8909,
"name": "Celtic vs Ross County",
"starting_at": "2024-11-30 15:00:00",
"result_info": "Celtic won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732978800
},
{
"id": 19146776,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340586,
"state_id": 5,
"venue_id": 8947,
"name": "Dundee United vs St. Mirren",
"starting_at": "2024-11-30 15:00:00",
"result_info": "Dundee United won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732978800
},
{
"id": 19146778,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340586,
"state_id": 5,
"venue_id": 8906,
"name": "Kilmarnock vs Dundee",
"starting_at": "2024-11-30 15:00:00",
"result_info": "Game ended in draw.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732978800
},
{
"id": 19146779,
"sport_id": 1,
"league_id": 501,
"season_id": 23690,
"stage_id": 77471570,
"group_id": null,
"aggregate_id": null,
"round_id": 340586,
"state_id": 5,
"venue_id": 8922,
"name": "Motherwell vs Hibernian",
"starting_at": "2024-11-30 15:00:00",
"result_info": "Hibernian won after full-time.",
"leg": "1/1",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"has_premium_odds": true,
"starting_at_timestamp": 1732978800
}
],
"pagination": {
"count": 11,
"per_page": 25,
"current_page": 1,
"next_page": null,
"has_more": false
},
"subscription": [
{
"meta": {
"trial_ends_at": "2024-10-16 14:50:45",
"ends_at": null,
"current_timestamp": 1735442005
},
"plans": [
{
"plan": "Enterprise plan (loyal)",
"sport": "Football",
"category": "Advanced"
},
{
"plan": "Enterprise Plan",
"sport": "Cricket",
"category": "Standard"
},
{
"plan": "Formula One",
"sport": "Formula One",
"category": "Standard"
}
],
"add_ons": [
{
"add_on": "All-in News API",
"sport": "Football",
"category": "News"
},
{
"add_on": "pressure index add-on",
"sport": "Football",
"category": "Default"
},
{
"add_on": "Enterprise Plan Predictions",
"sport": "Football",
"category": "Predictions"
},
{
"add_on": "xG Advanced",
"sport": "Football",
"category": "Expected"
}
],
"widgets": [
{
"widget": "Sportmonks Widgets",
"sport": "Football"
}
]
}
],
"rate_limit": {
"resets_in_seconds": 1124,
"remaining": 2998,
"requested_entity": "Fixture"
},
"timezone": "UTC"
}
This method FormatJson formats the raw JSON string into an indented, human-readable format without the use of any external library, as it utilises the built-in functionality from the System.Text.Json namespace.