SoundCloud Api or as an example of “bad” documentation

1. Free

2. Convenient (at least I thought so at the beginning)

The choice has been made, which means it’s time to read documentation and make a couple of get, post requests. For basic functionality like downloading and searching for music, only 3 requests are required. But if it were known right away which way to send the request, what parameters should be present in the request, etc., then this article would not have been published. As I already said, I read the documentation and found out a few things:

1. This is what a request looks like search

2. You can’t just download a track from the SoundCloud api (for example, in .mp3 format)

Looking ahead (since this article is not about this topic), problem 2 can be solved using FFmpeg. The fact is that, although the SoundCloud Api does not support downloading, it allows you to use streaming(that is, the .m3u8 format). Accordingly, we can download music as such a file and convert it using FFmpeg.

ffmpeg -i "ссылка полученная с 3 запроса" -c copy -f mp3 "путь к папке куда будете сохронять + имя.формат" 

So far everything is going perfectly until the first test request.

As you can see, the server returned 403. I started searching on various forums why this was happening and found out that the SoundCloud api has 2 versions. They differ not much, except that in the second version there are no endpoints dedicated to obtaining oauth tokens. But the fact that version 2 is not documented in any way forced:

A) ,,get angry and swear''

B) It takes a long time to search through different forums for snippets of the endpoints I need.

During the search, in addition to the fact that I found the endpoints I needed, I was able to find out that the second version of the api is not documented because it is still in development (since 2019) and may change. More details here.

After you receive the oauth token on the site, you can safely create the following requests (ps I use Postman and all get requests):

To search:

https://api-v2.soundcloud.com/search?q=название трека&limit=количество ответов

In response, you will receive json in which you need the following block:

"media": {
                "transcodings": [
                    {
                        "url": "тут будет нужный вам uri",
                        "preset": "",
                        "duration": "",
                        "snipped": false,
                        "format": {
                            "protocol": "hls",
                            "mime_type": "audio/mpeg"
                        },
                        "quality": ""
                    },
                   
                ]
            },

Next, use the resulting uri to get another uri:

And now use this Uri for the command in ffmpeg

ffmpeg -i "ссылка полученная с 2 запроса" -c copy -f mp3 "путь к папке куда будете сохронять + имя.формат" 

Congratulations, you have the track you need.

In the end, I would like to say that I sincerely hope that I could help someone.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *