Matdweb / Jamming

This project allows you 👥 to create and customize 🎨 playlists 🎼 and send them to your Spotify account using API technology.
https://jamming-sooty.vercel.app
MIT License
0 stars 1 forks source link

Create new playlist endpoint #27

Closed Matdweb closed 1 year ago

Matdweb commented 1 year ago

About this Pull Request

Here I created a createNewPlaylist functionality that's capable of:

How did we create a new playlist in the user's account ?

We accomplished this using the Spotify API, learn more here Here's the step by step:

  1. We use the access-token retrieved from the spotify API, learn how we did that here #22
  2. With this access-token we make the rest of the API calls, passing it as a Bearer token, learn about it here

The header of this requests looks like this: image

  1. Then, first we needed to retrieve the user_id, learn how we did it here

We made a POST request to this https://api.spotify.com/v1/me spotify endpoint headers: Authorization: Bearer {access_token}

  1. Then, with the user_id we made a POST request to make a new empty/default playlists with the indicated name, learn how to do it here

endpoint: https://api.spotify.com/v1/users/${user_id}/playlists headers: Authorization: Bearer ${access_token}, 'Content-Type': 'application/json', 'Accept': 'application/json', body: "name": playlist_name, "description": "Jamming New playlist default description", "public": false

This responds with a playlist_id which is the id for the new playlist created

  1. Next, we added the mock-songs the to previous and just created playlist using the playlist_id, learn how we did it here

endpoint: https://api.spotify.com/v1/playlists/${playlist_id}/tracks headers: Authorization: Bearer ${access_token}, 'Content-Type': 'application/json' body: "uris": songs_uris, "position": 0

  1. This way, we created a new playlist in the user's spotify account with the costumized data

IMPORTANT The songs_uris are an array of strings contains each song's uri, learn about it here

api/spotify/createNewPlaylist/addSongs

We modified the original endpoint establish in the #6 ticket, because we are now calling the endpoint just to add the songs to the already created playlist

How to use this endpoint?

If you want to make use of this functionality in the Front-end, you need to follow this:

  1. You need to import the createNewPlaylist() function from thelib/ folder
  2. You use this function passing the access-token, the playlistName setted and thesongs-uris
  3. This function will call the createNewDefaultPlaylist() in the backend and return a playlist_id. You need to pass the access-token and the playlistName to this function
  4. Next, the backend will call the api/spotify/createNewPlaylist/addSongs wich will add the songs to the new playlist using the playlist_id

    This is how this call should look like: image

  5. Then, if everything goes right the createNewPlaylist() function should return a response object with the snapshot_id data

Test

See how it works: createNewPlaylistEndpoint

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jamming ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 21, 2023 7:42pm