Spotify | Music Discovery with Postman

We all love music. Love hearing it, making it, discovering it. Services like Pandora and Spotify have altered the way we satiate our need for discovering the next amazing song or artist.

A few years ago, Spotify introduced me to Neutral Milk Hotel. Their album, In the Aeroplane over the Sea was surreal, raw and honest. Today we’re going to make our own simplified version of the Discovery algorithm to identify similarly fervent music, using our beloved Postman app and Spotify’s fantastic API.

The objective is to generate a playlist with top songs from NMH (or an artist of your choosing), and other artists similar to it. Quickly jotting down the workflow:

  1. GET meta information about NMH using it’s Spotify ID
  2. GET the artists related to NMH
  3. Create a playlist named “Neutral Milk Hotel Mix”
  4. GET the most popular songs from NMH, choose a random 5 and add to the playlist
  5. For each related artist, do the same
  6. Et voila! A wonderful new mix!


At this point, anyone who wants to short circuit their way to the Playlist, click the Run in Postman button below to get the Collection and Environment file in your Postman app (versions 4.0 and above). Then follow the first 3 steps underneath.


Get the artist ID from Spotify:


You get something like – spotify:artist:2ooIqOf4X2uz4mMptXCtie

2ooIqOf4X2uz4mMptXCtie is the artist id. Copy this inside your imported environment variable, as id.

Get the access token from Spotify

To not go beyond the scope of this discussion, I’ll keep auth simple and take the access token directly from Spotify’s Web Console.
Head to: and sign in. You’ll have to sign up as a developer account (A lot easier than it sounds)


Click on the Get OAuth Token button and grant the privileges as shown below:


Copy the access token in your environment under the authorization field.

Create/Update the Environment

If you used Run in Postman, update the imported environment, otherwise create a new Environment in Postman with the values as shown below:
a. id -> Artist ID from Step 1
b. country_code -> As outlined under available_markets in Spotify ( – Defaulting to US
c. authorization -> AccessToken (from Step 2)
d. user_id -> Spotify username
e. N -> number of related songs and artists you want to add to the playlist


For the importers, jump to Step 5, else follow along!

Create Postman Collection

Create a new collection, named Spotify Playlist Generator, with the following requests:

a. Get Artist – As per the diagram above, the first request will GET the Artist’s metadate and store its name in the environment. We will use this to create the playlist name later.


URL: GET{{id}}
i. Accept: application/json
ii. Authorization: Bearer {{authorization}}


i. This function that allows non-linear execution of requests. As is evident from the name, you just need to specify the name of the subsequent request and the runner will take care of the rest.
ii. postman.setNextRequest is attached to a request and can be set in the pre-request or the test script. In case of more than one assignment, the final value is considered.
iii. The absence of postman.setNextRequest in the request’s pre-request or test scripts signals the end of workflow execution.
iv. If any request in the collection uses setNextRequest, the collection is considered to have a manual flow control, i.e. the normal linear execution is no longer honored.

So in the test script above, if the responseCode wasn’t 200, execution would stop.

b. Get Related Artists – I think I’d like a little randomness here, so we get a different playlist each time we run the collection. So let’s choose the N related artists randomly, instead of going for the most popular ones.


URL: GET{{id}}/related-artists
i. Accept: application/json
ii. Authorization: Bearer {{authorization}}


c. Create a Playlist – Retrieve the name stored in the first request and create a playlist with the name “Artist_Name Mix”


URL: POST{{id}}/related-artists
i. Accept: application/json
ii. Content-Type: application/json
iii. Authorization: Bearer {{authorization}}



d. Get Artist Top Tracks – Now we get into the fun part. We are going to loop through this and the next request N times (once per artist stored in the artists array). i.e. For each artist, get the top tracks and store them in the playlist.


URL: GET{{artist_id}}/top-tracks?country={{country_code}}
i. Accept: application/json
ii. Authorization: Bearer {{authorization}}

Pre-request Script:


e. Add Tracks to Playlist – Add the track ids retrieved in the previous call, for the artist indexed at c, to the playlist (playlist_id)


URL: POST{{user_id}}/playlists/{{playlist_id}}/tracks?uris={{uris}}
i. Accept: application/json
ii. Content-Type: application/json
iii. Authorization: Bearer {{authorization}}

Pre-request Script:


Run the Collection

Moment of truth! Now let’s run the collection and see if it works.



As you can see, the last 2 requests executed 6 times (1 for NMH and 5 for related artists)

And even Spotify has the playlist. Woohoo!


By the Way, the same collection executes in Newman as well. Just export the Collection and Environment files and execute the following command


I hope you enjoyed this sneak preview into Postman Workflows. setNextRequest is just the beginning and we are working on more APIs to create increasingly complex workflows in the easiest possible manner in Postman. So stay tuned!

Author: ankit

Founder and CTO | Postman