User Playlists and Playlist Information

Take a look at how to get information about the playlists the current user is interested in.

Spotify provides an endpoint that is used to get the list of playlists owned or followed by the current user. We can then get information about these playlists using the Get Playlist endpoint.

Playlists owned or followed by the current user

The base URL https://api.spotify.com/v1/me/playlists is used to get information about these playlists. This can help us analyze what kind of content a user is interested in. We can then suggest similar content to the user.

Request parameters

The information obtained by calling this endpoint can be filtered using the following query parameters:

Query parameter

Category

Type

Description

limit

Optional

Integer

This limits the number of items to be returned in the response. Its value ranges from 0 to 50 and the default value is 20.

offset

Optional

Integer

This parameter can be used to offset the first item we get in response.

Let's see how we can use this endpoint to get the information about a playlist in the code widget below:

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/me/playlists');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters,
};
async function fetchUsersPlaylists() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchUsersPlaylists();

Let's look at the code explanation:

  • Line 1: We set https://api.spotify.com/v1/me/playlists as endpointURL.

  • Line 9: We set the request type to GET.

  • Lines 13–20: We define a function, fetchUsersPlaylists(), that calls the endpoint and prints the response.

We'll get a list of playlists in response. We're extracting the ID of the first playlist to use it with the second endpoint that we'll discuss in this lesson.

Try limiting the number of playlists we get in response by using the query parameter limit.  For example, for only ten playlists to appear in the result, append ?limit=10 to the base URI in line 1. This will limit the output to ten playlists.

Response fields

The response of this endpoint contains the following information:

Response field

Type

Description

description

String

This is the description of the playlist written by its owner.

owner

Array of objects

This contains information about the owner of the playlist.

tracks

Array of objects

This contains information about the tracks in the playlist.

images

Array of objects

This contains the link for the cover image of the playlist and its dimensions.

id

String

This is the ID of the playlist.

name

String

This is the name of the playlist.

Playlist details

The Get Playlist endpoint is used to retrieve information about a playlist using its Spotify ID. Its base URI is https://api.spotify.com/v1/playlist/{id}. The path parameter {id} is replaced with the playlist ID when making an API call. Let's assume that we want to get information for a playlist with Spotify ID sample_id. Our URL, in this case, will be https://api.spotify.com/v1/artists/sample_id.

Request parameters

The information obtained by calling this endpoint can be filtered using the following query parameters:

Query parameter

Category

Type

Description

additional_types

Optional

String

This contains the types of items, separated by commas, that the user's subscription allows to access. Its possible values are tracks and episodes. Its default value is tracks.

fields

Optional

String

This contains the list of fields that the user wants in response, separated by commas.

market

Optional

String

This is an ISO 3166-1 alpha-2 country code that can be used to filter the content available in a country. In case we send an auth token in the request header, the user's country will take priority over the country specified in this parameter.

Let's look at how to use this endpoint to get information about a playlist.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/playlists/{{PLAYLIST_ID}}');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{CLIENT_CREDENTIALS_ACCESS_TOKEN}}'
}
const options = {
method: 'GET',
headers: headerParameters
};
async function fetchPlaylistInfo() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
fetchPlaylistInfo();

Let's look at how we changed the code:

  • Line 1: We change the URL to https://api.spotify.com/v1/playlists/{{PLAYLIST_ID}}.

  • Line 5: We change the access token to clients credentials access token.

  • Line 13: We change the function's name to fetchPlaylistInfo().

Try changing the playlist ID in line 1 to get information about any other Spotify playlist.

Note: The playlist’s ID can be obtained using the Search for Item endpoint.

Response fields

The response data returned by this endpoint contains the following important information:

Response field

Type

Description

description

String

This is the description of the playlist that the creator wrote.

public

Boolean

This shows whether the playlist is public or not.

owner

Array of objects

This array contains the information of the owner.

tracks

Array of objects

This contains the information of the tracks in the playlist.

followers

Array of objects

This contains the information about the followers of the playlist.

images

String

This is the link to the cover of the playlist.

name

String

This is the title of the playlist.