Control Spotify from a REST api and python

Odysseas Lamtzidis
Nov 7 · 2 min read

Disclaimer: Originally published in 2017 in my personal blog.

In this blog post I want to talk about my first shareable python script.

It’s a little flask server that enables you to control the spotify client of the device on which you are running the server via simple REST calls.

This can be quite useful if you want to control a spotify device without using spotify connect, as when you want to automate tasks and use scripts.

How does it work?

While surfing the Internet I discovered a blog post. It talked about an http-server behind every spotify client.
Apparently, in order to use the spotify-play button, found on many websites, there is a “secret” server that is listening for commands. So, all you have to do is to find the proper requests and make them.

Thankfully, the blogger had done the job for us, creating a simple script that builds the right tokens and send the proper commands.

And when I thought that I had finally found a way to automate my spotify device (after searching for a year for a method), I encountered yet another obstacle.

To my surprise,the spotify server was only accessible from the computer from which was running, i.e localhost.

But, then it hit me again. Why not deploy my own REST server on the same device, that will listen for commands that I define and then simply make proper requests. The concept was quite easy, thanks to [flask](http://flask.pocoo.org/).

Flask

Flask is a cool micro-framework for web development. You can create simple web applications in a breeze, and for the needs of the project, the default development server that comes bundled is more than enough.

Some Code:

The core of every flask app is the decorator @app.route(‘string’) that decorates the function that will run each time the string route is accessed.

If we type http://ip.of.server:port/play in the browser , we activate the function under @app.route(/play).

When we enter the play command we must also put the URI of the playlist/song that we want to play. If `uri` is empty, meaning I typed `/play` and not `/play/uri`, it return an error.

Finally, before using the server we need to generate the tokens and be sure that our spotify client is up and running. The tokens that are generated, remain as attributes of the `client` instance and are used in the communication with the spotify server.

Well, better not to dwell in the spotify script, it simply defines the right structure of the requests that spotify needs in order to be controlled. There should be more commands , like /shuffle.json, but I still haven’t looked into it.

Please do feel free to experiment and enrich the server with more commands!

https://github.com/OdysLam/spotify-local-http-api

Cheers!

Odysseas Lamtzidis

Written by

I focus on solving problems that affect the quality of everyday life and empowering people.Researching interconnection of Blockchain + IoT. // Go to: odyslam.me

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade