Set request headers
Many APIs require users provide an API key, obtained by registering for the service. Keys typically are passed in the request header, rather than as parameters.
The Yelp API documentation says "To authenticate API calls with the API Key, set the Authorization
HTTP header value as Bearer api_key
."
You'll set up a dictionary to pass this information to get()
, call the API for the highest-rated cafes in NYC, and parse the response.
pandas
(as pd
) and requests
have been loaded. The API endpoint is stored as api_url
, and the key is api_key
. Parameters are in the dictionary params
.
This exercise is part of the course
Streamlined Data Ingestion with pandas
Exercise instructions
- Create a dictionary,
headers
, that passes the formatted key string to the"Authorization"
header value. - Query the Yelp API (
api_url
) withget()
and the necessary headers and parameters. Save the result asresponse
. - Extract the JSON data from
response
. Save the result asdata
. - Load the
"businesses"
values indata
to the dataframecafes
and print thenames
column.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create dictionary that passes Authorization and key string
headers = {____: "Bearer {}".format(____)}
# Query the Yelp API with headers and params set
response = ____
# Extract JSON data from response
data = ____
# Load "businesses" values to a dataframe and print names
cafes = ____
print(cafes.name)