Python, RapidAPI Terms

APIs and tooling like Jupyter docs allows many opportunities in fields like Data Science. As more and more developers use APIs, they build standards in how you setup a client, send requests and receive information...

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': "26d9a3c8fbmshd1c8fc32ca8acc3p190a69jsn54f737b8e33b",
    '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
# 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)
World Totals
total_cases 509,268,964
new_cases 204,268
total_deaths 6,242,509
new_deaths 630
total_recovered 461,827,849
active_cases 41,198,606
serious_critical 42,510
total_cases_per_1m_population 65,334
deaths_per_1m_population 800.9
statistic_taken_at 2022-04-24 11:18:01

Country Totals
country_name USA
cases 82,649,779
deaths 1,018,316
region 
total_recovered 80,434,925
new_deaths 0
new_cases 0
serious_critical 1,465
active_cases 1,196,538
total_cases_per_1m_population 247,080
deaths_per_1m_population 3,044
total_tests 1,000,275,726
tests_per_1m_population 2,990,303

Digital Coin Example

This example provides digital coin feedback (ie Bitcoin). It include popularity, price, symbols, etc.

  • A valid X-RapidAPI-Key is required. Look in code for link to RapidAPI page
  • Read all comments in code for further guidance
# 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": "jcmbea0fa2ff5msh7f14bf69be38ca6p175482jsn6c4988114560",  # place your key here
	"X-RapidAPI-Host": "coinranking1.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)
# 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"
{"message":"You are not subscribed to this API."}

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!")
JSON data is Python type: <class 'dict'>
Did you insert a valid key in X-RapidAPI-Key of API cell above?
{'message': 'You are not subscribed to this API.'}

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.

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.

My Personal API (Weather)

from curses.ascii import US
from parso import split_lines
import requests

url = "https://weatherbit-v1-mashape.p.rapidapi.com/forecast/3hourly"

querystring = {"lat":"35.5","lon":"-78.5"}

headers = {
	"X-RapidAPI-Key": "26d9a3c8fbmshd1c8fc32ca8acc3p190a69jsn54f737b8e33b",
	"X-RapidAPI-Host": "weatherbit-v1-mashape.p.rapidapi.com"
}

response = requests.request("GET", url, headers=headers, params=querystring)

# print(response.text)

print("Weather")
weathers = response.json().get('data')
for weather in weathers:  # weathers is a list
    if weather["wind_cdir"] == "N":  # this filters for N
        for key, value in weather.items():  # this finds key, value pairs in country
            print(key, value)
Weather
wind_cdir N
rh 51
pod d
pres 1014
clouds 25
vis 36.896
wind_spd 3.88
snow_depth 0
wind_cdir_full north
slp 1022
datetime 2022-10-08:18
ts 1665252000
dewpt 9.5
uv 4.7
wind_dir 4
ghi 739.78
dhi 112.16
ozone 286.8
clouds_hi 100
weather {'description': 'Scattered clouds', 'code': 802, 'icon': 'c02d'}
clouds_low 11
temp 19.9
app_temp 19.3
timestamp_utc 2022-10-08T18:00:00
timestamp_local 2022-10-08T14:00:00
snow 0
wind_gust_spd 4.71
solar_rad 733.805
precip 0
pop 0
dni 882.97
clouds_mid 0
wind_cdir N
rh 90
pod n
pres 1015
clouds 100
vis 3.8
wind_spd 1.88
snow_depth 0
wind_cdir_full north
slp 1022
datetime 2022-10-10:09
ts 1665392400
dewpt 13.2
uv 0
wind_dir 10
ghi 0
dhi 0
ozone 287.3
clouds_hi 13
weather {'description': 'Light shower rain', 'code': 520, 'icon': 'r04n'}
clouds_low 100
temp 14.8
app_temp 14.8
timestamp_utc 2022-10-10T09:00:00
timestamp_local 2022-10-10T05:00:00
snow 0
wind_gust_spd 3.32
solar_rad 0
precip 2.8787613
pop 80
dni 0
clouds_mid 100

Burgers and Fries Example

import sys
total = 0 
menu =  {"burger": 10.99, "fries": 4.99, "drink": 4.99}

print("Menu")
for k,v in menu.items():
    print(k + "  $" + str(v)) 

ordering = True

while ordering:
    food = input("Please select an item from the menu. Press enter when you are done. Press exit to quit.")
    
    if food == "exit":
        break 

    if food in menu:
        total += menu[food]
        print("Your total: ""$" + "{:.2f}".format(total))
    else:
        print("Here is your order!")
        ordering = False

print("done")
Menu
burger  $10.99
fries  $4.99
drink  $4.99
done