HOW TO GET ALL TEAMS AND THEIR IDS

- Posted in Tutorials by

We'll look at the different ways of retrieving all teams and their ids from API-FOOTBALL.

  1. With the Dashboard
  2. With the API

The first is the easiest, as it requires no scripts.

Simply go to the dashboard and select ids then teams in the left-hand sidebar.

You will then be redirected to this page:

dashboard teams list

You can select the country of your choice to display all the teams attached to it. There's also a search bar to help you find the right team.

That’s all for the easy part.

The second methods use the API to retrieve the team list.

With the API, there are several ways to retrieve the team list and associated ids, depending on what you need.

If you want to retrieve the team list for a couple or competitions, you'll need to use the endpoint teams with the league id and season as parameters.

Say you want to retrieve the team list for the following competitions for the 2022/2023 season:

  • Germany - Bundesliga
  • England - Premier League
  • France - Ligue 1

It will therefore be necessary to make 3 API calls (one for each competition).

The calls will look like this:

  • /teams?league=78&season=2022 For Bundesliga
  • /teams?league=39&season=2022 For Premier League
  • /teams?league=61&season=2022 For Ligue 1

Note : Competition ids can also be retrieved via the dashboard or endpoint leagues.

The API response will be an array containing all the teams' information, like this one:

{
  "team": {
    "id": 33,
    "name": "Manchester United",
    "code": "MUN",
    "country": "England",
    "founded": 1878,
    "national": false,
    "logo": "https://media.api-sports.io/football/teams/33.png"
 },
 "venue": {
    "id": 556,
    "name": "Old Trafford",
    "address": "Sir Matt Busby Way",
    "city": "Manchester",
    "capacity": 76212,
    "surface": "grass",
    "image": "https://media.api-sports.io/football/venues/556.png"
 }
}

This method is useful if you have a few teams to retrieve in a few competitions, but it will consume a lot of API calls if you do it for all the competitions available in the API.

In fact, the method described above isn't the most efficient in terms of API calls if you need to retrieve all the teams available in the API, as there are currently over 1 000 competitions and several seasons for each competition. This would consume at least 5 000 API calls.

So what do I do if I want a list of all the teams in the API?

Let's have a look to the teams endpoint.

The teams endpoint contains a sub-endpoint called teams/countries which allows you to retrieve the list of all available team countries.

If we call this endpoint we'll get the list of countries that can be used as parameters for the teams endpoint.

/teams/countries :

{
  get: "teams/countries"
  parameters: [
  ]
  errors: [
  ]
  results: 230
  paging: {
  current: 1
  total: 1
}
response: [
{
  name: "Albania"
  code: "AL"
  flag: "https://media-3.api-sports.io/flags/al.svg"
}
{
  name: "Algeria"
  code: "DZ"
  flag: "https://media-3.api-sports.io/flags/dz.svg"
}

The value of the name field contained in the teams/countries response should therefore be used as country parameter when calling the teams endpoint.

So we'll have to loop over all the countries contained in the teams/countries response to call the teams endpoint.

Example for "Albania" : /teams?country=Albania

{
  get: "teams"
  parameters: {
    country: "Albania"
  }
  errors: [
  ]
  results: 82
  paging: {
    current: 1
    total: 1
  }
  response: [
    {
      team: {
        id: 577
        name: "FK Kukesi"
        code: "KUK"
        country: "Albania"
        founded: 1930
        national: false
        logo: "https://media-1.api-sports.io/football/teams/577.png"
      }
      venue: {
        id: 19194
        name: "Kukës Arena"
        address: "Rruga Stadiumi"
        city: "Kukës"
        capacity: 6200
        surface: "grass"
        image: "https://media-1.api-sports.io/football/venues/19194.png"
      }
    }

In this way, with 231 calls to the API (That's far less than 5 000), you can retrieve all the teams available in the API.

This approach is a good starting point for getting all the API teams on board. Please also note that we add new competitions and teams every month and that team data may also change (name, venue, etc.), but their ID will always remain the same. It will therefore be necessary to do an update regularly.

Here's an example in PHP that retrieves all the API teams. All you have to do is modify it to suit your needs.

<?php

/*
This function is used to call the API, replacing "YOUR_API_KEY_HERE" with your API-KEY.
If you are using a Rapidapi account you have to replace $url = 'https://v3.football.api-sports.io/'.$endpoint; by $url = 'https://api-football-v1.p.rapidapi.com/v3/'.$endpoint;
Only one parameter is required: the endpoint to call.
This function returns the data in the "response" field of the API response.
*/
function call_api($endpoint) {

    $headers = array(
        "x-rapidapi-key: YOUR_API_KEY_HERE"
    );
    $url = 'https://v3.football.api-sports.io/'.$endpoint;
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    $response = curl_exec($curl);
    curl_close($curl);
    $data = json_decode($response, true);

    if (isset($data["response"]) && count($data["response"]) > 0) {

        return $data["response"] ;
    }
    return null;
}

$all_teams = [] ; // This array will contain all the teams and their information
$countries = call_api('teams/countries'); //Call the API to retrieve all countries from the "teams/countries" endpoint

if ($countries !== null) {

    foreach ($countries as $country) {

        $teams = call_api('teams?country='.$country['name']); // Call the API to retrieve all teams in the country set as parameters
        if ($teams !== null) {

            foreach ($teams as $team) {

                $all_teams[] = $team ;
            }
        }
    }
}