FPL

The FPL class is the main class used for interacting with Fantasy Premier League’s API. It requires an aiohttp.ClientSession for sending requests, so typical usage of the FPL class can look something like this:

import asyncio
import aiohttp
from fpl import FPL

async def main():
    async with aiohttp.ClientSession() as session:
        fpl = FPL(session)
        await fpl.login()
        user = await fpl.get_user()
        my_team = await user.get_team()

    print(my_team)

# Python 3.7+
asyncio.run(main())
...
# Python 3.6
loop = asyncio.get_event_loop()
loop.run_until_complete(main())

Note that when calling the login function, you must either specify an email and password, or set up system environment variables named FPL_EMAIL and FPL_PASSWORD.

class fpl.fpl.FPL(session)[source]

The FPL class.

FDR()[source]

Creates a new Fixture Difficulty Ranking (FDR) based on the number of points each team gives up to players in the Fantasy Premier League. These numbers are also between 1.0 and 5.0 to give a similar ranking system to the official FDR.

An example:

{
  "Man City": {
      "all": {
      "H": 4.4524439427082,
      "A": 5
      },
      "goalkeeper": {
      "H": 3.6208195949129,
      "A": 5
      },
      "defender": {
      "H": 3.747999604078,
      "A": 5
      },
      "midfielder": {
      "H": 4.6103045986504,
      "A": 5
      },
      "forward": {
      "H": 5,
      "A": 3.9363219561895
      }
  },
  ...,
  "Arsenal": {
      "all": {
      "H": 3.4414041151234,
      "A": 4.2904529162594
      },
      "goalkeeper": {
      "H": 4.1106924163919,
      "A": 4.3867595818815
      },
      "defender": {
      "H": 3.6720291204673,
      "A": 4.3380917450181
      },
      "midfielder": {
      "H": 3.3537357534825,
      "A": 4.0706443384718
      },
      "forward": {
      "H": 2.5143403441683,
      "A": 4.205298013245
      }
  }
}
Return type:dict
get_classic_league(league_id, return_json=False)[source]

Returns the classic league with the given league_id. Requires the user to have logged in using fpl.login().

Information is taken from e.g.:
https://fantasy.premierleague.com/api/leagues-classic/967/standings/
Parameters:
  • league_id (string or int) – A classic league’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a ClassicLeague object. Defaults to False.
Return type:

ClassicLeague or dict

get_fixture(fixture_id, return_json=False)[source]

Returns the fixture with the given fixture_id.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/fixtures/ https://fantasy.premierleague.com/api/fixtures/?event=1
Parameters:
  • fixture_id (int) – The fixture’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a Fixture object. Defaults to False.
Return type:

Fixture or dict

Raises:

ValueError – if fixture with fixture_id not found

get_fixtures(return_json=False)[source]

Returns a list of all fixtures. Information is taken from e.g.:

Parameters:return_json (bool) – (optional) Boolean. If True returns a list of dicts, if False returns a list of Fixture objects. Defaults to False.
Return type:list
get_fixtures_by_gameweek(gameweek, return_json=False)[source]

Returns a list of all fixtures of the given gameweek.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/fixtures/ https://fantasy.premierleague.com/api/fixtures/?event=1
Parameters:
  • gameweek (string or int) – A gameweek.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of Player objects. Defaults to False.
Return type:

list

get_fixtures_by_id(fixture_ids, return_json=False)[source]

Returns a list of all fixtures with IDs included in the fixture_ids list.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/fixtures/ https://fantasy.premierleague.com/api/fixtures/?event=1
Parameters:
  • fixture_ids (list) – A list of fixture IDs.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of Fixture objects. Defaults to False.
Return type:

list

get_gameweek(gameweek_id, include_live=False, return_json=False)[source]

Returns the gameweek with the ID gameweek_id.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/bootstrap-static/ https://fantasy.premierleague.com/api/event/1/live/
Parameters:
  • gameweek_id (int) – A gameweek’s ID.
  • include_live (bool) – (optional) Includes a gameweek’s live data if True.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a Gameweek object. Defaults to False.
Return type:

Gameweek or dict

get_gameweeks(gameweek_ids=None, include_live=False, return_json=False)[source]

Returns either a list of all gamweeks, or a list of gameweeks whose IDs are in the gameweek_ids list.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/bootstrap-static/ https://fantasy.premierleague.com/api/event/1/live/
Parameters:
  • gameweek_ids (list) – (optional) A list of gameweek IDs.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of Gameweek objects. Defaults to False.
