r/ReverseEngineering • • 10d ago

I reverse-engineered Spotify's private API and turned it into an async Python library — no API key required

https://github.com/Aran404/SpotAPI
1.1k Upvotes

63 comments sorted by

77

u/Major-Ad-4196 10d ago • Bravo

Hey everyone!

I started reverse-engineering Spotify's web player when I was 15. What began as a small Python wrapper turned into a two-year project exploring how much of Spotify's functionality could be accessed without going through its official developer API.

The interesting part wasn't just finding undocumented endpoints. It was figuring out how to make them reliable enough for other developers to actually build on.

Some of the challenges:

  • Undocumented GraphQL queries: Spotify's web player uses queries whose identifiers can change between builds. SpotAPI discovers the current queries and caches them against the corresponding web-player build rather than relying entirely on hardcoded identifiers.
  • Authentication: Public catalogue data can be accessed without an account or API key, while private library operations and playback controls require an authenticated Spotify session.
  • Undocumented response contracts: Mapping responses into typed Python models, accounting for missing data, and keeping the public API stable despite changes upstream.
  • Playback control: Interacting with Spotify Connect devices through private interfaces rather than the official Web API.

I recently rewrote the whole library around asyncio. It's called SpotAPI, and I wanted to make it useful beyond just demonstrating that these endpoints exist.

If you want to play around with it, there's an interactive terminal browser:

pip install "spotapi[demo]"
spotapi discover "Daft Punk"

That opens a Spotify catalogue explorer in your terminal. No API key, account, Premium subscription, or configuration required.

Underneath it is the actual async Python library, which you can use for searches, metadata, recommendations, playlists, and authenticated player control.

One limitation worth mentioning: this doesn't bypass Spotify's DRM or stream audio. Playback controls an existing Spotify device.

Source: https://github.com/Aran404/SpotAPI

I'd especially love feedback from people who've reverse-engineered APIs that change frequently. How do you handle breaking changes in undocumented protocols without turning your codebase into a pile of workarounds?

And if you find the project useful or interesting, consider giving it a star. It helps more developers discover the work.

Thanks for checking it out!

19

u/therainpocket 9d ago

Love this. Great work. we are building a music engine for fun and my friends sent me this. Not a lot to give back to you for your expertise but maybe you see something that inspires you. we are pretty specific about the genre but have visions to build it for any fans choice. if I blew some protocol about messaging in a post, I apologize. My name is Doug. Looking forward to hearing all about it and seeing if there's any advice you'd have for us.

9

u/Major-Ad-4196 9d ago

Hey Doug! No worries at all about the messaging etiquette, I actually love hearing about projects like this. And don’t worry about giving anything back. I built SpotAPI because I enjoy working on this stuff, and seeing people use it to build something cool is already rewarding enough.

I’d genuinely love to check out what you and your friends are building! What genre are you focusing on, and how does your music engine work so far? If you’ve got a GitHub repo or even a demo, send it over. I’d be happy to take a look, share some ideas, and see if there’s anything SpotAPI could help you with!

3

u/therainpocket 9d ago

@therainpocket on IG. Hoping to show you what’s up and have you peak at the logic I got going. It that’s too much I share links. Just a little embarrassed to show publicly.

3

u/Major-Ad-4196 9d ago

No need to be embarrassed at all! Everyone starts somewhere, and I'd genuinely love to see what you've been working on. Feel free to send me a Reddit DM with whatever you're comfortable sharing, whether that's a GitHub repo, some code, or just an explanation of the logic. No judgment here, just happy to exchange ideas and help out where I can!

0

u/therainpocket 9d ago

Why is dm so hard here lol

0

u/therainpocket 9d ago

I found the spot and it doesn’t let me send. Do you dm other places?

2

u/therainpocket 9d ago

I am starting to love Reddit!!!

1

u/scillicet 3d ago

Wow! I wonder if they realize people are doing this... Or if they care 😅 Waiting for the comment "hi I work for Spotify..."

60

u/SigridFischer42 10d ago • Bravo

Set a custom User-Agent header to avoid getting blocked when using /api/v1/access/.

8

u/Major-Ad-4196 9d ago

Wreq sets custom User-Agent and browser-dependent headers automatically to match the TLS profile that is being used. We even parse the user-agent with Spotify’s UAparser (transpiled to python), so that values that are a function of the browser variables can be dynamically generated (i.e, “os”, “browser”, etc.)

18

u/Salamandar3500 9d ago

This project is still up 9 hours after ? Github DMCA takedowns are slow these days.

20

u/strutb 9d ago

