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": "982d09f4b6msha36abf60bba5931p11f8b1jsn17c33bd8dbf4",
	"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)
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":"asc","limit":"50","offset":"0"}

headers = {
	"X-RapidAPI-Key": "982d09f4b6msha36abf60bba5931p11f8b1jsn17c33bd8dbf4",
	"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
#print(response.json())
# 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"

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'>
API status: success

FXTC 0.0779200097827158
Icon Url: https://cdn.coinranking.com/VCcpsxMOb/fxtc-pools.svg
Rank Url: https://coinranking.com/coin/JJEVdobzFfNn+fxtcpools-fxtc

BOOT 0.006634674736075586
Icon Url: https://cdn.coinranking.com/WQzZCePhf/BOOT_1563330941007_light.png
Rank Url: https://coinranking.com/coin/cuPvJZZsffVr3+bitcoinrebooted-boot

BC 0.013626999507315067
Icon Url: https://cdn.coinranking.com/ZNnDh97xw/bc.png
Rank Url: https://coinranking.com/coin/00143FdsSOmd+bitcoinconfidential-bc

CCA 0.00015015368848074
Icon Url: https://cdn.coinranking.com/HY9czzwx_/4122.png
Rank Url: https://coinranking.com/coin/CNm61Nt1_ih_+counoscoin-cca

SVR 0.005450799802926027
Icon Url: https://cdn.coinranking.com/KCxC1pJB3/4594.png
Rank Url: https://coinranking.com/coin/9I0C0CCwF_Rd+sovranocoin-svr

MCN 0.000194671421533072
Icon Url: https://cdn.coinranking.com/BImEmxkuc/423.png
Rank Url: https://coinranking.com/coin/58uJvk3vOkGf+monetaverde-mcn

HOSP 0.003893428430661448
Icon Url: https://cdn.coinranking.com/o512Nw0-e/Hospital_Coin.png
Rank Url: https://coinranking.com/coin/rL5brTWKayJU+hospitalcoin-hosp

PEOS 0.000055204707588876
Icon Url: https://cdn.coinranking.com/-Q7fMD4gj/3910.png
Rank Url: https://coinranking.com/coin/OtjXUM9ymkdI+peos-peos

OPN 0.002920071322996086
Icon Url: https://cdn.coinranking.com/QZlPEiCWt/opn.png
Rank Url: https://coinranking.com/coin/NXE_WTIH4Diy+openbit-opn

YOB 0.005450799802926027
Icon Url: https://cdn.coinranking.com/Q6hQcT2s4/yobank.png
Rank Url: https://coinranking.com/coin/br5t9N75S_XQ+yobank-yob

XGK 5.637878338e-7
Icon Url: https://cdn.coinranking.com/HgleTy8uZ/F8pONNqi_400x400.png
Rank Url: https://coinranking.com/coin/WuSl3eeO2Jt0+goldkash-xgk

ABET 0.001406212522500279
Icon Url: https://cdn.coinranking.com/TVxx6o4vv/4502.png
Rank Url: https://coinranking.com/coin/tsYwLMUHlEtr+altbet-abet

INNBC 2.0021489258e-8
Icon Url: https://cdn.coinranking.com/3oSm7vNHl/innovative-bioresearch-coin.svg
Rank Url: https://coinranking.com/coin/kDDoiqG4W6s+innovativebioresearchcoin-innbc

TNR 1.4339925769e-8
Icon Url: https://cdn.coinranking.com/y61K2cKYf/tnr.png
Rank Url: https://coinranking.com/coin/2lXwQqlHiVe3+tonestra-tnr

NOR 0.005256128381392954
Icon Url: https://cdn.coinranking.com/xEnOrtfZ8Vc/noir.svg
Rank Url: https://coinranking.com/coin/cdhFXkXV3mRP0+noir-nor

GLT 0.000175204279379765
Icon Url: https://cdn.coinranking.com/jInwKn4GcYi/globaltoken.svg
Rank Url: https://coinranking.com/coin/kpHamxaeamGVC+globaltoken-glt

HATCH 0.00200241757081142
Icon Url: https://cdn.coinranking.com/txhGRJgGU/4185.png
Rank Url: https://coinranking.com/coin/DKNkOIfAiPJB+hatch-hatch

COW 0.003737427441862211
Icon Url: https://cdn.coinranking.com/HPlXvsAcg/5895.png
Rank Url: https://coinranking.com/coin/kVSbvjZ8Mlit+cowry-cow

EKO 0.000072028617323584
Icon Url: https://cdn.coinranking.com/S1l3RA1zX/eko.svg
Rank Url: https://coinranking.com/coin/QmaGeC2kxWNzc+echolink-eko

AUDAX 0.00021413856368638
Icon Url: https://cdn.coinranking.com/gdHwdnTfV/logo-284x284.png
Rank Url: https://coinranking.com/coin/fYNqTyYGQ+audax-audax

RISE 0.000502561667010731
Icon Url: https://cdn.coinranking.com/ryJ6Xr9NM/rise.svg
Rank Url: https://coinranking.com/coin/p7VD1xOWgroAy+rise-rise

YAP 0.001912308780124906
Icon Url: https://cdn.coinranking.com/F0TfmGvWF/yap.png
Rank Url: https://coinranking.com/coin/k828ut4b+yapstone-yap

GUN 0.000042684172199619
Icon Url: https://cdn.coinranking.com/BJjQnHg67/gun.svg
Rank Url: https://coinranking.com/coin/_rfn6Am9rkEhy+guncoin-gun

XSH 0.000136275913583028
Icon Url: https://cdn.coinranking.com/B148rB9Vz/xsh.svg
Rank Url: https://coinranking.com/coin/d6UohiNz99VG_+shield-xsh

EXCL 0.011096752963189438
Icon Url: https://cdn.coinranking.com/qg0ee-keMm/exclusive-coin.svg
Rank Url: https://coinranking.com/coin/ugHHr58GbHNgR+exclusivecoin-excl

GRIMM 0.0700104601574839
Icon Url: https://cdn.coinranking.com/FRKlvl5RV/4866.png
Rank Url: https://coinranking.com/coin/gAcPTtQT+grimm-grimm

MBC 0.000001551676220874
Icon Url: https://cdn.coinranking.com/tZfbQ2-Yo/3507.png
Rank Url: https://coinranking.com/coin/vzs9mp4qQYw3+microbitcoin-mbc

ZCR 0.003270249011629434
Icon Url: https://cdn.coinranking.com/LMgtIf8rdgu/zcore.svg
Rank Url: https://coinranking.com/coin/rI74W2gx6DuLs+zcore-zcr

ETNXP 5.40724403774e-7
Icon Url: https://cdn.coinranking.com/4E1UMVvqW/5669.png
Rank Url: https://coinranking.com/coin/jxWBqe7rwfzS+electroneropulse-etnxp

USDCLP 1.4676216587401518
Icon Url: https://cdn.coinranking.com/o9RJOVs42/usdclp-waves.png
Rank Url: https://coinranking.com/coin/asCEJv7MI+usdclp-usdclp

MEME 0.06210288061855142
Icon Url: https://cdn.coinranking.com/l4XrqHA7Gf/pepe.svg
Rank Url: https://coinranking.com/coin/SNnn13b3diN6f+pepe-meme

GET 0.000093463455186644
Icon Url: https://cdn.coinranking.com/s_FyvCVUK2z/themis.svg
Rank Url: https://coinranking.com/coin/IPbVhqaRHFqd7+themis-get

XQR 0.003504085587595303
Icon Url: https://cdn.coinranking.com/36Qb3hA8e/3821.png
Rank Url: https://coinranking.com/coin/KdRlEzu26D8Y+qredit-xqr

LTK 4.0058290611e-8
Icon Url: https://cdn.coinranking.com/TbnjE4Wmh/litkoin.png
Rank Url: https://coinranking.com/coin/kRdfEZ63gfQ2+litkoin-ltk

CURE 0.00934463407426479
Icon Url: https://cdn.coinranking.com/N_mfFHsEa/333.png
Rank Url: https://coinranking.com/coin/NMbkyaCf4rWjJ+curecoin-cure

APR 0.000661882833212446
Icon Url: https://cdn.coinranking.com/2eD2WvevrVG/apr-coin.svg
Rank Url: https://coinranking.com/coin/htLu2sg2z0lUU+aprcoin-apr

XGG 0.0299026883154086
Icon Url: https://cdn.coinranking.com/tM0p4PBZV4v/going-gems.svg
Rank Url: https://coinranking.com/coin/9KoB2vsXgpfp+goinggems-xgg

LUNES 0.004404508195435037
Icon Url: https://cdn.coinranking.com/_az8jwbum/Lunes-Symbol-200x200.png
Rank Url: https://coinranking.com/coin/Jwrwwrh_z9s2+lunes-lunes

SPHR 0.007981874938434509
Icon Url: https://cdn.coinranking.com/4kr3S0UafJ2/sphere-coin.svg
Rank Url: https://coinranking.com/coin/8EOiMbVD_r71T+spherecoin-sphr

BTCR 0.010231502830473535
Icon Url: https://cdn.coinranking.com/D1knZEEJul/bitcurrency.svg
Rank Url: https://coinranking.com/coin/tBbVxV9pmmzlA+bitcurrency-btcr

BSTY 0.006708098862218256
Icon Url: https://cdn.coinranking.com/jE3b0Qzpj0J/globalboost.svg
Rank Url: https://coinranking.com/coin/0ltXI_jLXJRuS+globalboost-y-bsty

AFC 0.4035513100157843
Icon Url: https://cdn.coinranking.com/oegp1dZG4um/anti-fraud-chain.svg
Rank Url: https://coinranking.com/coin/CvAIy2j_1a-rJ+anti-fraudchain-afc

WCC 0.019463875485540686
Icon Url: https://cdn.coinranking.com/-J18ej4xe/5233.png
Rank Url: https://coinranking.com/coin/X3E9XWPYw+wincash-wcc

ACM 0.004282735431699251
Icon Url: https://cdn.coinranking.com/5mNWGtDKlr/actinium.svg
Rank Url: https://coinranking.com/coin/CB_utF8r69Aze+actinium-acm

ARQ 0.005450754185799047
Icon Url: https://cdn.coinranking.com/MtjTL5b4m/arqma.svg
Rank Url: https://coinranking.com/coin/rflPPmiBQp1iC+arqma-arq

MONK 0.014076870847099432
Icon Url: https://cdn.coinranking.com/4NcGdg13raj/monkey-project.svg
Rank Url: https://coinranking.com/coin/uvdwgYO8XF-Av+monkeyproject-monk

VRC 0.000778719506188733
Icon Url: https://cdn.coinranking.com/ByFWHPdub/vrc.svg
Rank Url: https://coinranking.com/coin/I9t3toniFVZow+vericoin-vrc

YCE 0.001771509935950959
Icon Url: https://cdn.coinranking.com/YUJbQllWkI/myce.svg
Rank Url: https://coinranking.com/coin/f8y0e39gedW-Y+myce-yce

GPS 0.001557110038843255
Icon Url: https://cdn.coinranking.com/wAr2DzdaP/gps.png
Rank Url: https://coinranking.com/coin/ezR2yeG1X+gpsecosystem-gps

UNN 0.1938927358469401
Icon Url: https://cdn.coinranking.com/LEGFuKk6J/GyWRnJKC_400x400.png
Rank Url: https://coinranking.com/coin/Qal82VrX+uninet-unn

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.