Python RapidAPI
APIs can be found all over the internet. A great consolidator of many APIs is RapidAPI. In this blog we will use a site to consolidates API stats. Learning a few lines of code and you can start extracting lots of data from the internet via APIs.
Quote API
To begin the API journey. You need to find an API provider.
- I used a motivational quote API Motivation Quote Data- This API pulls random motivational quotes from a website -It will prompt you to rate the quote from 1-10 each time it generates a quote
import requests
url = "https://motivational-quotes1.p.rapidapi.com/motivation"
payload = {
"key1": "value",
"key2": "value"
}
headers = {
"content-type": "application/json",
"X-RapidAPI-Key": "97cada0567msh30f120b09c87306p148cacjsn5a7d8d05dc40",
"X-RapidAPI-Host": "motivational-quotes1.p.rapidapi.com"
}
response = requests.request("POST", url, json=payload, headers=headers)
rating =int(input("Please rate the quote on a scale from 1 to 10"))
if rating <= 10 and rating >= 1:
print("You rated this quote", rating, "out of 10")
else:
print("Please have a rating between 1 and 10")
print(response.text)
Go deeper into APIs
Web Development vs Jupyter Notebook. A notebook is certainly a great place to start. But, for your end of Trimester project we want you to build the skill to reference and use APIs within your Project. Here are some resources to get you started with this journey.
- In the Nighthawk Coders APCSP you can find an Overview and Examples using APIs:APCSP APIs menu- Using Covid RapidAPI
- JavaScript frontend API code in APCSP Fastpages GitHub repo: https://github.com/nighthawkcoders/APCSP/blob/master/_posts/2022-07-10-PBL-rapidapi.md
- Making a Jokes API (this will next API tech talk)
- Frontend. JavaScript frontend code in APCSP fastpages GitHub repo: https://github.com/nighthawkcoders/APCSP/blob/master/_posts/2022-07-10-PBL-jokes.md
- Backend Endpoints. Python code that allows Frontend access: https://github.com/nighthawkcoders/flask_portfolio/blob/main/api.py
- Backend Jokes Management. Python code that support Create, Read, Update, Delete (CRUD): https://github.com/nighthawkcoders/flask_portfolio/blob/main/model_jokes.py
Hacks
Find and use an API as part of your project. An API and a little coding logic will be a big step toward getting meaningful data for a project. There are many API providers, find one that might work for your project to complete this hack. When picking an API you are looking for something that will work with either JavaScript Fetch or Python Request. Here are some samples, these are not qualified in any way.
Show API and format results in either Web Page or Jupyter Notebook. Ultimately, I will expect that we do APIs in backend (Python/Flask). However, for this Hack you can pick your preference. We will discuss pros and cons in next API tech talk.