Search Spotify Resources
Learn how to search for information about an artist, playlist, album, show, or an episode.
Overview
Now that we have the authorization token, we can start exploring the endpoints of the Spotify API. The first endpoint in our list is the Search for Item endpoint.
This endpoint provides us the option to search for information about any artist, album, track, show, or episode. The base URI used for this endpoint is https://api.spotify.com/v1/search
. It returns us the information available on Spotify. This endpoint provides the Spotify IDs that can be used with other endpoints requiring a Spotify ID.
Request parameters
The query parameters for the Search for Item endpoint are given in the table below:
Query parameter | Category | Type | Description |
| Required | String | This is set to what we want to search using this endpoint. |
| Required | String | This is the category we want to search in. The possible options are: |
| Optional | String | This decides whether the user can play externally hosted audio or not. Its possible option is |
| Optional | Integer | This limits the number of items to be returned in the response. Its value ranges from |
| Optional | String | This is an ISO 3166-1 alpha-2 country code that we can use to fetch only the content available in a specific country. Some examples of country codes are |
| Optional | Integer | This parameter can be used to offset the first item we get in response. |
Search for an artist using client credentials access token
The items we get in response differ based on whether we use the client credentials access token or authorization code access token for this endpoint. The code below calls this endpoint with the client credentials access token. Click the "Run" button to see the response.
Note: If the client credential access token has expired, refer to this lesson in the "Appendix" chapter to refresh the access token.
url = ('https://api.spotify.com/v1/search?''q=metallica&''type=artist')headers = {'Content-Type': 'application/json','Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'}response = requests.request("GET", url, headers=headers)print(json.dumps(response.json(), indent=4))
- Lines 1–3: We define the URL.
- Lines 5–8: We define the header.
- Line 10: We make the API call and save the JSON response in the
response
variable. - Line 11: We print the
response
.
Note: The code structure will remain the same for all other endpoints. We only need to change the URLs and the request type (for some endpoints).
Search for an artist using an authorization code access token
Let's replace the client credentials access token in line 7 of our code with the authorization code access token to see how changing the tokens affects the response.
url = ('https://api.spotify.com/v1/search?''q=jake&''type=artist')headers = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}response = requests.request("GET", url, headers=headers)print(json.dumps(response.json(), indent=4))
Note: If your authorization code access token has expired, you can refresh the access token using the code in this lesson of the "Appendix" chapter.
Notice that this time the output result is different from the previous executable. Because now, the results are filtered based on the country associated with our profile. So the authorization code access token can also affect the results of some public endpoints.
Response fields
Some important response fields from the code above are given below:
Response field | Type | Description |
| String | This array contains the genres the artist has performed in. |
| String | This is the name of the artist. |
| Integer | This index shows how popular an artist is on Spotify. It ranges from |
| Array of objects | This contains the information for the artist's images. Each object in this array contains three elements which give us the URL, height, and width of the image respectively. |
| String | This is the Spotify ID of the artist. |
| String | This is the link to the next page. |
| String | This is the link to the previous page. |
| Integer | This tells us the total number of albums that we get in response. |
Try changing the type
(in line 3) and notice the change in the response fields.