Music Discovery with Spotify and Postman

Ankit Sobti
The RESTful Web
Published in
3 min readDec 4, 2016

--

This continues from my previous post in the series Conditional Workflows in Postman. We will continue to focus on Postman Collections and elaborate on Postman Runtime.

Today, we are going to create our own on-demand playlist using the Spotify API and Postman Collections.

I love Spotify. Over the years it has helped me discover great music. Discover Weekly, in particular, has made me look forward to Monday mornings, with some amazing curated music delivered to the app. Usually, when I come across a great new artist, I look for their top songs and when a bit more adventurous, check out the related artists and repeat the process. Quickly jotting this down into a workflow:

1. Choose the artist(s) that you like.
2. Randomly choose one (let’s say The Who) from the list and GET the meta information using Spotify API
3. Get artists related to The Who and choose a random 5
4. Create a playlist named “The Who Mix”
5. GET the most popular songs from The Who, choose a random 5 and add to the playlist
6. For each related artist, do the same

And that’s it! We get a brand new playlist.

Click the Run in Postman button below to get the Collection and Environment in your Postman app.

You can check out the documentation for the collection here and download the collection here:

Setup the Environment

  1. Spotify

a. Head to Spotify Developer and register, then create a new app in the My Applications section. Add the `client_id` and `client_secret` to your environment.
b. Under the newly created app config, add the following Redirect URI — “https://www.getpostman.com/oauth2/callback"
c. We’ll use the Authorization Code Flow to obtain the Refresh Token.
d. Open Postman, under a new request, click on the Authorization tab, select OAuth 2.0 and fill in these values:

Token Name: can be anything
Auth URL: https://accounts.spotify.com/authorize
Access Token URL: https://accounts.spotify.com/api/token
Client ID: {{client_id}}
Client Secret: {{client_secret}}
Scope: playlist-read-private playlist-modify-private
Grant Type: Authorization Code
Request access token locally: Checked

Click on Request Token, go through the OAuth flow, and add the refresh_token to your environment

2. Environment Config

Set the following variables in your environment
a. user_id: your spotify username
b. country_code: in ISO 3166–1 format
c. N: defaults to 5. Refer to the algorithm section for more details
d. artists: List of seed artists for the collection. You can find them as part of the Spotify artist link (Available in the app): e.g. for Oasis, the link looks something like https://open.spotify.com/artist/2DaxqgrOhkeH0fpeiQq2f4, choose the last section. The artists variable needs to be set up as an array of strings, that is, with square brackets and double quotes. So it’ll look something like: [“2DaxqgrOhkeH0fpeiQq2f4”, “2ooIqOf4X2uz4mMptXCtie”]

3. Run the Collection

a. Now run the collection in the Postman app using the Collection Runner

By keeping Persist Variables checked, the next time we run this collection, it will add the newly discovered Related Artists to the original seed and thus expanding the scope of discovery, each time we run this collection.

b. The same collection executes in Newman as well. Just export the Collection and Environment files and execute the following command

newman -c SpotifyPLGen.json.postman_collection -e SpotifyPlGen.json.postman_environment

In the final post of this series, we will schedule this collection to run every week for that true Discover Weekly experience.

--

--

Ankit Sobti
The RESTful Web

CTO | Postman. Passionate about building products and companies.