Sample Code

Sample Code, Client Libraries & Tutorials

As well as sample code developed by Betfair, this page allows you to find sample code or documentation prepared by members of the Developer Program community. 

If you would like to contribute to this space, please contact Developer Support

All Betfair-developed sample code follows a typical workflow:

  • Find the next UK Horse Racing Win market

  • Get prices for the market

  • Place a bet on the market 

  • Handle the error returned by the API when the bet fails as it is below the minimum stake size.

Please Note: In order to aid ease of understanding, the basic Betfair samples are not intended to show certain best practices for speed and throughput. Well-designed applications should follow the best practices for client design of the application/language platform and should optimise on an HTTP request level with features such as requesting gzip'd responses and http connection keep alives. Please see Optimizing API Application Performance for further details.

The following samples are currently available:

Client Libraries & Sample Applications

Language

Available From

Description

Developed By

C#

https://github.com/joelpob/betfairng

API-NG Client Library for c#

Community member - joelpob

Java

https://github.com/joelpob/jbetfairng

Client library for Java

Community member - joelpob

Excel/VBA

https://github.com/betfair/API-NG-Excel-Toolkit

Excel Sample Spreadsheet Application

Robin Barrett

Delphi

https://github.com/betfair/API-NG-Delphi-Client

API-NG Client Library for Delphi

Community member - khughes

Javascript

https://github.com/AlgoTrader/betfair

API-NG Client Library for Node.js

Community member - Algotrader

Perl

https://github.com/MyrddinWyllt/WWW-BetfairNG

Perl Library for API-NG

Community member - Merlin

PHP

https://github.com/danieledangeli/betfair-php

API-NG Client Library for PHP

Community member - daniele8805

Ruby

https://github.com/mikecmpbll/betfair

Ruby wrapper for API-NG

Community member - mikecmpbll 

Python

https://github.com/betcode-org/betfair

Lightweight Python wrapper for Betfair API-NG (with streaming)

Community member - LiamP

Python

https://github.com/betcode-org/flumine

Betting Trading Framework

Community member - LiamP

Scala

https://github.com/city81/betfair-service-ng

Scala sample code for API-NG

Community member - theswan1

R

https://github.com/phillc73/abettor

Sample code for R

Community member - phill_c

C++

https://github.com/captain-igloo/greentop

C++ Betfair API Client

Community member - plachner

C++

https://github.com/tosinalagbe/hedg

High-frequency trading framework for Betfair

Community member - tosin

Rust

https://docs.rs/botfair/0.3.0/botfair/

Rust bindings for the Betfair API

Community member - esotericnonsense

Stream API

Language

Available From

Description

Developed By

C#

https://github.com/betfair/stream-api-sample-code/tree/master/csharp

Sample application for Stream API

Betfair 

Java

https://github.com/betfair/stream-api-sample-code/tree/master/java

Sample application for Stream API

Betfair

Node.js

https://github.com/betfair/stream-api-sample-code/tree/master/node.js

Sample application for Stream API

Betfair

Historical Data

 Code for processing the data provided by the Betfair Exchange historical data service available via https://historicdata.betfair.com

Language

Available From

Description

Developed By

Web Application

https://www.betfairhistoricdata.co.uk/

Simple web application for converting downloaded historical data files to CSV format.  Click Betfair Historical Data Processor - Quick Guide.pdf for a user guide

Community member - LiamP

Python

https://github.com/mberk/betfairviz

Create visualisations of Betfair order books

Community member - mberk

Python

https://github.com/betcode-org/betfair

Parse/output historical data allowing back testing or with a custom listener, csv creation

Community member - LiamP

Excel/VBA

https://github.com/betfair/historic-data-workbook

Simple Excel workbook for use with BASIC historical data Coverts files into Excel and provides basic custom reporting tools

Robin Barrett

Other Resources

Additional Information#CompetitionId&EventMappingData

Competition & Event Mapping Data for all historical markets from 2018-2023 (Please note:  mapping data prior to 2018 isn't available)

Betfair

Tutorials

Language

Available From

Description

Developed By

R

https://betfair-datascientists.github.io/tutorials/apiRtutorial/

Betfair API R Tutorial

Betfair Australia

Python

https://betfair-datascientists.github.io/tutorials/jsonToCsvRevisited/

Historical data - Json to Csv Tutorial using Python

Betfair Australia

Python

https://betfair-datascientists.github.io/tutorials/apiPythontutorial/

Betfair API Python Tutorial

Betfair Australia

Python

https://betfair-datascientists.github.io/historicData/backtestingRatingsTutorial/#complete-code

Backtesting wagering models with Betfair JSON stream data

Tom Bishop

Python

https://betfair-datascientists.github.io/tutorials/How_to_Automate_1/

How to Automate I: Understanding Flumine

Betfair Australia

Python

https://betfair-datascientists.github.io/tutorials/How_to_Automate_2/

How to Automate II: Backing or laying the 1st/2nd/.../nth favorite shot

Betfair Australia

         Python

https://betfair-datascientists.github.io/tutorials/How_to_Automate_3/

How to Automate III: Betfair Data Scientist's Models

Betfair Australia

         Python

https://betfair-datascientists.github.io/tutorials/How_to_Automate_4/

How to Automate IV: Automate your own Model

Betfair Australia

Python

https://betfair-datascientists.github.io/tutorials/How_to_Automate_5/

How to Automate 5

Betfair Australia