postman-team

Generate Spotify Playlists using a Postman Collection

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.

spotify-workflow-diagram

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

Run in Postman

You can find detailed documentation for 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:

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

screen-shot-2016-10-25-at-9-19-22-pm

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
screen-shot-2016-10-25-at-8-59-53-pm

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.

screen-shot-2016-10-25-at-9-00-41-pm

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

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

Update: Part 3 of non-linear workflows using Postman

Part 3: "Daily Mix" with Postman Monitors and Spotify

Author: ankit

Founder and CTO | Postman

  • Vanessa

    Thank you for your interesting blog post. I tried out your template for creating a Spotify Playlist. Unfortunately, Postman yields an error when I run the tests:

    "Error executing pre-request scripts for Get Artist Top Tracks: SyntaxError: Unexpected token u in JSON at position 0"

    When I try it manually it works. Can you reproduce this error?

    Regards