Manage Albums in a User’s Profile
Learn how to save or delete an album from a user profile using Spotify API.
We'll cover the following
Spotify users can save albums of their favorite songs on the platform. All the saved albums can be viewed from the user’s profile. We can manage the albums saved in a user profile using the Spotify API. In this lesson, we’ll discuss two types of HTTP requests. One is used to save an album to the current user profile whereas the other is used to remove an album from a user profile.
Album management
We can use the base URL https://api.spotify.com/v1/me/albums
to save or delete an album to the current user's profile. A PUT
request to this endpoint saves the album, whereas a DELETE
request deletes the specified album.
Note: The user is the one with whom the authorization code access token is associated. Currently, we're using access tokens associated with our own profile, so we'll be making changes to our own resources.
Request parameters
This endpoint has the following query parameter:
Query parameter | Category | Type | Description |
| Optional | String | This contains the IDs of the albums we want to save or delete from the current user's profile. These IDs are separated by commas (,). We can save a maximum of 20 albums with one call. |
The table below contains the body parameter of this endpoint.
Body parameter | Category | Type | Description |
| Optional | String | This contains the IDs of the albums we want to save or delete from the current user's profile. These IDs are separated by commas (,). We can save or delete a maximum of 50 albums with one call using this parameter. If we use the |
Note: We can use the Search for Item endpoint to get the Spotify ID of an album.
Let's look at how this endpoint is used to save albums with the authorization code access token. We're providing the IDs of Pitbull's album Can't Stop Us Now, and Bruno Mars' albums Doo-Wops & Hooligans as the values for the query parameter ids
.
const endpointUrl = new URL('https://api.spotify.com/v1/me/albums');const queryParameters = new URLSearchParams({ids: '4aawyAB9vmqN3uQ7FjRGTy,1uyf3l2d4XYwiEqAb7t7fX'});//4aawyAB9vmqN3uQ7FjRGTy is the ID of Pitbull's album Can't Stop Us Now//1uyf3l2d4XYwiEqAb7t7fX is the ID of Bruno Mars's album Doo-Wops And HooligansheaderParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}const bodyParameters={}const options = {method: 'PUT',headers: headerParameters,body: bodyParameters,};async function saveAlbum() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}saveAlbum();
Let's look at the code explanation:
Line 1: We set the URL to
https://api.spotify.com/v1/me/albums
.Lines 3–5: We define the query parameters.
Line 14: We define an empty object,
bodyParameters
. Even though we are not using body parameters in this case, we still send an empty object as body parameters because it is required for this endpoint.Line 17: We set the request type to
PUT
.Lines 22–30: We define a function,
saveAlbum()
, that calls the defined endpoint and prints the response.
Let's delete one of the two albums we saved above from our profile by sending a DELETE
request to https://api.spotify.com/v1/me/albums
. Just like the case where we saved albums, we'll use the query parameters to delete the album.
const endpointUrl = new URL('https://api.spotify.com/v1/me/albums');const queryParameters = new URLSearchParams({ids: '4aawyAB9vmqN3uQ7FjRGTy'});//4aawyAB9vmqN3uQ7FjRGTy is the ID of Pitbull's album Can't Stop Us NowheaderParameters = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}const bodyParameters={}const options = {method: 'DELETE',headers: headerParameters,body: bodyParameters,};async function saveAlbum() {try {endpointUrl.search = queryParameters;const response = await fetch(endpointUrl, options);printResponse(response);} catch (error) {printError(error);}}saveAlbum();
Let's look at the modifications we made in the code:
Lines 4: We set only one ID as a query parameter as we want to remove a single album.
Line 13: We change the request type to
DELETE
.Line 21: We change the function's name to
removeAlbum()
.
Try removing multiple albums from the user's library by adding more IDs (separated by commas) in line 4.
Response fields
The content
of the response is empty for a successful request, and the value of status_code
is 200
. For an unsuccessful request, we get a message
in response that tells us about the error.