Python RapidAPI
This notebook is about how to use API's and an introduction to them including what they are
- Python, RapidAPI Terms
- Covid19 RapidAPI Example
- Digital Coin Example
- Formatting Digital Coin example
- My API
Covid19 RapidAPI Example
To begin the API journey. You need to find an API provider.
- RapidAPI is a great option. You must setup and account, but there are many free options.
- Goto this page for starters, the Corona virus World and India data- Under Code Snippets pick Python - Requests
RapidAPI, you will select Python Requests type of code to work with you Notebook.
- The url is the endpoint to which the API is directed
- The headers is a dictionary data structure to send special messaging to the endpoint
- The requests.request() python function is used to send a request and retrieve their responses
- The response variable receives result of of the request in JSON text
Next step, is to format the response according to your data science needs
"""
Requests is a HTTP library for the Python programming language.
The goal of the project is to make HTTP requests simpler and more human-friendly.
"""
import requests
"""
RapidAPI is the world's largest API Marketplace.
Developers use Rapid API to discover and connect to thousands of APIs.
"""
url = "https://corona-virus-world-and-india-data.p.rapidapi.com/api"
headers = {
'x-rapidapi-key': "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf",
'x-rapidapi-host': "corona-virus-world-and-india-data.p.rapidapi.com"
}
# Request Covid Data
response = requests.request("GET", url, headers=headers)
# print(response.text) # uncomment this line to see raw data
#print(response.json())
# This code looks for "world data"
print("World Totals")
world = response.json().get('world_total') # turn response to json() so we can extract "world_total"
for key, value in world.items(): # this finds key, value pairs in country
print(key, value)
print()
# This code looks for USA in "countries_stats"
print("Country Totals")
countries = response.json().get('countries_stat')
for country in countries: # countries is a list
if country["country_name"] == "USA": # this filters for USA
for key, value in country.items(): # this finds key, value pairs in country
print(key, value)
# RapidAPI page https://rapidapi.com/Coinranking/api/coinranking1/
# Begin Rapid API Code
import requests
url = "https://coinranking1.p.rapidapi.com/coins"
querystring = {"referenceCurrencyUuid":"yhjMzLPhuIDl","timePeriod":"24h","tiers[0]":"1","orderBy":"marketCap","orderDirection":"desc","limit":"50","offset":"0"}
headers = {
"X-RapidAPI-Key": "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf", # place your key here
"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers, params=querystring)
#print(response.text),code that was here, and prints data but not organized
# End Rapid API Code
json = response.json() # convert response to python json object
# Observe data from an API. This is how data transports over the internet in a "JSON" text form
# - The JSON "text" is formed in dictionary {} and list [] divisions
# - To read the result, Data Scientist of Developer converts JSON into human readable form
# - Review the first line, look for the keys -- "status" and "data"
for coin in json["data"]["coins"]:
print(f'{coin["symbol"]} {coin["name"]} {coin["price"]}')
Formatting Digital Coin example
JSON text transferred from the API in the previous cell was converted to a Python Dictionary called json. The "coins" in the dictionary contain a list of the most relevant data. Look at the code and comments to see how the original text is turned into something understandable. Additionally, there are error check to make sure we are starting the code with the expectation that the API was run correctly.
"""
This cell is dependent on valid run of API above.
- try and except code is making sure "json" was properly run above
- inside second try is code that is used to process Coin API data
Note. Run this cell repeatedly to format data without re-activating API
"""
try:
print("JSON data is Python type: " + str(type(json)))
try:
# Extracting Coins JSON status, if the API worked
status = json.get('status')
print("API status: " + status)
print()
# Extracting Coins JSON data, data about the coins
data = json.get('data')
# Procedural abstraction of Print code for coins
def print_coin(c):
print(c["symbol"], c["price"])
print("Icon Url: " + c["iconUrl"])
print("Rank Url: " + c["coinrankingUrl"])
# Coins data was observed to be a list
for coin in data['coins']:
print_coin(coin)
print()
except:
print("Did you insert a valid key in X-RapidAPI-Key of API cell above?")
print(json)
except:
print("This cell is dependent on running API call in cell above!")
import requests
url = "https://public-holiday.p.rapidapi.com/2019/US"
headers = {
"X-RapidAPI-Key": "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf",
"X-RapidAPI-Host": "public-holiday.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
print(response.text)
import requests
url = "https://public-holiday.p.rapidapi.com/2019/US"
headers = {
"X-RapidAPI-Key": "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf",
"X-RapidAPI-Host": "public-holiday.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
#print(response.text)
#holidays = response.json().get('Public Holidays')
#for date in holidays: # Refer to list
#if date["2019-01-01"] == "2019-01-01": # Supposed to not include this date
#for key, value in date.items(): # Find specific date in API
#print(key, value)
#ids = response.json().get('data')
#for id in ids:
#if id["_id"] == "61d8eefe8d2a7580b2f9bebe":
#for key, value in date.items():
#print(key, value)
#items = response.json().get('data')
#for date in items: # the specific item in the list
#if date["date"] == "2019-01-01": # this filters
#for key, value in date.items(): # looks for the key and value and outputs it
#print(key, value)
print("Holidays")
import requests
# https://rapidapi.com/fyhao/api/currency-exchange/
# The link for the API I used
url = "https://currency-exchange.p.rapidapi.com/listquotes"
headers = {
"X-RapidAPI-Key": "2348020badmshecfd384514830c4p1f8a42jsn72e4bf449cdf",
"X-RapidAPI-Host": "currency-exchange.p.rapidapi.com"
}
response = requests.request("GET", url, headers=headers)
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.
- RapidAPI- Google Developer I used an API from RapidAPI and this helped me and I used Currencies and it listed it Show API and format results in either Web Page or Jupyter Notebook. Ultimately, I will expect that we do APIs in backend (Python/Flask).