requests
¶When making requests for data relating to a particular track or album, the URL itself (with the {id}
at the end of the endpoint) is sufficient to specify the data we want returned:
https://api.spotify.com/v1/albums/1To7kv722A8SpZF789MZy7
However, we often need or want to be more specific, for example:
APIs tend to be structed to handle such requests through the use of query parameters.
First, fork the repl.
!!! warning "Access Token"
Remember that you'll need to enter your own valid `access_token`, which you can generate by forking [this repl](https://repl.it/@datadesigns/seeder-requests-auth#main.py) and adding your own Spotify API credentials to a `.env` file.
Run the repl, copy your `access_token` from the console, and and paste it into `main.py` in the repl for the walkthrough.
params
keyword argument¶Having covered authorization and endpoints in the MixTape tutorial, we'll skip straight to the relevant code for query parameters:
params = {'q': 'waterfall', 'type': 'track'}
response = requests.get(search_url, headers=headers, params=params)
parameter:value
pairs to params
params
dictionary as the params
argument in the GET
requestAfter running the repl, you can try out the following code snippets in the console.
data = response.json()
type(data)
dict
list(data.keys())
['tracks']
type
wasn't a required query parameter and so the returned dataset could also have contained albums, artists, etclist(data['tracks'].keys())
['href', 'items', 'limit', 'next', 'offset', 'previous', 'total']
items
value is a list of dictionaries, each containing data about a given trackWe have to go deep into the JSON structure to find our individual track data:
print(list(data['tracks']['items'][0].keys()))
['album', 'artists', 'available_markets', 'disc_number', 'duration_ms', 'explicit', 'external_ids', 'external_urls', 'href', 'id', 'is_local', 'name', 'popularity', 'preview_url', 'track_number', 'type', 'uri']
len(data['tracks']['items'])
20
items
, but there are many more tracks which were identified by our search...APIs will not typically return all items if there are a large number of matches; instead, further calls to the API will be required. You may also encounter limits on the frequency or volume of requests to an API or particular endpoint.
print(waterfall_tracks['tracks']['total'])
print(waterfall_tracks['tracks']['limit'])
print(waterfall_tracks['tracks']['next'])
97307 20 https://api.spotify.com/v1/search?query=waterfall&type=track&offset=20&limit=20
Spend some time using requests.get()
to fetch data from endpoints listed under Albums
, Artists
, Browse
, Search
, and Tracks
in the documentation; all of these can be done using the Client Credentials access_token
you created earlier.
See if you can create some reuable functions, to do things such as:
access_token
and a track id
as argumentsaccess_token
and a string of keywords as argumentsFor now, don't worry about automatically renewing the access_token
or diving too deep into the nested data - we'll be looking at that later on.