Tuesday 10th December 2019 

WARNING: the following information assumes that you are fluent in the Python programming language. This page is NOT for newbies!

Using the Betfair API-NG with Python

Python is the chosen language for development for many reasons:
  • Simple syntax - easy to learn. Ideal for beginners
  • Rapid development - requires less lines of code in comparison to other languages
  • Multi platform support - code will run on Windows, Linux, Mac, etc without modification
  • Python is pre-installed on Linux and Mac by default
  • Syntax forces indentation - reduces "messy" and unreadable code
  • Excellent support - a simple Google search will help you resolve most problems
  • Extensive libraries allow easy integration of specialized tasks
  • Smaller file sizes - a basic bot is less than 100KB in Python. The Java equivalent is several MB!
  • Ideal for web development - Python can be run from dedicated or VPS servers
Betfair have replaced their old SOAP based API6 with a new JSON formatted API-NG. The new API offers some new features such as retrieving prices for up to 5 markets in one request and filters which allow you to retrieve only markets you are interested in. Filtering is done server-side, so we no longer need to get ALL markets and then filter within our bots. The most significant upgrade is the addition of "metadata" such as horse racing form, trainer/jockey info, handicap ratings, forecast prices, etc. This information is pulled from the Betfair's Timeform website, so it is now possible to create a bot that "studies form" without needing to resort to 3rd party websites such as Sporting Life, Racing Post, etc.

The new API-NG offers a nicer interface that can be easily parsed using Python's built-in JSON library which allows us to work with the data in a standard dictionary object. If you have used my old API6 library, you will already be familiar with the use of dictionary formatted data.

Although easier to get up and running, the new API-NG does require that the user is fully conversant in programming and reference to the API-NG Reference Guide is ESSENTIAL. The reason for this is that many of the requests require user-defined parameters in the format of a dictionary. There are a significant number of parameters available, resulting in far too many combinations to be able to "dumb it down" to simple Python functions. My Python API-NG library attempts to make this more user friendly, however you WILL need to understand how the API-NG works in order to use it to its full potential.

Having said all that, the API is quite nice once you are familiar with it and gives us much more control over what data we receive from the Betfair servers. It should be noted that my API-NG library is NOT compatible with API6 (which is now obsolete), so if you have bots using API6, they will need to be completely re-written for API-NG.


Betfair have increased security and API users now require self-signed SSL certificates as well as their username and password in order to login and use other secure functions. Creation of the SSL certificates is a bit of a PITA, however it is only a one-off operation per betfair account. Please see my tutorial:
Betfair API-NG: Creating SSL Certificates For Login

You will also need to create an "Application Key" which will identify your bot to the Betfair servers. Instructions for both of these operations are detailed in the sample code (download below) README.txt and Betfair's documentation, so I will not repeat them here.

Once you have your SSL certificates and Application Keys, you will be able to start bot development. To get you started, here is a simple Python "Gubbins" bot that will:
  • Login to Betfair using the non-interactive login (requires SSL certs)
  • Download the complete markets menu as shown on Left side of the betfair exchange homepage
  • Filter the menu paths according to the rules in settings.py (currently setup for Horse Racing and Greyhound Racing WIN markets)
  • LAY the field for £2.00 at odds 1.01 and 1.02
  • If bets are successfully placed, add the market to an "ignores" list so that we do not bet on this market again in future
  • Loop continuously, searching for new markets and placing bets
  • Monitor number of bets placed to avoid the 1000 bets per hour transaction charges (bot is currently limited to 250 bets per hour in settings.py)
  • If the transaction limit is reached, the bot will sleep until a new hour starts. Note that your PC clock should be accurately synchronised with an NTP server: Linux, Mac, Windows
  • The manager.py file will handle any errors gracefully and ensure the bot runs 24/7. This is the standard setup I use for all of my bots and is ideally suited for use on a VPS or dedicated server
You MUST be conversant in Python to use this library. If you have never programmed in Python, then this library is NOT for you!

Please ensure you setup the bot before attempting to run it. Details on how to do this are in the README.txt file included in the download.

NOTE: This API-NG library has now been heavily tested and can be considered as a STABLE release.
The library is developed under Python 3.2 but is compatible with Python 2.7+.

API Library and Example Bot: gubbins-ng.zip (May, 2015)
API Library ONLY: api-ng.zip - V0.10 (April, 2017) changelog.txt

Creative Commons License
BespokeBots Betfair API-NG Library by Birchy is licensed under:
Creative Commons Attribution-NonCommercial 4.0 International License.