How to Get the Currently Playing Song using the Spotify Node.js API & TypeScript

A quick and straightforward guide to hooking into the Spotify's awesome API to show the current song that you're listening to. It may have taken me a few attempts, but I've finally figured it out. Check it out.

Intro

When I first started building khalilstemmler.com, I stubbed out a component to present the current song that I was listening to on Spotify. Y'know, I just wanted to add a neat little feature to make the site a little bit more unique.

Fast forward eight months, and it's still not hooked up. After about four attempts, I've finally gotten the hard part down (integrating with the API). At some point, I might code the React component.

This fun Spotify feature is part of my Personal Data Graph.

The reason I need a Personal Data Graph for this feature is, to fetch authenticated user resources from Spotify, the service that asks for the data needs to be, well- authenticated... as me.

The docs from Spotify's Authorization Guide told me that I needed to use the Authorization Code approach, one of 3, to make this work.

I'm going to plow through this process and show you, very imperatively, the steps involved to authenticate a backend service to act on your behalf to fetch your currently playing song from Spotify.

Prerequisites

You're going to need to be using Node version 10x or higher for this to work. The reason for that is because the code that talks to Spotify relies on the URLSearchParams global.

Installation and Setup

Clone or fork my repo over at https://github.com/stemmlerjs/stemmlerjs-graph to follow along.

Run the following commands to clone, install, and set up the project.

git clone git@github.com:stemmlerjs/stemmlerjs-graph.git
cd stemmlerjs-graph
npm install
mv .env.template .env

Creating a Spotify Developers Account

Go to the Spotify Developers Dashboard and sign up for an account.

When you're all signed up, you'll get to the Spotify Developers Dashboard that shows your applications. Click "Create An App".

"Spotify Dashboard"

Spotify Developers Dashboard. You won't have an application already when you sign up the first time.

Follow the prompts and select Non-Commercial for the type of integration that we're creating (if you are creating a non-commercial app, that is).

"Creating a new app"

"Finishing registration"

Getting your Client ID and Client Secret

When you log in, you should be able to see your Client ID. Underneath it, you can click to see your Client Secret.

"Access Code and Secret Access Code"

First homework on the repo you cloned is to go into the .env file and supply the Client ID and Client Secret into the file for SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET.

.env
SPOTIFY_CLIENT_SECRET= # paste here
SPOTIFY_CLIENT_SECRET= # paste here as well :-)

Setting up our application

Now we need to configure the application with the website and redirect URI.

On the dashboard page, click "Edit Settings", and a modal should appear.

"Finishing registration"

Configuring the Website

For website, you don't need to have a website up and running yet, so just put in the name of a website that you own.

If you don't own a website, you can use google.com.

Configuring the Redirect URI

This is where Spotify sends us after we've logged in. They send us to the URL that we supply, but also give us back an authorization code. We use that authorization code to get an access and refresh token.

If you have a website, you can put any URL from your domain here, and Spotify will redirect us there after logging in.

If you don't have a website, again- that's OK, we can use http://google.com/callback or something, but do understand that the domain that owns the website that we redirect to is fully capable of reading the authorization code we receive.

When you enter your Redirect URI, don't forget to press the "Add" button beside the URL, and THEN click "Save". That's how attempt #1 of hooking this up failed for me - though you're much smarter so that won't happen to you.

Whatever you've provided as the Redirect URI, make sure to update the .env with this as well.

.env
...
SPOTIFY_REDIRECT_URL=https://khalilstemmler.com/callback # For example

Getting the initial Authorization Code

Take this URL and substitute your Client ID and Redirect URI, where I've placed PASTE_HERE, then paste the entire link into your browser.

https://accounts.spotify.com/authorize?client_id=PASTE_HERE&
response_type=code&redirect_uri=PASTE_HERE&
scope=user-read-private%20user-read-currently-playing

When you paste the link in your browser, you'll be asked to log in using your Facebook or email. Do that.

Successfully logging in will bring you to this page that asks you to confirm the type of data that we're asking for. The authorization scope that we're interested in is being able to see the user's currently playing song. Click "agree" here.

"Accept scope"

