How to Create a Sports Betting Application with Python (using TheRundown API)

RapidAPI Team
The Era of APIs
Published in
8 min readJan 3, 2020

The sports industry is one of the most attractive and sturdy in entertainment. The level of attention to the top sports events is impressively high. That’s why sports betting is an exciting way to earn money. A simple spectator, who is familiar with the current team situation, can easily “invest” in them. Plus, gambling can make sports more interesting personally for you. So today, we will be talking about sports betting and some useful APIs for its implementation.

For our project, we’ll be using TheRundown ‘s sports betting API.

How to use TheRundown’s Sports Betting API on RapidAPI

Prerequisites

Of course, different implementations require some technical stack. In our case, we will use Python as the main programming development instrument. But we won’t just run the necessary Python files, we will also focus on the web application. So for this purpose, we will be using Flask — lightweight, swift, and very powerful micro-framework.

Also, there is a need to make requests with responsive handling. Python has many high-quality solutions and requests are one of them. Both libraries should be installed in your Python version (whatever it will be — local or virtual). The most obvious way is via pip command.

pip install flask
pip install requests

After that, you can go to the project directory and create file main.py. For now, let’s add the following content:

from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello world!"
if __name__ == "__main__":
app.run(debug=True)

You can test this app by running the file and visit your localhost in the browser. If all is good, the language prerequisites are done, so let’s talk about API.

If you don’t want to waste your time on searching API hub process, you should take a look at the RapidAPI. Here you can find everything possible for APIs. And all, what is required — a simple registration. After that, the world of application programming interfaces is open for you.

A user account on RapidAPI gives private and non-limited access to all the existing endpoints here. One can create multiple applications for projects separately, make some direct interaction, and not be afraid about security and storage.

Making an API Call

Finally, it’s time to do some practice test calls. One of the most interesting sports betting APIs is TheRundown. You can find it just by search on the main RapidAPI page.

This is a compelling and informative betting system. Here you can find detailed information about various sports and leagues per many different affiliates. Also, this API allows getting more advanced bets during real-time moments of game, e.g., 1st Quarter / Period situation.

The main window of the concrete API can be subdivided into a few sections. Let’s talk about them briefly.

The upper part of the page is delegated to the description tabs. There are four of them:

  • endpoints — this is the mainframe or API console where we can explore the existing requests, test them and even find some useful snippets for various languages
  • API details — information from the API authors, it can be in the docs format or just a link to some other pages
  • discussions — discussion threads for questions about using the API
  • pricing — tiered subscription plans based on API usage. hint: there’s a freemium plan that gives you 25 free API calls/day. However, it’s likely you’ll need to subscribe to a heavier usage plan if you plan on having a lot of queries.

Let’s test some endpoints of TheRundown. First of all, it can be helpful to find out all the supportable sports. Pick the Sports dropdown object on the left side and choose the GET Sports request.

As you may see, it doesn’t require any parameters for the request, so we can immediately execute it. Press the Test Endpoint button and you should see the response on the right side of the window.

TheRundown supports data for multiple sports and we can interact with any of them. Let’s choose basketball, for example.

Take note that the unique sport_id of the basketball is 4.

Also, on the screen above, you can find a snippet on Python with an endpoint using. It looks like a signal to return to our Flask application, doesn’t it?

Process the response

All that we want right now is the same response from our code as in the browser test. Modify main.py with the following lines:

from flask import Flask
import requests
app = Flask(__name__)
url = "https://therundown-therundown-v1.p.rapidapi.com/"
headers = {
'x-rapidapi-host': "therundown-therundown-v1.p.rapidapi.com",
'x-rapidapi-key': "<YOUR_RAPIDAPI_KEY>"
}
# Endpoints
sports = "sports"
see the rest at https://rapidapi.com/blog/sports-betting-app-python/

As you may notice, the code is modified by adding a few objects for headers (valuable data for HTTP communication) and URL. In the home method, we call the request and return all response in the JSON format. Right now, if you run this file, the output should look something like this:

Now that we’ve successfully connected to the API, let’s explore some practice applications.

Example: Create a Dynamic Bidding Guide

It is evident that right now, our app is too limited by functionality. And for the demonstration of TheRundown potential, we will expand it.

