Skip to content

A quick and easy way to display what your currently playing song on Spotify is on your personal website

License

Notifications You must be signed in to change notification settings

dev-sda1/spotify-web-presence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spotify-web-presence

image image

A quick way to display what your currently playing song on Spotify is on your website or elsewhere in a JSON format. If you have any questions / improvements send me a dm or open a PR :p

ko-fi

Prerequisites

  • NodeJS and npm (sadly this was done with express)
  • Spotify Account
  • A webserver to host the endpoint on

Setting up (Server-side)

The client folder only serves as an example for how you could display it to the client.

Creating your app

1) Go to https://developer.spotify.com/dashboard, login and click "create an app" to give it a name and description.

App creation wizard

  1. You'll be given a Client ID and Client Secret. Store these somewhere as you'll need them later. Make sure to keep your Secret private. The client secret should never leave the server

Keys

  1. Click edit settings and scroll down to the Redirect URIs section. This is what spotify uses to tell our server that authentication was a success when we get to acquiring our access and refresh tokens. As this is being used on a portfolio (personal) website hopefully, all you'll need is to add localhost. Type http://localhost:1337/callback and click add, then save.

Configuring the server

1) Clone the repository into your directory of choice for the endpoint and enter the config.json file in the server folder, where you'll need to paste your client token and secret into the labelled fields. Keep the refresh token space blank that is how spotify will know it's you later.

  1. Save the config.json file and exit out, then open a terminal and run npm install && node . to install the 14 trillion node dependencies and to start it immediately.

  1. Once everything is installed, you'll be prompted in the console / terminal output to visit an auth url on Spotify's website to verify yourself. Make sure you're signed into the right spotify account before continuing.

  1. If you've entered everything on the developer console and app.js correctly, you'll be taken to an OAuth page, where you'll need to click "Accept".

  1. After clicking agree, check your console output for the tokens that spotify would've sent to you. Your access token expires after an hour, and your refresh token is used to acquire a new access token. The refresh token never expires unless you delete the app from your developer panel or you revoke access on your account settings, therefore it is absoloutely essential you keep your server secure and the refresh token secret.

  1. Copy the refresh token in it's entirety and paste it into the RefreshToken column, save the json, then stop the application. If you're doing this on a linux-based SSH session (which is likely), you'll need to exit out of the node process before you're able to edit the config. (Done by pressing CTRL+C).

  1. Restart the application, where it should now only notify you that your (access) token has been refreshed. If you get a Spotify API error complaining about an invalid refresh token, check you pasted it correctly then try again.

  1. Open spotify and play a song. You can test your endpoint by visiting http://localhost:1337/api/currentplaying where it'll show you the track's name, author(s), a direct URL, albumn art URL from their CDN, and the playing state. (Screenshot was from MS Edge Devtools. You can use this if you find the output json in the browser hard to read properly) image

You now have a spotify endpoint to show on your website!

Running the API unattended

There are many different ways that you're able to run this unattended on your production instance, I reccomend using something like PM2, although other alternatives are available.

Deploying to your server

Deploying shouldn't be too much of a hassle, and chances are you'll be running this alongside your standard Apache/NGINX install. The guides below from DigitalOcean and IONOS should help!

Closing thoughts

If you need any extra help don't be afraid to raise an issue on Github. If you want to contribute at all for some reason, you're free to open a PR too :D

API endpoint and server security is your responsibility. This program comes with ABSOLUTELY NO WARRANTY.

About

A quick and easy way to display what your currently playing song on Spotify is on your personal website

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published