Skip to main content

Lightweight python wrapper for Betfair API-NG

Project description


Build Status Coverage Status PyPI version Downloads

Lightweight, super fast (uses C and Rust libraries) pythonic wrapper for Betfair API-NG allowing all betting operations (including market and order streaming) and account operations, see examples.


join betcode slack group

Currently tested on Python 3.7, 3.8, 3.9 and 3.10.


$ pip install betfairlightweight

To use C/Rust libraries install with

$ pip install betfairlightweight[speed]


In order to connect to the Betfair API you will need an App Key, SSL Certificates and a username/password.

App Key

Follow these instructions to get your app key, you can either use a delayed or live key.

SSL certificates

Follow these instructions to set up your SSL certificates. Save your .ctr and .key files to a local directory. The default directory where the library is looking for the keys is '/certs' but you can specify any other directory.

Using the library

The library can then be used as follows:

import betfairlightweight

trading = betfairlightweight.APIClient('username', 'password', app_key='app_key', certs='/certs')


or the following for interactive login with no certs (not as secure)

import betfairlightweight

trading = betfairlightweight.APIClient('username', 'password', app_key='app_key')

event_types = trading.betting.list_event_types()

[<EventTypeResult>, <EventTypeResult>, ..]

Following endpoints are available:


Currently two listeners available, below will run the base listener which prints anything it receives. Stream listener is able to hold an order stream or a market stream (one per listener). The listener can hold a cache and push market_books/order_books out via a queue.

Exchange Stream API

from betfairlightweight.filters import (

betfair_socket = trading.streaming.create_stream()

market_filter = streaming_market_filter(
market_data_filter = streaming_market_data_filter(
    fields=['EX_ALL_OFFERS', 'EX_MARKET_DEF'],


betfair_socket.start()  # blocking

historic data

The historic endpoint provides some basic abstraction for the historicdata api:

Historic Data API


[{'plan': 'Basic Plan', 'purchaseItemId': 1343, 'sport': 'Cricket', 'forDate': '2017-06-01T00:00:00'}]

Taking advantage of the streaming code lightweight can parse/output historical data in the same way it process streaming data allowing backtesting or with a custom listener, csv creation (see examples).

Historic Data

stream = trading.streaming.create_historical_stream(


or use the stream generator:

stream = trading.streaming.create_historical_generator_stream(

g = stream.get_generator()

for market_books in g():

Project details

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

betfairlightweight-2.17.0.tar.gz (220.6 kB view hashes)

Uploaded source

Built Distribution

betfairlightweight-2.17.0-py3-none-any.whl (68.5 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page