DEV Community

Cover image for Postman ft. Spotify, a study into the automation capabilities of Postman
Amruth Pillai
Amruth Pillai

Posted on

Postman ft. Spotify, a study into the automation capabilities of Postman

Disclaimer

Most of you may have read my previous article on how I lost a job opportunity that I was lucky to get to COVID. Well, fast forward two months later and here I am, working for one of the companies I've always admired and used on a day-to-day basis.

I started working for Postman from last week, and it's been a wild roller coaster ride of self-judgement and amazement, but the people around me have kept me motivated to working at my best.

They have this tradition where everyone who joins is supposed to make a first collection and demo it, mainly to explore the product and find out more of what it can do. This article is a documentation of just that.

Introduction

I've recently moved to Spotify from YouTube Music, and ever since, I've been in love with it's recommendation engine, and more than that, it's developer APIs. It's a delight to work with APIs that have so much to do with music, machine learning and personalization.

My idea here was to build my very first collection on what I am most passionate about... and since Unsplash APIs for photography were too restrictive, I went with my second passion, music.

Screenshots

Spotify Screenshot

Prerequisites

  • Google Cloud Auth Token, with Natural Language API enabled
  • Spotify Developer Application (Client ID, Client Secret)

Documentation

Documentation

https://documenter.getpostman.com/view/281418/SzzdE2VK

Automation Workflow

On-Demand Personalized Playlists based on your Current Mood

On-Demand Personalized Playlists based on your Current Mood

The flow of this automated Postman Collection is as follows:

  • User enters a small and simple sentence describing their mood.
    • Get a Sentiment Score using Google's Natural Language API
    • Use math to map the value from an original range of (-1, 1) to (0, 1)
  • Get Currently Logged In User's Profile on Spotify
    • Run Test to check if User was Returned Successfully
    • Store user_id value to Collection Variables
  • Get Current User's 5 Recently Played Tracks
    • Run Test to check if 5 Tracks were returned
    • Store Track IDs in recently_played Collection Variable as CSV
  • Create Private Playlist "Postman ft. Spotify", if not already created
    • Go to next request if already created using postman.setNextRequest()
    • Run Test to check if status code is 201 Created
    • Store playlist_id value to Collection Variables
  • Get Recommendation of Tracks for Current User
    • Use target_energy to gauge the energy of tracks, we will be using the mapped sentiment value for this
    • Use the value of recently_played for the query param seed_tracks which takes a max. of 5 track IDs to seed it's recommendations
    • Run Test to check if status code is 200 OK and snapshot_id was returned
    • Store the recommended tracks into a comma-separated variable called recommended_tracks in the Collection Variables
  • Replace the Original Playlist Tracks with New Recommended Tracks
    • Run Test to check if status code is 201 Created
    • Check Spotify for the newly updated playlist, strap on your headphones and enjoy!

One-Click Lyrics for the Current Playing Song

Another later added feature to this collection was the ability to view lyrics to songs that were currently playing on your Spotify account. This is something I do very often, and found it useful for me since I would be able to get the lyrics to a song on-demand with just one click.

Get Lyrics for the Current Playing Song

  • Get Currently Playing Song on Spotify
    • Extract artist_name and track_name from the response and store as a collection variable
  • Get Lyrics to the Song from lyrics.ovh APIs
    • Use artist_name and track_name as query parameters to the GET request made to api.lyrics.ovh
  • Display Lyrics using Postman Visualizer
    • Response of the API is in a JSON format, unreadable and unsuitable for karaoke with friends, so display them on a Postman Visualize Template.

One Click Lyrics

What else can Postman do?

Postman Collection Runner

Thanks to Postman's Collection Runner, and the branching functionality provided through Postman’s Test Scripts, I was able to automate the process and get a new batch of fresh music to discover on-demand with just a click of a button.

Another added advantage is Postman's Monitor feature to mimic the work of cron jobs (at least that's how I see it), to be able to make these requests on a regular interval, quite similar to Spotify's Discover Weekly feature.

Key Takeaways

  • Learning that Postman can do a lot more than just Documentation, it can perform Automation Tasks which usually require some scripting knowledge, but a part of the weight is lifted off here.
  • Making use of tools like Postman Collection Runner, Postman Monitor, Newman (always found the name interesting, Seinfeld #Represent)
  • Trying to Authenticate with different kind of APIs in different ways: OAuth 2.0 for Spotify, JWT Bearer Token for Google Cloud and handling them within the Postman User Interface

Thank you for reading!

Top comments (0)