Playing with the Spotify Connect API

José M. Pérez

José M. Pérez / April 22, 2017

6 min read4,765 views

Spotify released recently a set of endpoints in beta to fetch information of what is playing and send playback commands. This allows for a wide range of integrations and I wanted to hack a bit with it.

Spotify Connect

What is Spotify Connect

Spotify Connect is a way to transmit the playback from one device to another one without having to use a physical connection like a cable or bluetooth. You can send the music from your Spotify desktop client to a speaker, from the Spotify app on mobile to Spotify for PlayStation, from the Spotify web player to Chromecast… In short, you have controllers and devices that can play music.

Spotify Web Player

Spotify's Web Player (zoomed in). Clicking on the icon next to the volume we get a list of connected devices (FireTV, desktop client, speakers and the web player).

Your application can become a controller through the Web API endpoints, getting information about what is playing currently and from where, being able to transmit the playback to another connected device or interact with the current context (pausing, changing the volume, skipping, playing something else…).

How to use the endpoints

Before using the Connect endpoints we need to obtain an access token on behalf of the user with certain permissions. There is more information about what scopes are needed in the documentation for each endpoint.

You don't need to have a premium account to get the playback status, a free account is alright. You will do need a premium one if you want to send commands to change the playback.

A caveat at the moment is that the endpoints don't support any kind of web socket connection nor long polling. Thus, if you want to get updates on the position of the current playing track or any other change in the context, you need to poll every few seconds.

A small library to make it easier to use the endpoints

The trickiest part of using the Spotify Web API is to implement the authorization flow. The Authorization Guide does a good job explaining it, but I thought I could do something so developers wouldn't need to worry about setting up the whole flow, hiding away the authentication and just getting.

That's why I have created spotify-player. It's both a server and a library that you use to communicate with it. To use it, you just need to include a script, call login() and subscribe to the updates:

<script src="https://spotify-player.herokuapp.com/spotify-player.js"></script>
var spotifyPlayer = new SpotifyPlayer();

spotifyPlayer.on('update', response => {
  // render the track received
});

spotifyPlayer.on('login', user => {
  if (user === null) {
    // no user
  } else {
    // say hello to the user, and tell them to play something!
  }
});

spotifyPlayer.init();

spotifyPlayer.login();

You can forget about setting up a Spotify application and a server, carrying out the token exchange, token refresh, and persisting the current user, so you can focus on the fun part.

Other methods include a function to make calls to other Spotify endpoints reusing the same access token, so you can fetch other data that can help you creating a more complete visualisation.

Let's have a look at this pen as an example of a basic visualisation:

And in case you can't try it or don't have a Spotify account this is pretty much what it looks like:

Example of a visualisation of Spotify

See the Pen on Codepen

I have kept the example very basic since the point is understanding the usage of the library. If you are into performance and UX you'll see there is room for improvement, but as a front-end developer I know an example can get out of hand very easily when adding things.

I encourage you to fork it and start making your own visualisation. And once you do it, ping me so I include it in this Codepen collection.

Possible applications

Disclaimer: This is a list of some use cases. You still need to comply with Spotify's terms of use when implementing an application that uses Spotify'sWeb API. This might mean adding certain messaging and link to the song in Spotify.

Dynamic visualisations

You could combine the playback position with the audio analysis of the track to generate dynamic visualisations using loudness, tempo, key, timbre or pitch of the segments that compose the track. You can also use the endpoint to fetch audio features of a track, which gives you high level information about characteristics of the song.

See the Pen on Codepen and Possan's original visualisation on GitHub

Now playing view

Are you a coffee-shop owner and people always wonder what song is playing? You could have a TV showing a branded now playing view. You could even have a widget on your website, or a script posting to a social network what is currently playing so your customers know. Or a small browser extension showing what is playing and/or showing desktop notifications when the track changes.

Spotify Visualisation showing Web Notifications when the track changes

See the Pen on Codepen. When the track changes the browser shows a Web Notification.

Or if you are at home hosting a dinner or party, show on the TV or computer what is playing.

You can combine other Spotify Web API endpoints too. Eg you could fetch the artists info to show the artist profile image in the background:

Spotify Visualisation showing the artist profile image in the background

See the Pen on Codepen. A simple visualisation of what's playing in Spotify rendering the artist profile image as the background.

You could use an API like Musixmatch's and create an app or a website showing the lyrics for the current song, synchronised with the playback position.

And your imagination is the limit. Use Genius' API to get annotations about a song, or search for trivia and more info about the song or artist using Wikipedia's API. And if you don't want to miss what's happening in the world, implement a news ticker in your view using News API.

See the Pen on Codepen

MVP

The library is really small and it only supports reading the playback state and not send commands. Let's call it an MVP. I might work on adding more features in the future.

两个鬼故事企业管理咨询有限公司起名张姓名字大全女孩起名杨思起名女孩市政公司起名大全字库天净沙秋思翻译看不见的客人下载快穿之收视女王起名网免费取名周易起名78年属马公司起名红盟网络淘客助手阿华田是什么印章软件尾行3解3d马赛克补丁生辰八字起名测名打分海字起名字大全办公室恋情特种军官的娇妻宝宝起名女朱姓起名纸巾商标起名中国女人人体艺术香江大亨凯立德激活码生成器带金字旁的公司起名网上花钱起名靠谱不哪家装修公司口碑好十大家装网店起名大全2021最新版的新华网评5天走失58名儿童冒险王卫斯理之蓝血人少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化