31  APIs — Workbook

Author

Melanie Walsh

In this notebook, we will explore how to make simple API requests.

import requests
import pandas as pd
pd.options.display.max_rows = 100
pd.options.display.max_colwidth = 100

31.1 Open Brewery Database

https://api.openbrewerydb.org

Open Brewery Database API Endpoints:
- List Breweries
- Get Brewery
- Search Breweries
- Autocomplete

31.2 List Breweries

https://api.openbrewerydb.org/breweries

response = requests.get(f"https://api.openbrewerydb.org/breweries")
response.json()
json = response.json()
pd.json_normalize(json)

31.3 List Breweries Paramaters

  • by_city
  • by_name
  • by_state
  • by_postal
  • by_type
  • page
  • per_page
  • sort

31.3.1 By City

https://api.openbrewerydb.org/breweries?by_city=Ithaca

city = 'Ithaca'
response = requests.get(f"https://api.openbrewerydb.org/breweries?by_city={city}")
json = response.json()
pd.json_normalize(json)

31.4 By Type

https://api.openbrewerydb.org/breweries?by_city=Ithaca&by_type=micro

city = 'Ithaca'
brew_type = 'micro'
response = requests.get(f"https://api.openbrewerydb.org/breweries?by_city={city}&by_type={brew_type}")
json = response.json()
pd.json_normalize(json)

31.5 Search Breweries

https://api.openbrewerydb.org/breweries/search?query=dog

query = 'four'
response = requests.get(f"https://api.openbrewerydb.org/breweries/search?query={query}")
json = response.json()
pd.json_normalize(json)

31.5.1 Your Turn!

Search for your own keyword by changing the query variable above! Or create your own API request entirely.

Discuss: What kind of projects or research questions do you think you (or someone else) could do with data from the Open Brewery Database API?

31.6 Open Library API

https://openlibrary.org/developers/api

Open Library API Endpoints - Books - Covers - Lists - Read - Recent Changes - Search - Search inside - Subjects