Schedules & Scores API for Streaming Live Sports StatsChris Riccomini on September 29, 2012
Recently, I’ve been looking for an API that exposes schedules and scores for “the big four” American leagues. Here’s what I was looking for:
- Daily game schedules for MLB, NBA, NFL, and NHL (the big four).
- Scores and times for MLB, NBA, NFL, and NHL. Can be delayed a minute or two.
I did a bit of reading, and found a few relevant Quora discussions:
- What options are there for streaming sports stats APIs?
- Are there any APIs with game schedules available for NFL, NCAA FB, NBA, NCAA BB, and MLB teams?
After some research, I was able to get the data I needed for free via either of two sources: MSNBC or ESPN. Later, I decided that I wanted a more secure/legitimate/reliable method of getting score data, so I switched to a pay service called XML Live Scores. I’ve decided to document my experience in this post.
I encourage you not to abuse the free APIs by polling them too frequently.
As I said, when I first started out, I was interested in a free solution. I found a few nice write-ups on how to get the scores from ESPN.
It turns out, ESPN has a little app called BottomLine, which makes HTTP requests to a few endpoints to get live sports data. This data is very easy to parse. Here are a few examples:
As you can see, the data is just URL encoded values. Everything here is pretty much live.
League sites (free)
Each league (NBA, NFL, NHL, MLB) exposes live scores on their website. All of these guys use AJAX, which means it’s possible to yank out the call that they’re using, and make the call yourself, programmatically. Here are some relevant end points:
You will have to reverse engineer the format of the data, but it’s usually pretty straight forward. I didn’t bother with this solution because polling these end points is usually a violation of the terms of service for the respective league, and I didn’t want to have a per-league score parser.
Another alternative that I found, but have not seen documented anywhere, is to use MSNBC’s live scores. Nearly all news outlets have live scores for sports, so all you need to do is find one that’s making AJAX calls to refresh the scores, and pull out the URL that they’re using. The one with the easiest formatting was MSNBC.
When looking at the MSNBC scores in the browser, make sure to view the source, since they have XML encoded in JSON (sigh).
This is the route that I ended up using for free scores. Unlike ESPN’s API, which is a bit clunky, MSNBC provides a nice JSON interface. I polled these scores once every 30 seconds, in rotation (MLB, wait 30s, NBA, wait 30s, NFL, wait 30s, etc). Here’s some example code (in Python) to get live scores:
I should note that I’m using a couple of Python libraries for help:
Again, this is a violation of their terms of service, so beware.
XML Live Scores (<$300/mo)
These are the guys that I use now. Their API is reasonably reliable, and they are really responsive via e-mail. Strangely, they seem to have two sites:
I contacted them via the first one (http://xml-livescores.com/), but their API is on the second one. Here’s an example of their XML data.
To get the data, an HTTP request is made:
$ curl http://xml-sportsfeeds.com/xml/baseball/livescore/?key=...
I pay in Euros, via PayPal.
I spoke with an XML team sales rep, who told me that I could get what I needed using their FlexSport On Demand package, which is their low-end pay-as-you-go package. This would have worked great if I were just interested in scheduling, but since I wanted near-realtime scores, the cost would have been too great. They wanted something like 25c per league score request, which would have worked out to 25c a minute per league.
Before using MSNBC, I was actually using FanFeedr. At the time, they provided a free API to get live scores, topics, discussion, roster information, etc. Shortly after I began using the site, though, they began charging a minimum of $3,500 a month. Here’s my discussion with them.
Sports Direct (~$4,000/mo)
I got in touch with a guy named John Morash. His response to my inquiry was:
Thanks for getting back to me. We do have the items that you’re looking for but the cost of our service is much greater than $300/mo. We’re not at the level of STATS Inc. ($4k+) but feel our product is every bit as good. We don’t compete on price with some of the low-cost vendors however, so it sounds like there may be another option that’s a better fit for you.
There are some other interesting APIs and sites that I didn’t bother looking at.