This notebook contains EVEN MORE API examples so you can get an idea of the types of services available. There's a world of API's out there for the taking, and we cannot teach them all to you. We can only teach you how they work in general... the details are 100% up to you!
This uses the cosmin phone number lookup API as found on https://market.mashape.com/explore
This api requires headers to be passed into the get() request. The API key and the requested output of json are sent into the header.
Enter a phone number as input like 3154432911 and then the API will output JSON data consisting of caller ID data and GPS coordinates.
In [1]:
import requests
phone = input("Enter your phone number: ")
params = { 'phone' : phone }
headers={ "X-Mashape-Key": "sNi0LJs3rBmshZL7KQOrRWXZqIsBp1XUjhnjsnYUsE6iKo14Nc",
"Accept": "application/json" }
response = requests.get("https://cosmin-us-phone-number-lookup.p.mashape.com/get.php", params=params, headers=headers )
phone_data = response.json()
phone_data
Out[1]:
This example uses http://fixer.io to get the current currency exchange rates.
In [2]:
import requests
apikey = '159f1a48ad7a3d6f4dbe5d5a71c2135c' # get your own at fixer.io
params = { 'access_key': apikey } # US Dollars
response = requests.get("http://data.fixer.io/api/latest", params=params )
rates = response.json()
rates
Out[2]:
Every computer on the internet has a unique IP Address. This service when given an IP address will return back where that IP Address is located. Pretty handy API which is commonly used with mobile devices to determine approximate location when the GPS is turned off.
In [3]:
import requests
ip = "128.230.182.170"
apikey = 'f9117fcd34312f9083a020af5836e337' # get your own at ipstack.com
params = { 'access_key': apikey } # US Dollars
url = f"http://api.snoopi.io/{ip}"
response = requests.get( url, params=params )
rates = response.json()
rates
Out[3]:
Process some text and more here: http://text-processing.com
In [4]:
# sentiment
message = input("How are you feeling today? ")
url = 'http://text-processing.com/api/sentiment/'
options = { 'text' : message}
response = requests.post(url, data = options)
sentiment = response.json()
print(sentiment)
In [5]:
term = 'Mandatory Fun'
params = { 'term' : term }
response = requests.get('https://itunes.apple.com/search', params = params)
search = response.json()
for r in search['results']:
print(r['trackName'])
Here's an example of the significant earthquakes from the past week. Information on this API can be found here:
http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
In [6]:
response = requests.get('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/significant_week.geojson')
quakes = response.json()
for q in quakes['features']:
print(q['properties']['title'])
The spotify example shows you how to call an API which uses the OAUTH2 prococol. This is a two step process. The first request, you request a token, and in the second request you call the api with that token. Twitter, Facebook, Google, and many other services use this approach.
Typically you will use the client credentials flow, which does not explicitly require the user to consent. https://developer.spotify.com/documentation/general/guides/authorization-guide/
API's that use this approach will issue you a client id and a client secret. The id is always the same but the secret may be changed.
We use that client id and client secret to get an bearer access token. Notice how we pass into the post a named argument auth= which authenticates with the client id/secret.
Next we use the bearer access token to make subsequent calls to the api.
In [ ]:
from base64 import b64encode
# USE YOUR OWN CREDENTIALS THESE ARE EXAMPLES
client_id = "413fe60240a7ad1881bcca301a345"
client_secret = "f6eae3c49a8a9a5c82cb00cfb153"
# Step one, get the access token
payload = { 'grant_type' : 'client_credentials'}
response = requests.post("https://accounts.spotify.com/api/token", auth=(client_id,client_secret),data=payload)
token = response.json()['access_token']
print(f"Access token: {token}")
# Step two and beyond, use the access token to call the api
url = "https://api.spotify.com/v1/tracks/2TpxZ7JUBn3uw46aR7qd6V"
header = {"Authorization" : f"Bearer {token}"}
response = requests.get(url, headers=header)
response.json()