Chapter 5: Incorporating Odds Data
In this chapter, we’ll explore how to incorporate odds data into our football application using the API. We’ll cover how to retrieve odds for upcoming matches, handle the odds data, and integrate it into our Python application.
5.1 Utilising Odds Endpoints
We provide various endpoints for accessing odds data for upcoming matches. We’ll explore these endpoints to understand how to retrieve odds information for different matches.
5.2 Fetching Odds for Upcoming Matches
We’ll implement functions in Python to retrieve odds data for upcoming matches in the UEFA Champions League. These functions will send GET requests to the appropriate odds endpoints and parse the JSON responses to extract the odds information.
# Initialize your Python environment
# Import required libraries
import requests
# Define your API key
api_key = "YOUR_API_KEY_HERE"
# Define the API endpoint URL
base_url = "https://api.sportmonks.com/v3/football"
endpoint = "/fixtures/between/2024-04-16/2024-04-17"
filters = "fixtureLeagues:2;bookmakers:2;markets:1"
include = "participants;odds"
endpoint_url = f"{base_url}{endpoint}?filters={filters}&include={include}"
# Include API key in request headers
headers = {"Authorization": api_key}
# Send the request
response = requests.get(endpoint_url, headers=headers)
# Check if the request was successful
if response.status_code == 200:
fixtures_data = response.json()
# Extract and process fixture information
for fixture in fixtures_data['data']:
print("Fixture:", fixture['name'])
print("Starting at:", fixture['starting_at'])
print()
print("Participants:")
for participant in fixture['participants']:
print("- Name:", participant['name'])
print(" Location:", participant['meta']['location'])
print()
print("Odds:")
for odd in fixture['odds']:
print("- Label:", odd['label'])
print(" Value:", odd['value'])
print("----------------------------------------------")
else:
print("Failed to retrieve data. Status code:", response.status_code)
{
"data": [
{
"id": 19101802,
"sport_id": 1,
"league_id": 2,
"season_id": 21638,
"stage_id": 77463969,
"group_id": null,
"aggregate_id": 52050,
"round_id": null,
"state_id": 5,
"venue_id": 2166,
"name": "Borussia Dortmund vs Atlético Madrid",
"starting_at": "2024-04-16 19:00:00",
"result_info": "Borussia Dortmund won after full-time.",
"leg": "2/2",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"starting_at_timestamp": 1713294000,
"participants": [
{
"id": 68,
"sport_id": 1,
"country_id": 11,
"venue_id": 2166,
"gender": "male",
"name": "Borussia Dortmund",
"short_code": "BVB",
"image_path": "https://cdn.sportmonks.com/images/soccer/teams/4/68.png",
"founded": 1909,
"type": "domestic",
"placeholder": false,
"last_played_at": "2024-04-21 15:30:00",
"meta": {
"location": "home",
"winner": true,
"position": 2
}
},
{
"id": 7980,
"sport_id": 1,
"country_id": 32,
"venue_id": 140808,
"gender": "male",
"name": "Atlético Madrid",
"short_code": "ATM",
"image_path": "https://cdn.sportmonks.com/images/soccer/teams/12/7980.png",
"founded": 1903,
"type": "domestic",
"placeholder": false,
"last_played_at": "2024-04-21 16:30:00",
"meta": {
"location": "away",
"winner": false,
"position": 1
}
}
],
"odds": [
{
"id": 99109805550,
"fixture_id": 19101802,
"market_id": 1,
"bookmaker_id": 2,
"label": "Away",
"value": "3.10",
"name": null,
"sort_order": 2,
"market_description": "Full Time Result",
"probability": "32.26%",
"dp3": "3.100",
"fractional": "31/10",
"american": "210",
"winning": false,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "2",
"latest_bookmaker_update": "2024-04-16 18:50:04"
},
{
"id": 99109805547,
"fixture_id": 19101802,
"market_id": 1,
"bookmaker_id": 2,
"label": "Draw",
"value": "3.50",
"name": null,
"sort_order": 1,
"market_description": "Full Time Result",
"probability": "28.57%",
"dp3": "3.500",
"fractional": "7/2",
"american": "250",
"winning": false,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "Draw",
"latest_bookmaker_update": "2024-04-16 18:50:04"
},
{
"id": 99109805544,
"fixture_id": 19101802,
"market_id": 1,
"bookmaker_id": 2,
"label": "Home",
"value": "2.15",
"name": null,
"sort_order": 0,
"market_description": "Full Time Result",
"probability": "46.51%",
"dp3": "2.150",
"fractional": "43/20",
"american": "114",
"winning": true,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "1",
"latest_bookmaker_update": "2024-04-16 18:50:04"
}
]
},
{
"id": 19101806,
"sport_id": 1,
"league_id": 2,
"season_id": 21638,
"stage_id": 77463969,
"group_id": null,
"aggregate_id": 52052,
"round_id": null,
"state_id": 5,
"venue_id": 300228,
"name": "FC Barcelona vs Paris Saint Germain",
"starting_at": "2024-04-16 19:00:00",
"result_info": "Paris Saint Germain won after full-time.",
"leg": "2/2",
"details": null,
"length": 90,
"placeholder": false,
"has_odds": true,
"starting_at_timestamp": 1713294000,
"participants": [
{
"id": 591,
"sport_id": 1,
"country_id": 17,
"venue_id": 131,
"gender": "male",
"name": "Paris Saint Germain",
"short_code": "PSG",
"image_path": "https://cdn.sportmonks.com/images/soccer/teams/15/591.png",
"founded": 1970,
"type": "domestic",
"placeholder": false,
"last_played_at": "2024-04-24 17:00:00",
"meta": {
"location": "away",
"winner": true,
"position": 2
}
},
{
"id": 83,
"sport_id": 1,
"country_id": 32,
"venue_id": 300228,
"gender": "male",
"name": "FC Barcelona",
"short_code": "BAR",
"image_path": "https://cdn.sportmonks.com/images/soccer/teams/19/83.png",
"founded": 1899,
"type": "domestic",
"placeholder": false,
"last_played_at": "2024-04-21 19:00:00",
"meta": {
"location": "home",
"winner": false,
"position": 1
}
}
],
"odds": [
{
"id": 99109805305,
"fixture_id": 19101806,
"market_id": 1,
"bookmaker_id": 2,
"label": "Away",
"value": "3.00",
"name": null,
"sort_order": 2,
"market_description": "Full Time Result",
"probability": "33.33%",
"dp3": "3.000",
"fractional": "3",
"american": "200",
"winning": true,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "2",
"latest_bookmaker_update": "2024-04-16 18:50:06"
},
{
"id": 99109805294,
"fixture_id": 19101806,
"market_id": 1,
"bookmaker_id": 2,
"label": "Draw",
"value": "4.00",
"name": null,
"sort_order": 1,
"market_description": "Full Time Result",
"probability": "25%",
"dp3": "4.000",
"fractional": "4",
"american": "300",
"winning": false,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "Draw",
"latest_bookmaker_update": "2024-04-16 18:50:06"
},
{
"id": 99109805292,
"fixture_id": 19101806,
"market_id": 1,
"bookmaker_id": 2,
"label": "Home",
"value": "2.15",
"name": null,
"sort_order": 0,
"market_description": "Full Time Result",
"probability": "46.51%",
"dp3": "2.150",
"fractional": "43/20",
"american": "114",
"winning": false,
"stopped": false,
"total": null,
"handicap": null,
"participants": null,
"created_at": "2024-04-10T23:21:27.000000Z",
"original_label": "1",
"latest_bookmaker_update": "2024-04-16 18:50:06"
}
]
},
This script performs the following steps:
– Constructs the request URL with the specified filters and includes.
– Sends the HTTP GET request to the Sportmonks API.
– Checks if the request was successful (status code 200).
– Parses the JSON response data.
– Extracts and prints fixture information, including participants and odds.
– You need to replace “your_api_key_here” with your actual Sportmonks API key to authenticate the request. This script prints fixture details, participant information (including their location), and odds for each fixture retrieved from the API response.