Chapter 2. Retrieving League Details using C#
In this exercise we will retrieve league details using the league’s endpoint and your API Token. You can easily place this url https://api.sportmonks.com/v3/football/leagues?api_token=API_TOKEN on your browser, before running the code on Visual Studio Code.
using System;
using System.Net.Http;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Define the base URL of the API
string url = "https://api.sportmonks.com/v3/football/leagues?api_token=API_Token"; //replace with your API Token.
// Create an instance of HttpClient
using (HttpClient client = new HttpClient())
{
try
{
// Send a GET request to the API
HttpResponseMessage response = await client.GetAsync(url);
// Ensure the request was successful
response.EnsureSuccessStatusCode();
// Read the response content as a string
string responseBody = await response.Content.ReadAsStringAsync();
// Output the response
Console.WriteLine(responseBody);
}
catch (HttpRequestException e)
{
// Output any error messages
Console.WriteLine($"Request error: {e.Message}");
}
}
}
}
{"data":[{"id":2,"sport_id":1,"country_id":41,"name":"Champions League","active":true,"short_code":"UEFA CL","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/2.png","type":"league","sub_type":"cup_international","last_played_at":"2024-10-23 19:00:00","category":1,"has_jerseys":false},{"id":5,"sport_id":1,"country_id":41,"name":"Europa League","active":true,"short_code":"UEFA EL","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/5\/5.png","type":"league","sub_type":"cup_international","last_played_at":"2024-10-24 19:00:00","category":1,"has_jerseys":false},{"id":8,"sport_id":1,"country_id":462,"name":"Premier League","active":true,"short_code":"UK PL","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/8\/8.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-27 16:30:00","category":1,"has_jerseys":false},{"id":9,"sport_id":1,"country_id":462,"name":"Championship","active":true,"short_code":"UK Champ","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/9\/9.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-27 15:00:00","category":2,"has_jerseys":false},{"id":12,"sport_id":1,"country_id":462,"name":"League One","active":true,"short_code":"UK L1","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/12\/12.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-28 20:00:00","category":2,"has_jerseys":false},{"id":14,"sport_id":1,"country_id":462,"name":"League Two","active":true,"short_code":"UK L2","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/14\/14.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 14:00:00","category":2,"has_jerseys":false},{"id":17,"sport_id":1,"country_id":462,"name":"National League","active":true,"short_code":"UK NL","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/17\/17.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 16:30:00","category":3,"has_jerseys":false},{"id":20,"sport_id":1,"country_id":462,"name":"Vanarama National League North","active":true,"short_code":"UK NLN","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/20\/20.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 14:00:00","category":4,"has_jerseys":false},{"id":23,"sport_id":1,"country_id":462,"name":"Community Shield","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/23\/23.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-08-10 14:00:00","category":2,"has_jerseys":false},{"id":24,"sport_id":1,"country_id":462,"name":"FA Cup","active":true,"short_code":"UK FA Cup","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/24\/24.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-15 18:45:00","category":2,"has_jerseys":false},{"id":27,"sport_id":1,"country_id":462,"name":"Carabao Cup","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/27\/27.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-01 18:45:00","category":2,"has_jerseys":false},{"id":30,"sport_id":1,"country_id":462,"name":"Fa Trophy","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/30\/30.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-26 14:00:00","category":2,"has_jerseys":false},{"id":32,"sport_id":1,"country_id":462,"name":"Premier League Cup","active":true,"short_code":"ENG DL2","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/0\/32.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-25 18:00:00","category":3,"has_jerseys":false},{"id":35,"sport_id":1,"country_id":462,"name":"Premier League U21","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/3\/35.png","type":"league","sub_type":"domestic","last_played_at":"2016-05-12 10:30:00","category":3,"has_jerseys":false},{"id":38,"sport_id":1,"country_id":462,"name":"Premier League International Cup","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/6\/38.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-28 19:00:00","category":3,"has_jerseys":false},{"id":39,"sport_id":1,"country_id":462,"name":"EFL Trophy","active":true,"short_code":"UK EFLT","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/7\/39.png","type":"league","sub_type":"domestic_cup","last_played_at":"2024-10-08 18:45:00","category":2,"has_jerseys":false},{"id":42,"sport_id":1,"country_id":462,"name":"Premier League U18","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/10\/42.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 12:00:00","category":3,"has_jerseys":false},{"id":44,"sport_id":1,"country_id":462,"name":"Wsl 2 Women","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/12\/44.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-20 13:00:00","category":3,"has_jerseys":false},{"id":45,"sport_id":1,"country_id":462,"name":"Women's Super League","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/13\/45.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-20 17:45:00","category":3,"has_jerseys":false},{"id":48,"sport_id":1,"country_id":462,"name":"Professional Development League","active":true,"short_code":"UK U23 DL","image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/16\/48.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-28 19:00:00","category":4,"has_jerseys":false},{"id":51,"sport_id":1,"country_id":462,"name":"Non League Premier: Isthmian","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/19\/51.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 14:00:00","category":4,"has_jerseys":false},{"id":53,"sport_id":1,"country_id":462,"name":"Non League Div One: Isthmian North","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/21\/53.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-23 18:45:00","category":4,"has_jerseys":false},{"id":54,"sport_id":1,"country_id":462,"name":"Non League Div One: Southern Central","active":true,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/22\/54.png","type":"league","sub_type":"domestic","last_played_at":"2024-10-26 14:00:00","category":4,"has_jerseys":false},{"id":57,"sport_id":1,"country_id":462,"name":"Non League Div One: Northern North","active":false,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/25\/57.png","type":"league","sub_type":"domestic","last_played_at":"2020-12-26 21:00:12","category":4,"has_jerseys":false},{"id":60,"sport_id":1,"country_id":462,"name":"Non League Div One: Isthmian South","active":false,"short_code":null,"image_path":"https:\/\/cdn.sportmonks.com\/images\/soccer\/leagues\/28\/60.png","type":"league","sub_type":"domestic","last_played_at":"2020-11-04 09:00:27","category":4,"has_jerseys":false}],"pagination":{"count":25,"per_page":25,"current_page":1,"next_page":"https:\/\/api.sportmonks.com\/v3\/football\/leagues?page=2","has_more":true},"subscription":[{"meta":{"trial_ends_at":"2024-10-16 14:50:45","ends_at":null,"current_timestamp":1730197289},"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":"League"},"timezone":"UTC"}
Explanation.
An httpClient.GetAsync(apiUrl) sends a GET request to the specified URL of the API. The function returns the expected http.Response in JSON, which is displayed on the terminal of your IDE, in this case Visual Studio Code.
The response, which may be difficult to comprehend, contains the server’s response to the request which was made via the url.