Clean room reverse engineering is fully legal.

13

u/RavenousTitan818 9d ago

When has that ever stopped a company from trying?

6

u/eagle33322 9d ago

nintendo would like a word

3

u/Major-Ad-4196 8d ago

Hey, you stole u/Salamandar3500 joke!

10

u/Kopachris 9d ago

I don't think this counts as clean room. But it also probably wouldn't be considered a derivative work of Spotify's copyrightable code anyway, so clean room to avoid copyright infringement doesn't even matter here. At most, this is a violation of Spotify's TOS, which isn't a copyright issue, just a contract issue. This doesn't even bypass Spotify's DRM.

IANAL, but very familiar with US copyright law.

3

u/strutb 9d ago

Yeah, you're correct, it's not clean room if it was solely him looking directly at it. Thanks for pointing that out.

2

u/zunjae 7d ago

Who cares if it’s legal or not lol

You can file a DMCA even if the content itself is legal

0

u/Salamandar3500 9d ago

Nintendo enters the chat

1

u/CompetitiveSpot2643 7d ago

isnt there a several years old project that lets you completely decrypt downloaded spotify music files lol

2

u/Major-Ad-4196 7d ago

There's many, in-fact you could probably find a few deeply hidden in high level Github projects (since Spotify really only goes for direct tools that advertise bypassing of DRM like SpotiFLAC). It's quite an easy process, any LLM could do it within seconds to minutes.

1

u/notPlancha 8d ago

Spicetify has the graph ql documented for a while now

0

u/Major-Ad-4196 9d ago

Haha, trust me, that was one of my concerns too 😭. Another project that distributed the TOTP related secrets received a DMCA takedown at just 45 stars. We learned from that and took a different approach: SpotAPI doesn’t ship those secrets. Instead, it derives the necessary bytes locally and dynamically for each user at runtime.

We’ve also included a legal notice and tried to be careful about what we actually distribute. Obviously, none of that makes us immune to legal challenges, but the goal is to build a useful open source library, not redistribute Spotify’s proprietary assets. Hopefully Spotify sees the value in what the community is building rather than sending us a love letter from their legal team.

6

u/adzm 9d ago

If only Spotify would show the bpm and keys without me having to make a new playlist and turn on mix mode. This is pretty useful even if just for that

1

u/Major-Ad-4196 9d ago

I'm glad you found this project useful, you can try it out of the box with no configuration/signup required: https://github.com/Aran404/SpotAPI/blob/main/examples/mix_map.py

6

u/Secret-Quit5400 8d ago

Wait, so you can actually sync lyrics with Spotify and click on a line to jump to that part of the song? That's pretty cool lol. Does that work without Premium too? Curious how you got that working.

3

u/Major-Ad-4196 8d ago

Yep! And the cool part is it can work without Premium. Studio pulls timestamped lyrics from LRCLIB, syncs them with your live playback, and clicking a line sends a seek command to your Spotify player. We reverse-engineered Spotify's private playback protocol to make that possible without relying on their official API. Only catch is that if LRCLIB doesn't have the lyrics, Spotify's own lyrics endpoint requires Premium. Took quite a bit of digging to get it all working.

1

u/CompetitiveSpot2643 7d ago

does it also let you modify the lyrics?

1

u/Major-Ad-4196 7d ago

Not directly, at least not yet. SpotAPI can fetch and sync timestamped lyrics, but editing them would require saving a custom version locally since we can't modify Spotify's or LRCLIB's lyrics directly. Could be a neat feature to add though.

3

u/CXgamer 8d ago

I have multi room audio controles by HomeAssistant but without a subscription to a streaming service, best I could do was a web radio.

I'll be trying this out later! Hopefully I'll finally have a good audio source with this!

2

u/Major-Ad-4196 8d ago

Oh that's a cool setup, you're actually not the first person who's mentioned using SpotAPI with Home Assistant! Just a heads up, SpotAPI can control existing Spotify players without Premium, but it doesn't stream audio itself. If you're looking for a free audio source, Music Assistant with a local library might be worth checking out. SpotAPI could still be useful for music discovery and playback controls though! Curious what speakers you're using for your setup.

2

u/mirh 9d ago

Wasn't librespot-python enough?

2

u/Major-Ad-4196 9d ago

Different goals! librespot-python focuses on Spotify Connect and streaming, while SpotAPI focuses on interacting with Spotify’s APIs, from search and metadata to playback controls, without needing API keys. They’re more complementary than competing projects :)

-1

u/p1-o2 9d ago

Nah gotta reinvent the wheel a few dozen more times.

