Application Program Interfaces: API, REST API, and Example Use in Sports Data

Jul 16, 2024

Application Program Interfaces (APIs)

What is an API

  • Allows two pieces of software to communicate
    • E.g., your program, data, and other software components
  • You interact with the API via inputs and outputs
  • Abstracts the internal workings; focus on inputs/outputs

API Libraries

  • Pandas API:
    • A set of software components, not all in Python
    • Used to process data by communicating with other software components
    • Example: Creating a dictionary and passing it to DataFrame constructor
      • This is an 'instance' in API terminology
      • Data in dictionary passed to Pandas API
    • Dataframe methods (head, mean) communicate with the API

REST API

  • Allows communication over the internet
  • Utilizes resources like storage, data access, AI algorithms
  • REST: Representational State Transfer
    • Client: Your program
    • Resource: The web service
    • Endpoint: How the client finds the service
    • Involves rules for communication, input (request) & output (response)

HTTP Methods

  • Transmit data over the internet
  • Requests communicated via HTTP messages (often JSON files)
  • Service performs operations and returns responses via HTTP messages
  • Information returned usually in JSON format

Example: NBA API by Swar Patel

  • Use Case: Constantly updating sports data
  • NBA API always updated from endpoints at NBA.com
  • Making a Request:
    • Requesting specific team info using unique id
    • Import module teams and use get_teams method
      • Returns list of dictionaries with team info
    • Convert dictionary to a table (DataFrame)
      • Use team nickname to find unique id
      • Use the id to request specific team information
  • League Game Finder:
    • Makes an API call with team_id_nullable (unique team ID)
    • API call transmitted to NBA.com, response returned
    • gamefinder object’s get_data_frame method returns DataFrame
    • DataFrame includes game details
      • PLUS MINUS column indicates game outcome (positive = win, negative = loss)
      • Matchup column details opponent and game location
    • Can create DataFrames for specific matchups (e.g., home vs away games)
    • Example visualization: Plotting PLUS MINUS for home vs away games (Warriors performed better at home)