Lab 6: Web APIs
Section 1: Background ฅ^•ﻌ•^ฅ
Tim’s cat Nim is near and dear to his heart. In his pursuit to be the best owner there ever was, he will stop at nothing. Learning cat facts? Checking the weather to make sure the temperature is just right? Real time stock data? Searching French addresses? Anything. As his trusty student, you can help him be a purr-fect cat owner.
Section 2: Working with Web APIs
Part 1: Make a Simple Request
Now, let’s practice making some requests and retrieving information from urls. In Python, the requests library has all of the functionality you’ll need. Let’s start by installing the requests library - in your terminal run:
$ pip3 install requests
Now we’re going to try and make a request to a cat facts website and get a cat fact. The cat facts website returns a JSON with a fact in it. To see the format of the json, feel free to copy and paste the url into a web browser. The cat facts url we are using is https://catfact.ninja/fact, and it does not take in any parameters.
Task 1: Make a get request to the cat facts URL and print out the fact!
Hint: We want only the fact to be printed out — don’t just print the JSON! Remember to access and print just the fact, refer to the slides for more information on accessing elements in a JSON.
What is a JSON?
JavaScript Object Notation (JSON) is a standard syntax for exchanging this sort of structured information. JSON syntax is very similar to Python dictionary syntax. Information is stored as key-value pairs of various types. Like in Python, a JSON object could have a string key whose value is a string, a list or a whole other dictionary.Part 2: National Weather Service
Now that Tim has a fact about cat, he wants to know the weather in Providence to know if it’s a good day to take Nim out for a walk. The National Weather Service (NWS) provides a free API that we can use to get weather information. Professional APIs tend to be well-documented, which makes it a good example. We’ll also use the NWS because, while many professional APIs require registration and the use of an API key to use them, the NWS API is free and requires no registration.
Task 2: Navigate to the NWS API documentation and read through the documentation to understand how to use the API. Check the Examples tab to see how to make a request.
Let’s get weather information for Providence. Our geocordinates here are: 41.8268, -71.4029
. According to the docs, we can start by sending a points query with these coordinates https://api.weather.gov/points/41.8268,-71.4029
.
Extracting Meaning See the documentation to understand the meaning of specific fields in the response. At a high level, this query tells us which NWS grid location Providence is in, along with telling us URLs for common queries about that grid location. The NWS API needs you to work with it in stages.
Task 3: Extract the forecast URL and make a request. Print out the temperature for this afternoon and tonight.
Part 3: Exploration of API
For this section, we want you to explore a different API. Here is a link to some public APIs to choose from. The only requirements are that you use a different endpoint.
Task 4: Print out something interesting about your selected endpoint.
Section 3: You’re All Set! /ᐠ˵- ˕ - ˵マ ᶻ 𝗓 𐰁
Success! Now you too can be an amazing cat owner (and also use APIs). If you have any lingering confusion, please be sure to ask a TA as you will be working with APIs in Project 3. That’s all for lab this week!