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:
- Choose the artist(s) that you like.
- Randomly choose one (let’s say The Who) from the list and GET the meta information using Spotify API
- Get artists related to The Who and choose a random 5
- Create a playlist named “The Who Mix”
- GET the most popular songs from The Who, choose a random 5 and add to the playlist
- 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 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
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.
Update: Part 3 of non-linear workflows using Postman
Part 3: “Daily Mix” with Postman Monitors and Spotify
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
I have the same error, but I am not able to debug it. :/
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
Never mind. Silly mistake.
Pretty cool tutorial. I’m able to create the playlists by following your guide. Thank you.
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
Great article, but I found no mention of how {{client_credentials}} is defined in “Get Auth Key” request.
Found it: client_credentials is defined in the Pre-request Script of Get Auth Key
callback URL in the example picture provided is wrong
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
I receive “JSONError: Unexpected token u in JSON at position 0” when trying to GET Artist Top Track. Anyone else facing this problem?
Hi, Please contact our support team at https://www.postman.com/support, and they’ll be glad to help you.
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.