How to get track ID from URL using the SoundCloud API
Asked Answered
S

6

18

How can I get a track ID from a track URL using the SoundCloud API?

Stenopetalous answered 10/10, 2014 at 0:9 Comment(1)
Related, for those who are seeking to do the inverse: #29496275Kobayashi
S
41

You didn't specify a language in your question, so I'm just going to provide a high-level answer.

Use the resolve endpoint. I'm using this URL as an example:

https://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix

HTTP GET: https://api.soundcloud.com/resolve.json?url=https%3A%2F%2Fsoundcloud.com%2Fmsmrsounds%2Fms-mr-hurricane-chvrches-remix&client_id=[your_client_id]

The resulting JSON:

{
  "status": "302 - Found",
  "location": "https://api.soundcloud.com/tracks/147986343.json?client_id=[YOUR client_id]"
}

That numerical value is the track_id. You can just stop here or hit that endpoint you just got back for more information:

{
  "kind": "track",
  "id": 90787841,
  "created_at": "2013/05/05 00:15:40 +0000",
  "user_id": 7853935,
  "duration": 188333,
  "commentable": true,
  "state": "finished",
  "original_content_size": 35031144,
  "last_modified": "2014/09/30 05:53:10 +0000",
  "sharing": "public",
  "tag_list": "MSMR CHVRCHES \"Secondhand Rapture\" Remix Hurricane",
  "permalink": "ms-mr-hurricane-chvrches-remix",
  "streamable": true,
  "embeddable_by": "all",
  "downloadable": true,
  "purchase_url": null,
  "label_id": null,
  "purchase_title": null,
  "genre": "",
  "title": "MS MR - Hurricane (CHVRCHES remix)",
  "description": "MS MR - Hurricane (CHVRCHES remix)\r\n\r\n\r\n",
  "label_name": "",
  "release": "",
  "track_type": "",
  "key_signature": "",
  "isrc": "",
  "video_url": null,
  "bpm": null,
  "release_year": null,
  "release_month": null,
  "release_day": null,
  "original_format": "wav",
  "license": "all-rights-reserved",
  "uri": "https://api.soundcloud.com/tracks/90787841",
  "user": {
    "id": 7853935,
    "kind": "user",
    "permalink": "msmrsounds",
    "username": "MSMR",
    "last_modified": "2014/08/04 09:03:56 +0000",
    "uri": "https://api.soundcloud.com/users/7853935",
    "permalink_url": "http://soundcloud.com/msmrsounds",
    "avatar_url": "https://i1.sndcdn.com/avatars-000037198343-2lptmy-large.jpg?86347b7"
  },
  "permalink_url": "http://soundcloud.com/msmrsounds/ms-mr-hurricane-chvrches-remix",
  "artwork_url": null,
  "waveform_url": "https://w1.sndcdn.com/56LCadzDEBZn_m.png",
  "stream_url": "https://api.soundcloud.com/tracks/90787841/stream",
  "download_url": "https://api.soundcloud.com/tracks/90787841/download",
  "playback_count": 1672227,
  "download_count": 18494,
  "favoritings_count": 20426,
  "comment_count": 464,
  "attachments_uri": "https://api.soundcloud.com/tracks/90787841/attachments",
  "policy": "ALLOW"
}
Senhorita answered 10/10, 2014 at 0:47 Comment(3)
thank you so much... BTW, it is in PHP. I just figured it out using json_decode() function.Stenopetalous
Unfortunately, SoundCloud closed registration to their API around July 2017. This method is only valid for those who registered before then.Kobayashi
For further reference regarding API registration, as mentioned below by Hamza Zymawy, the application form currently links here, where it says "we will no longer be processing API application requests at this time".Cementite
S
8

Since the SoundCloud API is indefinitely not accepting new registrations, here's one alternative solution. This URL will provide you with a wide/short embedded player:

https://w.soundcloud.com/player/?url= + URL of track

Example: https://w.soundcloud.com/player/?url=https://soundcloud.com/chrisbjerken/picking-up-the-pieces

This works for both a single track or a playlist.

