class: center, middle, inverse, title-slide .title[ # ISA 401: Business Intelligence & Data Visualization ] .subtitle[ ## 08: Connecting to APIs in
] .author[ ###
Fadel M. Megahed, PhD
Raymond E. Glos Professor in Business
Farmer School of Business
Miami University
@FadelMegahed
fmegahed
fmegahed@miamioh.edu
Automated Scheduler for Office Hours
] .date[ ### Fall 2025 ] --- # 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 # 2 To assess your understanding and retention of the topics covered last week, you will **compete in a Kahoot competition (consisting of 9 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: 0.25 on their Web Scraping II Assignment. Good luck!!! .footnote[ <html> <hr> </html> ] --- # 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: 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 $BTC over the past 100 days.** ] ] .panel[.panel-name[Code and Results] ``` ## time high low open close ## 96 2025-09-13 116361.1 115207.4 116116.3 115971.9 ## 97 2025-09-14 116227.4 115203.8 115971.9 115351.9 ## 98 2025-09-15 116808.1 114427.7 115351.9 115401.1 ## 99 2025-09-16 117006.6 114765.7 115401.1 116838.3 ## 100 2025-09-17 117331.1 114742.8 116838.3 116488.2 ## 101 2025-09-18 117957.6 116134.8 116488.2 117632.0 ``` ] ] --- 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) * [Getting Started with httr2](https://httr2.r-lib.org/articles/httr2.html) * [Managing secrets](https://httr.r-lib.org/articles/secrets.html)