Return type:

list

get_h2h_league(league_id, return_json=False)[source]

Returns a H2HLeague object with the given league_id. Requires the user to have logged in using fpl.login().

Information is taken from e.g.:
https://fantasy.premierleague.com/api/leagues-h2h-matches/league/946125/
Parameters:
  • league_id (string or int) – A H2H league’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a H2HLeague object. Defaults to False.
Return type:

H2HLeague or dict

get_player(player_id, players=None, include_summary=False, return_json=False)[source]

Returns the player with the given player_id.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/bootstrap-static/ https://fantasy.premierleague.com/api/element-summary/1/ (optional)
Parameters:
  • player_id (string or int) – A player’s ID.
  • players (list) – (optional) A list of players.
  • include_summary (bool) – (optional) Includes a player’s summary if True.
  • return_json – (optional) Boolean. If True returns a dict, if False returns a Player object. Defaults to False.
Return type:

Player or dict

Raises:

ValueError – Player with player_id not found

get_player_summaries(player_ids, return_json=False)[source]

Returns a list of summaries of players whose ID are in the player_ids list.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/element-summary/1/
Parameters:
  • player_ids (list) – A list of player IDs.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of PlayerSummary objects. Defaults to False.
Return type:

list

get_player_summary(player_id, return_json=False)[source]

Returns a summary of the player with the given player_id.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/element-summary/1/
Parameters:
  • player_id (int) – A player’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a PlayerSummary object. Defaults to False.
Return type:

PlayerSummary or dict

get_players(player_ids=None, include_summary=False, return_json=False)[source]

Returns either a list of all players, or a list of players whose IDs are in the given player_ids list.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/bootstrap-static/ https://fantasy.premierleague.com/api/element-summary/1/ (optional)
Parameters:
  • player_ids (list) – (optional) A list of player IDs
  • include_summary (boolean) – (optional) Includes a player’s summary if True.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of Player objects. Defaults to False.
Return type:

list

get_points_against()[source]

Returns a dictionary containing the points scored against all teams in the Premier League, split by position and location.

An example:

{
  "Man City": {
      "all": {
      "H": [3, ..., 1],
      "A": [2, ..., 2]
      },
      "goalkeeper": {
      "H": [3, ..., 3],
      "A": [2, ..., 3]
      },
      "defender": {
      "H": [1, ..., 2],
      "A": [4, ..., 1]
      },
      "midfielder": {
      "H": [2, ..., 1],
      "A": [2, ..., 2]
      },
      "forward": {
      "H": [1, ..., 2],
      "A": [6, ..., 1]
      }
  },
  ...
}
Return type:dict
get_team(team_id, return_json=False)[source]

Returns the team with the given team_id.

Information is taken from:
https://fantasy.premierleague.com/api/bootstrap-static/
Parameters:
  • team_id (string or int) – A team’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a Team object. Defaults to False.
Return type:

Team or dict

For reference here is the mapping from team ID to team name:

 1 - Arsenal
 2 - Aston Villa
 3 - Brentford
 4 - Brighton
 5 - Burnley
 6 - Chelsea
 7 - Crystal Palace
 8 - Everton
 9 - Leicester
10 - Leeds
11 - Liverpool
12 - Man City
13 - Man Utd
14 - Newcastle
15 - Norwich
16 - Southampton
17 - Spurs
18 - Watford
19 - West Ham
20 - Wolves
get_teams(team_ids=None, return_json=False)[source]

Returns either a list of all teams, or a list of teams with IDs in the optional team_ids list.

Information is taken from:
https://fantasy.premierleague.com/api/bootstrap-static/
Parameters:
  • team_ids (list) – (optional) List containing the IDs of teams. If not set a list of all teams will be returned.
  • return_json (bool) – (optional) Boolean. If True returns a list of dict``s, if ``False returns a list of Team objects. Defaults to False.
Return type:

list

get_user(user_id=None, return_json=False)[source]

Returns the user with the given user_id.

Information is taken from e.g.:
https://fantasy.premierleague.com/api/entry/91928/
Parameters:
  • user_id (string or int) – A user’s ID.
  • return_json (bool) – (optional) Boolean. If True returns a dict, if False returns a User object. Defaults to False.
Return type:

User or dict

login(email=None, password=None)[source]

Returns a requests session with FPL login authentication.

Parameters:
  • email (string) – Email address for the user’s Fantasy Premier League account.
  • password (string) – Password for the user’s Fantasy Premier League account.