The idea of the app is that it will be the guide for the actual basketball games in the NBA. Users will have not only short information about teams and locations but also an overview of all the opened betting lines.

The main endpoint of this application — events by sport. Here we need to point the sports_id (4 for the basketball, remember?). The response should be similar to this:

At this moment, there are three games and we would like to make some cards with each of them. Each card will contain the place of the game, time, and both participating teams. In the lower part, we will add three dropdown lists for moneylines, spread lines, and totals with all available affiliates.

Let’s prepare all the necessary data on the server-side. Open main.py and paste this code:

from flask import Flask, render_template
import requests
app = Flask(__name__)url = "https://therundown-therundown-v1.p.rapidapi.com/"headers = {
'x-rapidapi-host': "therundown-therundown-v1.p.rapidapi.com",
'x-rapidapi-key': "<YOUR_RAPIDAPI_KEY>"
}
# Endpoints
nba_events = "sports/4/events"
def get_lines(line_periods):

See the rest at: https://rapidapi.com/blog/sports-betting-app-python/

Don’t worry, a significant part of the code is just a response parsing.

After the request is sent, the received JSON data needs to be prepared. Our task is to make the data structure that will store all events with selected information. A brief description of the game is taken from the primary event node. Pay attention, that the final object with data is a dictionary.

However, with odds, we need to be more delicate. As they are stored in the deeper layer ( event->line_periods->period_full_time), there is a separate method for getting this data.

Finally, the code returns the template of the HTML-page with the event information. Now it’s part of prettifying the visualization, and this will be released via Flask templates.

As we don’t want to dive deep into web development today, let’s use some Bootstrap styles. They are free-to-use, cover many use cases, and have detailed documentation. Today we will use two components: dropdowns and cards.

First of all, create folder static inside of the project directory. This folder usually responds to static resources. In our case, there will be only one custom stylesheets file. Create a style.css file here and paste the next lines:

.grid-container {
display: grid;
grid-template-columns: auto auto auto;
padding: 10px;
grid-row-gap: 50px;
grid-column-gap: 50px;
}
.grid-item {
padding: 20px;
width: auto;
}

These two classes will help display our game cards in the adaptive and helpful grid. Now return to the root directory of the project and create a templates folder. Here, Flask will be looking for HTML templates. We will need two of them.

Create a file base.html and add this code:

<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Custom style file -->
<link rel="stylesheet" href="{{ url_for ('static', filename='style.css')}}">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
<title>{% block title %} Base page {% endblock %}</title>
</head>
<body>
{% block body %}
Body block
{% endblock %}
</body>
</html>
see the rest at: https://rapidapi.com/blog/sports-betting-app-python/

Here we add the required files: static styles and Bootstrap scripts.

Also, pay attention to the {% %} blocks. It’s called Jinja2 and allows us to make more advanced HTML templates. For example, Jinja connects sent Python objects with templates, extends layouts, etc.

Now we can finally create the main template of the project — index.html:

{% extends 'base.html' %}
{% block title %} NBA booking {% endblock %}
{% block body %}
<h1 class="display-4" style="text-align: center;">NBA events betting lines</h1>
<div class="grid-container">
{%for event in events%}
<div class="card grid-item" >
<div class="card-body">
<center><h6 class="card-subtitle mb-2 text-muted">{{ event['place'] }}</h6></center>
<center><h5 class="card-title">{{ event['teams'] }}</h5></center>
<center><h6 class="card-subtitle mb-2 text-muted">{{ event['time'] }}</h6></center>
<center>
<div class="btn-group">
<button type="button" class="btn btn-success dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
Moneylines
</button>
<div class="dropdown-menu">
{% for bet in event['bets']['moneylines'] %}
<a class="dropdown-item" href="#">{{ bet }}</a>
{% endfor %}
</div>
</div>
see the rest at: https://rapidapi.com/blog/sports-betting-app-python/

We’ve now implemented the dropdown and card objects in loops based on the event data.

Now save all your files and test out your project:

From our example, there are only three available games. Each of them is shown in the separate card with dropdowns for possible odds. Each odd category has a list of possible scores from different affiliates. With this app, you can now see whenever the hot NBA games are coming.

Originally published at https://rapidapi.com on January 3, 2020.

--

--