2

u/Either-Banana-7323 9d ago

Super cool project man. Thanks for sharing it

1

u/Major-Ad-4196 8d ago

Thank you man, means a lot.

2

u/kiwipaul17 7d ago

Will try

1

u/Major-Ad-4196 7d ago

Let me know how that goes, be sure to try out the examples/CLI!

2

u/MyNameIsCheesus 7d ago

Dumb question, what does this do that the Spotify app doesn't? Beside for home assistant. Also, you said no API token needed, does that mean anyone can play music bumping into ads?

3

u/Major-Ad-4196 7d ago

Not a dumb question at all! That's actually one of SpotAPI's biggest selling points. You can grab pretty much any public Spotify data without even logging in, and SpotAPI gives it back as clean, typed Python objects you can immediately use in your code.

The cooler part is that Spotify normally requires Premium to control playback through their official API (skipping, pausing, seeking, etc.). I reverse-engineered their private API to get around that limitation, so you can do all of that without Premium.

One thing worth mentioning though: SpotAPI doesn't actually stream or download music, or even touch the audio itself. You still need Spotify's web player or desktop app playing the music. SpotAPI just lets you control it and interact with Spotify without all the usual API restrictions :)

2

u/cat_dev_null_sync 9d ago

So now Spotify has a reason to lock this down?

2

u/Major-Ad-4196 9d ago

Fair concern, but Spotify has always had the ability to change its private APIs. SpotAPI has been an almost 3-year project, and these endpoints aren’t exactly a secret. I’m just making them more accessible to developers.

1

u/cat_dev_null_sync 9d ago

Makes sense. Sounds like this won't be like YouTube vs ad blockers.

1

u/Major-Ad-4196 9d ago

Yeah I don't think Spotify pays it's developers well or something, you should've seen the things I've encountered when working with Spotify's unofficial API. I remember a payload for some family request being {"foo": "bar"} and it was a genuinely important API endpoint, maybe they have some secret code at Spotify so it actually means something idk.

3

u/OtherwiseAlbatross14 9d ago

Maybe a reference to foobar2000?

1

u/Major-Ad-4196 9d ago

Well it does have to do with music, so maybe. Though it's not the only case of weird payload values for Spotify.

1

u/Major-Ad-4196 7d ago

For anyone curious what the reverse-engineered playback controls actually look like, here's a quick demo of SpotAPI Studio controlling a real Spotify player in real time. Track switching, playback controls, likes, and more, all without needing Spotify Premium. Pretty satisfying seeing both stay in sync.

1

u/scythe000 9d ago

Could I use this to show bpm in search results and random playlists?

1

u/Major-Ad-4196 9d ago

Yep, partially! SpotAPI already supports BPM and Camelot keys for tracks in public playlists, including ones you don't own. You can use Public.get_playlist_tracks() to build something that displays them, although Spotify doesn't always provide those values.

Search results are a little trickier since Spotify doesn't include BPM in the search response. You'd need to supplement that with another metadata source. There's actually a working example in the repo if you want to experiment: https://github.com/Aran404/SpotAPI/blob/main/examples/playlist_crate.py

1

u/d_e_g_m 5d ago

How are ads managed?

1

u/Major-Ad-4196 5d ago

Ads are still handled by Spotify itself. SpotAPI doesn’t stream audio or block ads, it just controls the existing Spotify player through its private APIs. So if you’re using a free account, you’ll still get ads as usual!

0

u/p1-o2 9d ago

Okay now make it stream audio too. I want to access Spotify without using Spotify app. I have a paid account.

Please? 🥺

4

u/Major-Ad-4196 9d ago

I’d probably get a DMCA take down within 24 hours. Has happened to many different github repositories before that bypassed DRM and downloaded music 🐸

2

u/kalilamodow 9d ago

not really possible without funny drm stuff or embedding a web view unfortunately.

0

u/endre_szabo 9d ago

and they downvote you

0

u/p1-o2 9d ago

Just reddit doing reddit things 

0

u/Luigi_Boy_96 7d ago

Even if you have a subscription, you don't get the right to legally obtain it via undocumented API. Especially, if they implement that, it'll be immediately taken down. The only thing that you can hope is to get a legal API with generatable API key. Or you add some endpoints yourself and download the files. There are hidden unadvertised tools in the internet/git-host-platforms.

0

u/[deleted] 9d ago

[deleted] — view deleted comment

1

u/Major-Ad-4196 9d ago

No it does not, that would be piracy and copyright infringement.

1

u/StringSentinel 9d ago

That makes sense. Thanks anyways