Learn from experts and connect with global tech leaders at POST/CON 24. Register by March 26 to save 30%.

Learn more →
X

Generate Spotify Playlists using a Postman Collection

Avatar

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.

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.postman.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

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

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.

Update: Part 3 of non-linear workflows using Postman

Part 3: “Daily Mix” with Postman Monitors and Spotify

Tags:

What do you think about this topic? Tell us in a comment below.

Comment

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

13 thoughts on “Generate Spotify Playlists using a Postman Collection

  • Avatar

    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

    • Avatar

      I have the same error, but I am not able to debug it. :/

  • Avatar

    Thank you for giving me a reason to get back into tinkering with web APIs. I tried following the instructions but I keep getting the error “invalid_client” on POST Get Auth Key.

    Request headers (7)
    Accept */*
    accept-encoding gzip, deflate
    Authorization Basic Og==
    content-length 170
    content-type application/x-www-form-urlencoded
    Host accounts.spotify.com
    User-Agent PostmanRuntime/3.0.11-hotfix.2

    Any help is greatly appreciated.

    Best,

    Joe

    • Avatar

      Never mind. Silly mistake.

  • Avatar

    Pretty cool tutorial. I’m able to create the playlists by following your guide. Thank you.

    • Avatar

      hi Yashwant I have same error like comment below (Error executing pre-request scripts for Get Artist Top Tracks etc..) Do you observe the same or how do you work around it? Cheers, Charlie

  • Avatar

    Great article, but I found no mention of how {{client_credentials}} is defined in “Get Auth Key” request.

    • Avatar

      Found it: client_credentials is defined in the Pre-request Script of Get Auth Key

  • Avatar

    callback URL in the example picture provided is wrong

  • Avatar

    The best playlist curators way to get more streams and listeners is to get on more playlists. With our Playlist Package, you can do exactly that. We connect you with popular Spotify and Apple Music playlist curators and help you boost your fan base by getting more plays on your songs.
    playlist curators

  • Avatar

    I receive “JSONError: Unexpected token u in JSON at position 0” when trying to GET Artist Top Track. Anyone else facing this problem?

  • Avatar

    Thank you for the very interesting – and useful! – example.

    I have one concern, though: When my token has expired, I would like the Collection Runner to initialize a request for a new token from Spotify – this would include displaying the Spotify login page if required – when I run the collection. Right now, I have to use that extra request I set up in step 1 d to initialize a new authenication, which seems a bit clunky.

    Any suggestions – with our without Postman Monitor, but preferably without as I don’t have accces to Monitor at the moment.