Sverige answered 12/4, 2019 at 17:14 Comment(0)
S
4

So my final code looks like this. Thank you guys for the help to get this working.

include_once('../Services/Soundcloud.php'); 
$client = new Services_Soundcloud('CLIENT_ID', 'CLIENT_SECRET');
$track_url=$_POST['trackurl'];
$track_url=str_replace("https://soundcloud.com/USERNAME/","",$track_url);
$track = json_decode($client->get("tracks/".$track_url));    
$track_id=$track->id;
echo $track_id;
Stenopetalous answered 27/1, 2015 at 23:58 Comment(0)
K
2

In The Meantime Writing This Answer The CLIENT_ID Is Close

Due to the high amount of requests recently received, we will no longer be processing API application requests at this time. We are working to re-evaluate our process to make it more efficient.

See Here

I Solved This Issue With My Laravel Project Like This

first I Made This Function To Get The Real Content

    public function getLessonUrl($lessonId = null)
{
    //Get the SoundCloud URL
    $validUrl = 'https://soundcloud.com/al-hazme';
    $result = stripos($lessonId, $validUrl);

    if (strpos($lessonId, $validUrl) !== false) {
        //Get the JSON data details with embed code from SoundCloud oEmbed
        $getValues = @file_get_contents('http://soundcloud.com/oembed?format=js&url=' . $lessonId . '&color=%234274e3&auto_play=false&hide_related=true&show_comments=false&show_user=false&show_reposts=false&show_teaser=true&show_artwork');

        if ($getValues === false) {
            return 'حدث خطأ اثناء تحميل الدرس نرجو منك التأكد من اتصال الانترنيت لديك او حدث الصفحة';
        }
        //Clean the Json to decode

        $decodeiFrame = substr($getValues, 1, -2);

        //json decode to convert it as an array

        $jsonObj = json_decode($decodeiFrame);

        //echo $jsonObj->html;

        $lessons = str_replace('height="400"', 'height="90"', $jsonObj->html);

        return $lessons;
    } else {
        return 'لايوجد استماع لهذا الدرس';
    }

}

Then I Made Another Function To Crap The Id Using Str Facade Methods

    use Illuminate\Support\Str;
    public function soundCloudId($url) {


    $do         = $lesson->getLessonUrl($lesson->url_link);
    $getTrickId = Str::after($do,'tracks%2F');
    $substr     = Str::substr($getTrickId,0,9);

     echo $substr = // 367893857
     }

It May Help Or Give You Clue

Kone answered 25/12, 2017 at 19:13 Comment(0)
C
1

I know this is kinda late but i faced the same issue yesterday and found a new (undocumented) endpoint that you can use.

https://api-widget.soundcloud.com/resolve?url=SOUNDLCOUD_URL&format=json&client_id=CLIENT_ID

You can find your CLIENT_ID by looking at the Network Inspector in the DevTools and select the XHR filter while browsing the SoundCloud website.

I think this is sufficient until the official APIs are usable again.

Cabal answered 7/9, 2020 at 11:6 Comment(2)
i get a 404 when i access this page...Ornithopter
@Ornithopter probably because of the latest api security changes developers.soundcloud.com/blog/security-updates-api.Cabal
J
0

I found a way to do it with PHP; may be a bit hacky and not be the most efficient but it works for me and doesn't require the (currently inaccessible) API.

function getTrackId($url) {
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'GET',
    ));
    $soundCloudResponse = curl_exec($curl);
    curl_close($curl);

    $scDocument = new DOMDocument();
    libxml_use_internal_errors(true);
    $scDocument->loadHTML(str_replace('\"', '"', $soundCloudResponse));
    libxml_use_internal_errors(false);
    try {
        $metaNodes = $scDocument->getElementsByTagName('meta');
        foreach($metaNodes as $metaNode) {
            if($metaNode->getAttribute('property') === "twitter:app:url:googleplay") {
                return explode(':', $metaNode->getAttribute('content'))[2];
            }
        }
    } catch(Exception $e) {
        return null;
    }
}
Jackdaw answered 15/9, 2023 at 19:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.