Create Playlist and Add Items to a Playlist

Learn to create a playlist and add items to it using a Spotify user account.

In this lesson, we'll discuss two endpoints. One is used to create a playlist using a Spotify user account, and the other is used to add items, or tracks, to that playlist or a different playlist that the user owns.

New playlist

The Create Playlist endpoint of the Spotify API creates a playlist using our current user's Spotify account. The base URI of this endpoint is https://api.spotify.com/v1/users/{user_id}/playlists. We'll need the Spotify ID of the user to create the playlist. The {user_id} in the base URI is replaced with the Spotify ID of the user when calling the API. A POST request is used to create a playlist. The playlist created will be empty.

Request parameters

This endpoint has no query parameters.

The body parameters of this endpoint will contain information about the playlist. The information about these parameters is given below:

Query parameter

Category

Type

Description

name

Required

String

The name that we want to assign to the playlist.

public

Optional

Boolean

This sets the playlist as public or private. The possible options are true and false. The value of this parameter by default is true.

collaborative

Optional

Boolean

This sets the playlist to be collaborative or not. If the playlist is collaborative, you can invite other users to edit your playlist. Its default value is false. If it is to be set to true, the value of the public parameter must be false.

description

Optional

String

This is the description of the playlist

To create a playlist, we need the user_id for the current user. Spotify provides the Get Current User's Profile endpoint, which is used to get the required ID. This endpoint requires no parameters. All we have to do is to call the base URI https://api.spotify.com/v1/me and we'll get the required information in response. The code below calls this URL to get the ID.

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

Click the "Save" button on the dialog box to save the user's Spotify ID throughout the course.

Now that we have a user ID, we can create a playlist. The code below shows how to use the Create Playlist endpoint with the request parameters.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/users/{{USER_ID}}/playlists');
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
bodyParameters = {
name: 'Educative playlist',
description: 'Playlist created using the Spotify API'
}
const options = {
method: 'POST',
headers: headerParameters,
body: JSON.stringify(bodyParameters),
};
async function createPlaylist() {
try {
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
createPlaylist();

Click the "Save" button on the dialog box to save the playlist's Spotify ID throughout the course.

Let's look at a brief explanation of the code in the widget above:

  • Line 1: We set the URL to https://api.spotify.com/v1/users/{{USER_ID}}/playlists.

  • Lines 8–11: We add the body parameters.

  • Lines 13–17: We set the request type to POST and set the body and header.

  • Lines 19–26: We define a function, createPlaylist(), that calls the endpoint and prints the response.

Response fields

Some vital information that we get in response to this request is given below:

Response field

Type

Description

description

String

This is the description of the playlist provided by us.

owner

Array of objects

This contains information about the user.

tracks

Array of objects

This contains information about the tracks in the playlist. It will be empty for newly created playlists.

images

Array of objects

This contains the link for the cover image of the playlist and its dimensions. It will be empty for now.

id

String

This is the ID of the playlist.

name

String

This is the name of the playlist.

Add tracks to a playlist

The playlist we've just created using the user's profile is empty.  Let’s learn how to add tracks to that playlist or in any user playlists using Add Items to Playlist endpoint.

The base URI of this endpoint is https://api.spotify.com/v1/playlists/{playlist_id}/tracks. The {playlist_id} in the base URI is replaced with the Spotify ID of the playlist when making an API call. So if we want to add tracks in a playlist whose ID is sample_id, our base URI will be https://api.spotify.com/v1/playlists/sample_id/tracks.

Request parameters

This endpoint has the following query parameters:

Query parameter

Category

Type

Description

position

Optional

Integer

This array contains the position at which we want to place the items in the playlist

uris

Optional

Array

This array contains the Spotify URIs of the item we want to add. These URIs can be obtained using the Get Track, Get Episodes or Search for Item endpoints. The array can contain a maximum of 100 URIs.

The information about the body parameters is given below:

Parameter

Category

Type

Description

position

Optional

Integer

This array contains the position at which we want to place the items in the playlist

uris

Optional

Array

This array contains the Spotify URIs of the item we want to add. These URIs can be obtained using the Get Track, Get Episodes, or Search for Item endpoints. The array can contain a maximum of 100 URIs. If the query parameter contains the URIs, these URIs will be ignored.

The code below shows how to add tracks to the playlist we just created.

Press + to interact
const endpointUrl = new URL('https://api.spotify.com/v1/playlists/{{PLAYLIST_ID}}/tracks');
const queryParameters = new URLSearchParams({
uris: 'spotify:track:4iV5W9uYEdYUVa79Axb7Rh,spotify:track:1301WleyT98MSxVHPZCA6M'
});
headerParameters = {
'Content-Type': 'application/json',
'Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'
}
bodyParameters = {}
const options = {
method: 'POST',
headers: headerParameters,
body: bodyParameters,
};
async function addTracks() {
try {
endpointUrl.search = queryParameters;
const response = await fetch(endpointUrl, options);
printResponse(response);
} catch (error) {
printError(error);
}
}
addTracks();

We made the following changes in the code:

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

  • Lines 3–5: We set the query parameters. In this case, we are using the query parameters to send the uris of the items we want to add to the playlist.

  • Line 12: We change the body parameters. Since we are using the query parameters to send the uris of the items we want to add to the playlist, we set the body parameters as an empty object.

  • Line 20: We change the function's name to addTracks().

Response fields

We get the snapshot_id of the playlist in response.