Handling Odds
Overview
The Sports Game Odds API comes complete with odds and result data for every event.
This guide will show you how you can easily fetch and parse the odds for a specific event or group of events!
Example
In our previous example you saw how you would fetch upcoming NBA events from the API using our cursor pattern, let’s take that a step further!
Now, assuming the NBA week has passed, we will fetch all finalized NBA events from that week, then parse the odds results for each event, so we can grade them.
js
let nextCursor = null;
let eventData = [];
do {
try {
const response = await axios.get('/v2/events', {
params: {
leagueID: 'NBA',
startsAfter: '2024-04-01',
startsBefore: '2024-04-08',
finalized: true,
cursor: nextCursor
}
});
const data = response.data;
eventData = eventData.concat(data.data);
nextCursor = data?.nextCursor;
} catch (error) {
console.error('Error fetching events:', error);
break;
}
} while (nextCursor);
// Now that we have the events, let's parse the odd results!
// Based on the bet type, compare score to the odds and grade the odds, for this example assume the odds are over/under
eventData.forEach((event) => {
const odds = event.odds;
Object.values(odds).forEach((oddObject) => {
const oddID = oddObject.oddID;
const score = parseFloat(oddObject.score);
const closeOverUnder = parseFloat(oddObject.closeOverUnder);
if (score > closeOverUnder)
console.log(`Odd ID: ${oddID} - Over Wins`);
else if (score === closeOverUnder)
console.log(`Odd ID: ${oddID} - Push`);
else
console.log(`Odd ID: ${oddID} - Under Wins`);
});
});