class: center, middle, inverse, title-slide .title[ # ISA 401: Business Intelligence & Data Visualization ] .subtitle[ ## 07: Connecting to APIs in
] .author[ ###
Fadel M. Megahed, PhD
Professor of Information Systems and Business Analytics
Farmer School of Business
Miami University
@FadelMegahed
fmegahed
fmegahed@miamioh.edu
Automated Scheduler for Office Hours
] .date[ ### Fall 2024 ] --- # Quick Refresher from Last Week ✅ Understand when can we scrape data (i.e., `robots.txt`) ✅ Scrape a webpage Using
✅ Utilize loops to scrape data from multiple webpages --- # Kahoot Competition # 1 To assess your understanding and retention of the topics covered last week, you will **compete in a Kahoot competition (consisting of 16 questions)**: - Go to <https://kahoot.it/> - Enter the game pin, which will be shown during class - Provide your first (preferred) and last name - Answer each question within the allocated 20-second window (**fast and correct answers provide more points**) <br> **Winning the competition involves having as many correct answers as possible AND taking the shortest duration to answer these questions.** The winner
of the competition from each section will receive: $10 Starbucks gift card. Good luck!!! .footnote[ <html> <hr> </html> **P.S:** The Kahoot competition will have **no impact on your grade**. It is a **fun** way of assessing your knowledge, motivating you to ask questions about topics covered that you do not have a full understanding of it, and providing me with some data that I can use to pace today's class. ] --- # Learning Objectives for Today's Class - Describe what we mean by an API - Explain how APIs will be a huge part of your career as a business analyst and/or data scientist - Use APIs for extracting web data --- class: inverse, center, middle # What is an API? <br> (A Web Server Based Perspective) --- # What is an API? [1] - An **API** is an acronym for application programming interface. - It is a **popular** approach to interact with an application/service or data since it: * Defines a set of functionalities independent of implementation (i.e., it only exposes information that a programmer might find useful and keep those parts consistent even if the implementation changes later) * Provides some level of privacy/control over one's internal data and the rate at which it can be accessed. --- # What is an API? [2] <center> <iframe width="896" height="504" src="https://www.youtube.com/embed/s7wmiS2mSXY" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </center> --- # What is an API? [3] .center[**Scenario:** Alone, you went into a warehouse and are trying to retrieve 3 screwdrivers, a toolbox, and 15 Phillips screws. But you do not know, where those things are in the warehouse.] <div class="figure" style="text-align: center"> <img src="https://miro.medium.com/max/1400/1*oklkpcOX4lpttIAsd2gwXg.jpeg" alt="The API is the set of instructions provided by the warehouse manager on where/how to retrieve this information without touching/accessing other things in there." width="60%" /> <p class="caption">The API is the set of instructions provided by the warehouse manager on where/how to retrieve this information without touching/accessing other things in there.</p> </div> .footnote[ <html> <hr> </html> **Source:** [Matt Z. (2018). What is an API? (explanation with cartoon picture so a 5 year old could understand it)](https://medium.com/@xiang_zhou/what-is-an-api-explanation-with-cartoon-picture-so-a-5-year-old-could-understand-it-6c6c42b75be4) ] --- class: inverse, center, middle # API Useages for Business Analysts and Data Scientists --- # Application #1: Request Data from Web Server This is the **classical** example, which was explained in the previous slides and we will be coding several in-class demos to illustrate how to perform such operations in
. --- # Application #2: Access Python Libraries <iframe src="https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html" frameBorder="0" height="450" width="1000" title="Pandas API Reference" class="container p-0 flex-grow space-iframe"> </iframe> --- # Application #3: Use a Pretrained ML Model <iframe src="https://player.vimeo.com/video/692375454?h=4e0c951c7c&color=ffffff&title=0&byline=0&portrait=0" width="1000" height="450" frameborder="0" allow="autoplay; fullscreen; picture-in-picture" allowfullscreen></iframe> <p>Please click on the <a href="https://vimeo.com/692375454"> "Watch on Vimeo" to see OpenAI's explanation of their DALL·E 2 model</a>.</p> --- class: inverse, center, middle # Accessing APIs in <svg viewBox="0 0 581 512" style="height:1em;position:relative;display:inline-block;top:.1em;fill:white;" xmlns="http://www.w3.org/2000/svg"> <path d="M581 226.6C581 119.1 450.9 32 290.5 32S0 119.1 0 226.6C0 322.4 103.3 402 239.4 418.1V480h99.1v-61.5c24.3-2.7 47.6-7.4 69.4-13.9L448 480h112l-67.4-113.7c54.5-35.4 88.4-84.9 88.4-139.7zm-466.8 14.5c0-73.5 98.9-133 220.8-133s211.9 40.7 211.9 133c0 50.1-26.5 85-70.3 106.4-2.4-1.6-4.7-2.9-6.4-3.7-10.2-5.2-27.8-10.5-27.8-10.5s86.6-6.4 86.6-92.7-90.6-87.9-90.6-87.9h-199V361c-74.1-21.5-125.2-67.1-125.2-119.9zm225.1 38.3v-55.6c57.8 0 87.8-6.8 87.8 27.3 0 36.5-38.2 28.3-87.8 28.3zm-.9 72.5H365c10.8 0 18.9 11.7 24 19.2-16.1 1.9-33 2.8-50.6 2.9v-22.1z"></path></svg> --- # The 3 Step Process Before you dive into the API documentation, you **should first check if there is a R (or Python if you are familiar)** package/library that serves as a wrapper for that API. 1. **Find** the **API's documentation** and find information about the following: A. Does the API require an **authentication key**? B. What are the API's **base URL** and **query parameters**? C. How does the request URL look like? 2. Craft your **request**. My recommendations are to: A. First, start with a simple request. B. Test that request in your browser and see what results you get. 3. Construct that request in
by **either**: A. If the generated content seems to be a `JSON` file/webpage, you can capitalize on the reading the content from `jsonlite::fromJSON()`; **OR** B. By passing the `base url` inside the `httr::GET()` and parsing the results with `httr::content()`. --- # Demo 1: Accuweather API .panelset[ .panel[.panel-name[Demo Description] > - Go to <https://developer.accuweather.com/> and create an account. > - Add your first app (from the `MY APPS` tab) and copy the generated API key. > - Then using the API Reference Tab
Locations API
City Search
find the location key for `Oxford, Ohio` > - Use this information in the `Forecast API` to obtain the `5 Day Forecasts` for `Oxford Ohio`. ] .panel[.panel-name[Code and Results] ``` ## List of 2 ## $ Headline :List of 9 ## ..$ EffectiveDate : chr "2024-09-23T08:00:00-04:00" ## ..$ EffectiveEpochDate: int 1727092800 ## ..$ Severity : int 3 ## ..$ Text : chr "Showers and thunderstorms around this morning through tomorrow afternoon" ## ..$ Category : chr "thunderstorm" ## ..$ EndDate : chr "2024-09-24T20:00:00-04:00" ## ..$ EndEpochDate : int 1727222400 ## ..$ MobileLink : chr "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?lang=en-us" ## ..$ Link : chr "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?lang=en-us" ## $ DailyForecasts:'data.frame': 5 obs. of 8 variables: ## ..$ Date : chr [1:5] "2024-09-23T07:00:00-04:00" "2024-09-24T07:00:00-04:00" "2024-09-25T07:00:00-04:00" "2024-09-26T07:00:00-04:00" ... ## ..$ EpochDate : int [1:5] 1727089200 1727175600 1727262000 1727348400 1727434800 ## ..$ Temperature:'data.frame': 5 obs. of 2 variables: ## .. ..$ Minimum:'data.frame': 5 obs. of 3 variables: ## .. ..$ Maximum:'data.frame': 5 obs. of 3 variables: ## ..$ Day :'data.frame': 5 obs. of 5 variables: ## .. ..$ Icon : int [1:5] 12 15 7 18 4 ## .. ..$ IconPhrase : chr [1:5] "Showers" "Thunderstorms" "Cloudy" "Rain" ... ## .. ..$ HasPrecipitation : logi [1:5] TRUE TRUE TRUE TRUE FALSE ## .. ..$ PrecipitationType : chr [1:5] "Rain" "Rain" "Rain" "Rain" ... ## .. ..$ PrecipitationIntensity: chr [1:5] "Light" "Heavy" "Light" "Light" ... ## ..$ Night :'data.frame': 5 obs. of 5 variables: ## .. ..$ Icon : int [1:5] 12 38 40 35 39 ## .. ..$ IconPhrase : chr [1:5] "Showers" "Mostly cloudy" "Mostly cloudy w/ showers" "Partly cloudy" ... ## .. ..$ HasPrecipitation : logi [1:5] TRUE FALSE TRUE FALSE TRUE ## .. ..$ PrecipitationType : chr [1:5] "Rain" NA "Rain" NA ... ## .. ..$ PrecipitationIntensity: chr [1:5] "Light" NA "Light" NA ... ## ..$ Sources :List of 5 ## .. ..$ : chr "AccuWeather" ## .. ..$ : chr "AccuWeather" ## .. ..$ : chr "AccuWeather" ## .. ..$ : chr "AccuWeather" ## .. ..$ : chr "AccuWeather" ## ..$ MobileLink : chr [1:5] "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=1&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=2&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=3&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=4&lang=en-us" ... ## ..$ Link : chr [1:5] "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=1&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=2&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=3&lang=en-us" "http://www.accuweather.com/en/us/oxford-oh/45056/daily-weather-forecast/340019?day=4&lang=en-us" ... ``` ] ] --- # Demo 2: The CryptoCompare API .panelset[ .panel[.panel-name[Demo Description] .small[ - Create a Personal (Free) account at [CryptoCompare.com](https://min-api.cryptocompare.com/pricing) - Click on create your free key to create your API key and copy the key. - Go to the [documentation](https://min-api.cryptocompare.com/documentation), and test their sample call by executing the call after you have pasted your API key in the call. - The executed call returns the price of BTC (Bitcoin) in USD, JPY and EUR. - Now click on the Historical Data Tab on the left - Click on Daily Pair OHLCV and Execute the Sample Call for BTC - This returns 10 days worth of OHLCV for BTC in USD. - **Let us obtain the price for $JASMY over the past 100 days.** ] ] .panel[.panel-name[Code and Results] ``` ## time high low open close ## 96 2024-09-18 61794.54 59185.20 60321.59 61775.68 ## 97 2024-09-19 63881.87 61582.28 61775.68 62963.41 ## 98 2024-09-20 64131.22 62345.57 62963.41 63211.47 ## 99 2024-09-21 63548.08 62767.25 63211.47 63363.17 ## 100 2024-09-22 64015.58 62394.47 63363.17 63587.45 ## 101 2024-09-23 64749.32 62584.50 63587.45 63311.29 ``` ] ] --- class: inverse, center, middle # Recap --- # Summary of Main Points By now, you should be able to do the following: - Describe what we mean by an API - Explain how APIs will be a huge part of your career as a business analyst and/or data scientist - Use APIs for extracting web data --- # Things to Do to Prepare for Next Class * [Getting Started with httr](https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html) * [Managing secrets](https://httr.r-lib.org/articles/secrets.html)