User Saved Tracks and Track Information
Learn how to get information about the tracks saved by the user on Spotify using the Spotify API.
In this lesson, we'll look at how we can get a list of tracks and their details that are saved in a user profile.
We can get information about a user’s taste in music using the Get User’s Saved Tracks endpoint. The base URL https://api.spotify.com/v1/me/tracks
provides a list of tracks saved in the current user profile. We have already used PUT
and DELETE
requests with this endpoint to save and remove tracks from a user profile, respectively. Now, we’ll use the GET
request to fetch the list of tracks saved in a user profile.
Request parameters
The information obtained by calling this endpoint can be filtered using the following query parameters:
Query parameter | Type | Category | Description |
| Integer | Optional | This limits the number of items to be returned in the response. Its value ranges from |
| String | Optional | 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 |
| Integer | Optional | This parameter can be used to offset the first item we get in response. |
The code below demonstrates how to get the saved tracks from a user profile with the authorization access token.
const endpointUrl = new URL('https://api.spotify.com/v1/me/tracks');headerParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}const options = {method: 'GET',headers: headerParameters,};async function fetchUsersTracks() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}fetchUsersTracks();
Let's look at the explanation of our code:
Line 1: We define the URL as
https://api.spotify.com/v1/me/tracks
.Lines 3–6: We add header parameters.
Line 9: We set the request type to
GET
.Lines 13–20: We define a function,
fetchUsersTracks()
, that calls the endpoint and prints the response.
Save the ID of the first track, which is automatically extracted from the response to use it further in this lesson.
Response fields
The table below contains the important fields that we get in response to an API call to this endpoint:
Response field | Type | Description |
| Array | This array contains the information about the album of the track saved in the user's profile. |
| String | This is the name of the track. |
| String | This is the link to the Spotify page of the item. |
| String | This shows how popular a track is. It ranges from |
| String | This is the URL for a 30 seconds preview of the track. |
| Array | This array contains the information about the artists of the track saved in the user's profile. |
| String | This is the Spotify ID of the track. |
| Array | This is the list of markets in which the track is available. |
| String | This contains the date and time at which the track was saved by the user. |
The Get Track endpoint is used to get information about a track. The base URI of this endpoint is https://api.spotify.com/v1/tracks/{id}
. The path parameter {id}
is replaced with the ID of the track we want to get. So if we want to fetch a track whose Spotify ID is sample_id
, the URL will be https://api.spotify.com/v1/tracks/sample_id
.
Request parameters
The information obtained by calling this endpoint can be filtered using the following query parameters:
Query parameter | Type | Category | Description |
| String | Optional | 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 |
Let's look at how to use this endpoint to get the information of a track. We're getting the information about the track whose ID was saved in the previous executable.
const endpointUrl = new URL('https://api.spotify.com/v1/tracks/{{TRACK_ID}}');headerParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'}const options = {method: 'GET',headers: headerParameters,};async function fetchTrackInfo() {try {const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}fetchTrackInfo();
Let's look at how we changed the code:
Line 1: We change the URL to
https://api.spotify.com/v1/tracks/{{TRACK_ID}}
.Line 5: We change the access token to client credentials access token.
Line 13: We change the function's name to
fetchTrackInfo()
.
Try changing the ID of the track in line 1 to get information for a different track. Here are some track IDs:
Track | Spotify ID |
Love Without Question |
|
Without Me |
|
Perfect Duet |
|
Bones |
|
Highwayman |
|
Response fields
The response of this endpoint contains the following information:
Response field | Type | Description |
| Array | This contains the information about the album of the track. |
| String | This is the name of the track. |
| String | This contains the links for the tracks of the artist. |
| Integer | This index defines how popular a track is on Spotify. It ranges from |
| String | This is the link to a 30 seconds preview of the track. It may be |