Create Playlist and Add Items to a Playlist
Learn to create a playlist and add items to it using a Spotify user account.
Overview
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 while 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 |
| Required | String | The name that we want to assign to the playlist. |
| Optional | Boolean | This sets the playlist as public or private. The possible options are |
| 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 |
| Optional | String | This is the description of the playlist |
Get the user's Spotify ID
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.
url = 'https://api.spotify.com/v1/me'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))
Click the "Save" button on the dialog box to save the user's Spotify ID throughout the course.
Create a playlist for the current user
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.
url = 'https://api.spotify.com/v1/users/{{USER_ID}}/playlists'data={'name': 'Educative playlist','description': 'Playlist created using the Spotify API'}headers = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}response = requests.request("POST", url, headers=headers, data=json.dumps(data))print(json.dumps(response.json(), indent=4))
Click the "Save" button on the dialog box to save the playlist's Spotify ID throughout the course.
Response fields
Some vital information that we get in response to this request is given below:
Response field | Type | Description |
| String | This is the description of the playlist provided by us. |
| Array of objects | This contains information about the user. |
| Array of objects | This contains information about the tracks in the playlist. It will be empty for newly created playlists. |
| Array of objects | This contains the link for the cover image of the playlist and its dimensions. It will be empty for now. |
| String | This is the ID of the playlist. |
| 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 |
| Optional | Integer | This array contains the position at which we want to place the items in the playlist |
| 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 |
| Optional | Integer | This array contains the position at which we want to place the items in the playlist |
| 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. |
Add tracks to the newly created playlist
The code below shows how to add tracks to the playlist we just created.
url = ('https://api.spotify.com/v1/playlists/{{PLAYLIST_ID}}/tracks')data={'uris':'spotify:track:4iV5W9uYEdYUVa79Axb7Rh,spotify:track:1301WleyT98MSxVHPZCA6M'}headers = {'Content-Type': 'application/json','Authorization': 'Bearer {{AUTHORIZATION_CODE_ACCESS_TOKEN}}'}response = requests.request("POST", url, headers=headers, params=data)print(json.dumps(response.json(), indent=4))
We get the snapshot_id
of the playlist in response.