- Create An App With Spotify Api Plugin
- Create An App With Spotify Api App
- Create An App With Spotify Api Authentication
- Spotify Api Key
- Create An App With Spotify Api Download
- Spotify Api Data
The Spotify media player integration allows you to control Spotify playback from Home Assistant.
Prerequisites
- Spotify account
- Spotify application configured for Home Assistant (see below)
Spotify Web API for Android. This project is a wrapper for the Spotify Web API. It uses Retrofit to create Java interfaces from API endpoints. Integrating into your project. This library is available in JitPack.io repository. To use it make sure that repository's url is added to the build.gradle file in your app. Spotify’s Web API is optimized to support app integrations, but it can be used for data analysis purposes, and this article is going to walk through the basics of. Creating a Spotify app. Creating is fairly easy to be honest, you don't need a developer account - the only thing you need is your existing Spotify account. When you are planning to use the Spotify Web API, you are bound to this idea of authentication. Spotify itself has a blog article available that covers the authentication and authorization. I have read this question: How to create a playlist and get the URI of it in a spotify apps API? But I was wondering if now it's possible to do it, or if you can give me a solution, I really need the URI, could I create a public Playlist or sth similar? I need that to create a App similar to soundrop:) Thank you a lot.
Spotify integrated media controls (pause, play, next, etc.) require a Premium account.If you do not have a Premium account, the frontend will not show the controls.
Create a Spotify application
- Login to Spotify Developer.
- Visit the My Applications page.
- Select Create An App. Enter any name and description.
- Once your application is created, view it and copy your Client ID and Client Secret, which are used in the Home Assistant configuration file below.
- Enter the Edit Settings dialog of your newly-created application and add a Redirect URI:
- If you are not using SSL:
http://<your_home_assistant_url_or_local_ip>:<port>/auth/external/callback
- If you are using SSL:
https://<your_home_assistant_url_or_local_ip>:<port>/auth/external/callback
- If you are not using SSL:
- Click Save after adding the URI.
Your Home Assistant instance does not need to be exposed to the internet. It works just fine with local IP addresses.
Update: There is a now a way to play full tracks! Read more at Spotify's Developer site, and @arirawr's answer to this question. Previous answer: No, there's currently no way to play the full song using the Web API. If you want full tracks to be playable from a website, you can use the Spotify.
Create An App With Spotify Api Plugin
Configuration
To add Spotify to Home Assistant, add the following to your
configuration.yaml
file:Configuration Variables
Client ID from your Spotify Developer application.
Client Secret from your Spotify Developer application.
Activating the Spotify integration
![Api Api](https://raw.githubusercontent.com/markkohdev/spotify-api-starter/master/assets/spotify_api.png)
- Go to the Configuration page in the Home Assistant frontend.
- Click on Integrations.
- Click the orange + on the bottom-right.
- Click on “Spotify”.
- Once the new Spotify tab opens, enter your Spotify credentials and allow Home Assistant to access your Spotify account.
If you receive an
INVALID_CLIENT: Invalid redirect URI
error while trying toauthenticate with your Spotify account, check the Redirect URI inthe address bar after adding the new integration. Compare this value with theRedirect URI defined in the Spotify Developer Portal.Using multiple Spotify accounts
This integration supports multiple Spotify accounts at once. You don’t need tocreate another Spotify application in the Spotify Developer Portal and nomodification to the
configuration.yaml
file is needed. Multiple Spotifyaccounts can be linked to a single Spotify application.To add an additional Spotify account to Home Assistant, go to the Spotify website and log out, then repeat only the stepsin the Activating the Spotify integration section.
Playing Spotify playlists
You can send playlists to Spotify using the
'media_content_type': 'playlist'
, which is part of themedia_player.play_media service, for example:The
media_content_id
value can be obtained from the Spotify desktop app by clicking on the more options (”…”) next to the album art picture, selecting “Share” and then “Copy Spotify URI” or “Copy Playlist Link” (also available in the Spotify phone and web app).Unsupported Devices
- Sonos: Although Sonos is a Spotify Connect device, it is not supported by the official Spotify API.
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.
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’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:
Create An App With Spotify Api App
![App App](https://miro.medium.com/max/1400/1*ytMWtgd5h-1EiGiCe_D3ew.png)
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.
Create An App With Spotify Api Authentication
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:
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.
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:
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.
Spotify Api Key
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
Create An App With Spotify Api Download
MVP
Spotify Api Data
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.