betfairlightweight
raw JSON → 2.23.2 verified Fri May 01 auth: no python
Lightweight Python wrapper for the Betfair API-NG. Provides access to Betfair's streaming and REST APIs for betting exchange operations. Current version 2.23.2, requires Python >=3.9. Released under MIT license. Active development with frequent releases.
pip install betfairlightweight Common errors
error ModuleNotFoundError: No module named 'betfairlightweight' ↓
cause The package is not installed or the Python environment is wrong.
fix
Run 'pip install betfairlightweight' and ensure you're using the correct environment (e.g., virtualenv).
error betfairlightweight.exceptions.LoginError: INVALID_USERNAME_OR_PASSWORD ↓
cause Wrong credentials or expired app key. Betfair may also require IP whitelisting for app keys.
fix
Double-check username/password. Ensure your app key is valid and IP is whitelisted in Betfair's developer portal.
error AttributeError: module 'betfairlightweight' has no attribute 'Betfair' ↓
cause Imported incorrectly. The Betfair class is not directly under the module.
fix
Use 'from betfairlightweight import Betfair' instead of 'import betfairlightweight; betfairlightweight.Betfair'.
error socketIO_client.exceptions.SocketIOError: Connection refused ↓
cause Streaming API connection failed. Usually because of network issues or incorrect app key permissions.
fix
Ensure your app key has streaming permissions enabled. Check firewall/proxy settings. Use 'wss://stream-api.betfair.com' as the streaming URL.
Warnings
breaking In version 2.0.0, the streaming API was rewritten. Old code using 'StreamListener' or 'streaming_socket' will not work. Migrate to 'StreamingResources' and 'MarketDataFilter'. ↓
fix Use 'from betfairlightweight.streaming import StreamingResources' and follow the new streaming pattern.
breaking The 'endpoint' parameter was removed from the Betfair constructor. Use environment variables or pass directly via 'app_key', 'username', 'password'. ↓
fix Remove 'endpoint' from Betfair() call. Use credentials as above.
gotcha Login may have a rate limit. Calling 'login()' rapidly can cause temporary blocks. Use a single login session and reuse the client. ↓
fix Cache the client session and call login() only once per session.
deprecated The 'betfairlightweight.endpoints' module is deprecated in favour of 'betfairlightweight.betting' and 'betfairlightweight.account'. ↓
fix Replace imports from 'betfairlightweight.endpoints' with 'betfairlightweight.betting' or 'betfairlightweight.account'.
Imports
- Betfair wrong
import betfairlightweightcorrectfrom betfairlightweight import Betfair - StreamingResources wrong
from betfairlightweight import StreamingResourcescorrectfrom betfairlightweight.streaming import StreamingResources
Quickstart
import os
from betfairlightweight import Betfair
trading = Betfair(
app_key=os.environ.get('BF_APP_KEY', ''),
username=os.environ.get('BF_USERNAME', ''),
password=os.environ.get('BF_PASSWORD', ''),
)
# Login
trading.login()
# Get all event types
event_types = trading.betting.list_event_types()
print(event_types)
# Logout
trading.logout()