After clicking "agree", we'll get sent to the redirect URI that we passed in earlier, but with our authorization code as a code parameter in the resulting URL (check the URL bar).

"Finishing registration"

We're going to want whatever comes after ?code= in the URL, so copy that.

Using the Authorization Code to get an Access Token and the Refresh Token

In the root of the repository, run the following script with the code you copied from the URL.

npm run get-refresh-token <paste-your-code-here>

This will ask for a permanent refresh token that we can use to make subsequent requests to the API and never have to worry about logging in again (that's dope).

If all goes well, console output should look like this.

"Getting the refresh token using a script"

Copy the refresh token that was emitted to the console into the .env file.

.env
...
SPOTIFY_REFRESH_TOKEN= # Paste the refresh token that we just got here

A couple of things to note if this doesn't work for you.

  • You have to run this script within roughly a minute of getting the authorization code from Spotify. This is a security by opportunity- they reduce the total amount of time that someone can use that code hanging out in the open. If you get a 400 error, go back to Getting the initial Authorization Code and paste the link again to generate a new authorization code.
  • It will also fail if you've forgotten to supply the SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, and SPOTIFY_REDIRECT_URL values.
  • The last reason this might fail is if you're not running Node version 10 or higher.

Getting the currently playing song via GraphQL Playground

In the repo, I've already gone and done all the hard work of setting up a GraphQL server, defining the schema, and mapping the API results to a song type.

The action happens in spotifyService.ts which you can peruse if you like.

If you'd like to play around with the graph via GraphQL playground, run:

npm run start

Then go to http://localhost:4000/, fire up your Spotify, play some music, and then run this query to see the results.

{
  spotifyGetCurrentSongPlaying {
    artist {
      name
    }
    title
    previewUrl
    externalUrl
    currentProgress
  	album {
      name
    }
  }
}

Voila!

"Getting the refresh token using a script"

If you're interested in my terrible music taste, pop over to stemmlerjs-graph.netlify.com and see what I'm listening to currently.

That's it! I hope this helps someone.



Discussion

Liked this? Sing it loud and proud 👨‍🎤.


3 Comments

Submit
Hannes
4 months ago

Hi there,

All I get is this error, even though im currently playing a song on spotify. (The same happens when I try it on your netlify).

{
  "data": {
    "spotifyGetCurrentSongPlaying": null
  }
}

Also, there is no .env file in the repo, so I added one myself, but im not sure if I got all there is to add:

SPOTIFY_CLIENT_ID=
SPOTIFY_CLIENT_SECRET=
SPOTIFY_REDIRECT_URL=
SPOTIFY_REFRESH_TOKEN=

Hannes
4 months ago

Nevermind, found the mistake :D


Khalil Stemmler
4 months ago

Woo!

Artem
a month ago

Hello, thanks for the article.


On the https://stemmlerjs-graph.netlify.app/ I'm getting GraphQL related error (see console in DevTools) when pressing the Play button.


Stay in touch!



About the author

Khalil Stemmler,
Developer Advocate @ Apollo GraphQL ⚡

Khalil is a software developer, writer, and musician. He frequently publishes articles about Domain-Driven Design, software design and Advanced TypeScript & Node.js best practices for large-scale applications.



View more in Web Development



You may also enjoy...

A few more related articles

How to Prerender Comments | Gatsbyjs Guide
Prerendering dynamic data can have several advantages. For Gatsby blogs with high engagement, comments can positively impact SEO, ...
Over $85 billion spent on fixing bad code [bootcamps, junior devs, JavaScript, and software design principles]
More and more money is being spent by companies on maintaining bad JavaScript code. Here's where I think our industry needs to cha...
TypeScript vs. JavaScript [Pros / cons]
TypeScript is arguably one of the best things to come to the JavaScript world helping developers comfortably create complex enterp...
Junction Model Pattern: Many-to-Many - Sequelize
Many-to-many is a common modeling relationship between two entities. Here's one way to handle it with the Sequelize ORM.

Want to be notified when new content comes out?

Join 5000+ other developers learning about Domain-Driven Design and Enterprise Node.js.

I won't spam ya. 🖖 Unsubscribe anytime.

Get updates