API Tutorial and Examples


Intro

Our free sports API has been designed to be easy to use and quick to get working.
All our V1 API methods should work in a standard browser with the jsonview extension.
Or for v2 API try HTTPie, Postman or the Chrome Modify Header extension.


V1 API

PHP
Simple example here to show the teams badge after a name search for "Arsenal".
$json = file_get_contents("https://www.thesportsdb.com/api/v1/json/3/searchteams.php?t=Arsenal");
$json_decoded = (json_decode($json));
echo 'Team Badge = ' . ($json_decoded->teams[0]->strBadge);

Python
Here we are looking up certain events by ID and printing the output to the console.
import requests

def lookup_events(event_ids):
    for event_id in event_ids:
        api_call = requests.get(f"https://www.thesportsdb.com/api/v1/json/3/lookupevent.php?id={event_id}")
        storage = api_call.json()
        for event in storage["events"]:
            date_event = event["dateEvent"]
            home_team = event["strHomeTeam"]
            away_team = event["strAwayTeam"]

        print(f"{date_event}: {home_team} vs {away_team}")

event_ids = [2052711, 2052712, 2052713, 2052714]

lookup_events(event_ids)

Javascript
Just reading the JSON file here and outputting the entire payload to the console
function fetchJSONData() {
            fetch("https://www.thesportsdb.com/api/v1/json/123/searchteams.php?t=Arsenal")
                .then((res) => {
                    if (!res.ok) {
                        throw new Error
                            (`HTTP error! Status: ${res.status}`);
                    }
                    return res.json();
                })
                .then((data) => 
                      console.log(data))
                .catch((error) => 
                       console.error("Unable to fetch data:", error));
        }
        fetchJSONData();


V2 API


Command Line
Lets show the livescores using curl on the command line of Linux, Windows or Mac

curl -X GET https://www.thesportsdb.com/api/v2/json/livescore/soccer -H "X-API-KEY: xxxxxx" -L
						

PHP
Showing Livescore using CURL and PHP

$apiKey = "xxxxxx";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://www.thesportsdb.com/api/v2/json/livescore/soccer");
curl_setopt($curl, CURLOPT_HTTPGET, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('X-API-KEY:' . $apiKey));
$result = curl_exec($curl);
$json = json_decode($result);
var_dump($json);
						

Python
Get League data using Python

import requests

url = "https://www.thesportsdb.com/api/v2/json/all/leagues"
api_key = xxxxxx

headers = {
    "X-API-KEY": f"{api_key}",
    "Content-Type": "application/json"
}

response = requests.get(url, headers = headers)

if response.status_code == 200:
    print(response.json())
else:
    print(f"Request failed with status code: {response.status_code}")
						

Javascript

Get League data using Javascript

var fbURLv2="https://www.thesportsdb.com/api/v2/json/all/leagues";
var commentdata = "";
$.ajax({
    url: fbURLv2,
    data: "message="+commentdata,
    dataType: "json",
    type: 'POST',
    beforeSend: function(xhr) {
        xhr.setRequestHeader('X-API-KEY', 'xxxxxx');
        xhr.setRequestHeader('Content-Type', 'application/json');
    },
    // If success         
    success: function (resp) {
        console.log(resp);
    },
    // If error
    error: function(e) {
        console.log(e);
    }
});