I’m working on a small project where I need to get information from the Spotify app running on my machine.
Basically I want to grab things like:
- What track is currently playing
- The current playback position/timestamp
- Maybe some other playback details
I’m wondering if there’s a way to connect to the local Spotify client and pull this data programmatically. I’ve been looking around but haven’t found a clear solution yet.
Has anyone managed to do something similar? I’m hoping to use Ruby for this but I’m open to other approaches if needed. Any suggestions on APIs or methods that might work for accessing the local Spotify instance would be really helpful.
Thanks in advance for any tips or code examples you might have!
there’s a neat workaround using spotify’s local http server on port 4381. when spotify’s running, it exposes endpoints you can hit with simple http requests. try http://localhost:4381/remote/status.json - works without oauth but can be finicky and might not work on all versions. way easier than dealing with web api auth for quick projects.
I faced a similar issue when developing a dashboard a while back. It’s true that Spotify’s Web API requires authentication and doesn’t provide real-time data for playback on the local client. I opted for the Web Playback SDK along with the Connect API; this necessitates creating a web app. If you’re using Ruby, consider exploring system-level solutions. For macOS, using AppleScript can facilitate direct queries to Spotify via the scripting bridge, while for Windows, there are various unofficial libraries that interact with Spotify’s process, though they are often prone to breaking after updates. Also, monitoring Spotify’s cache files or employing desktop automation are options, but they can be unreliable for production use. In the end, I found that using the official Web API with a proper OAuth setup, despite its complexity, was the most dependable approach.
Spotify Web API is your best option, but you’ll need OAuth authentication first - bit of a pain to set up. After that, use the “Get Currently Playing Track” endpoint for real-time data like track info, progress, and playback state. I built this in Python a few months back and it worked great. Heads up though - users need Spotify Premium and must be playing music on a Connect API device. For Ruby, check out the “rspotify” gem - handles API calls and auth for you. The refresh token saves you from constant re-authentication once it’s running. Watch the rate limits - about 100 requests per minute, which should cover most personal projects.