Filtering

Filter the unneeded data for more efficient responses!

In this chapter, we’ll teach you how to filter out data from our API, which is useful when you want to request specific data and can omit the rest for faster response time. You can filter out data for various parameters per endpoint. See our API reference guide for more detailed information. For this tutorial, we’ll use the get fixtures by date endpoint.

This endpoint has the following parameters:

  • Leagues: filter on league IDs

  • Bookmakers*: filter on bookmaker IDs

  • Markets:* filter on market IDs

  • Status: filter on status

You can only filter on the bookmakers & markets parameters when you've enriched your response with the odds include.

In this tutorial, we’ll show you step by step how filtering works, but most importantly, why you would want to use it.

Create the request

First, we’ll show you why exactly filtering is useful. For example, we want to have all the fixtures on a specific date of the Italian Serie A (league id: 384).

As mentioned above, we’re going to use the same endpoint as in our includes tutorial. Requesting all the fixtures played on the 2nd of August 2020:

https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?api_token={API_TOKEN}

Evaluate the response

The request works, but it is far from efficient. While you do receive the data you requested, it is incredibly hard to find the data for only the Italian Serie A (league id: 384) because you’ve just requested every single fixture on that date. For this example, we made the request with an Enterprise Plan subscribed API token, which means that the API would return all fixtures of 1200+ leagues.

Note, that there is only one league id in the previous response snippet. And it's not a fixture from the Italian Serie A (league id: 384).

You can find a short overview of the response at the bottom of the request. This is called the meta description:

],
        "pagination": {
            "total": 570,
            "count": 100,
            "per_page": 100,
            "current_page": 1,
            "total_pages": 6,
            "links": {
                "next": "https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?page=2"
            }
        }
    }

As you can see, the API returns a total of 570 results with 100 results per page. Obviously, this is way too much if you only want to request fixtures for a specific league. Therefore, this request is inefficient and needs some slight adjustments.

Please check our pagination tutorial for more information about pages.

Improve the request

This is where filtering comes into play. Because we only want the fixtures of the Italian Serie A (league id: 384) in our response. We can easily exclude the rest of the data that we won’t be needing. Simply add the leagues parameter to your request:

https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?api_token={API_TOKEN}&leagues={IDs}

Now, you can simply add the league id of the Italian Serie A to your request: &leagues=384

The result? Only five fixtures instead of 570! Again, you can see this in the meta description of the response.

https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?api_token={API_TOKEN}&leagues=384

Did you get a blank response?

Note that there may be no fixtures on the date you’ve requested in the leagues you’ve access to.

Since we’re on a roll, we might as well filter on more than just one league. Simply add more league ids! You can separate them with a comma:

https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?api_token={API_TOKEN}&leagues=8,384,501,271....etc. 

Filtering and using includes

In the previous chapter, you’ve learned about includes. Thanks to our API’s flexibility, you can filter and use includes in the same request! Let’s mix up the previous request with some includes:

https://soccer.sportmonks.com/api/v2.0/fixtures/date/2020-08-02?api_token={API_TOKEN}&include=localTeam,visitorTeam,events&leagues=384