Search code examples
iosswiftapple-musickit

Is it possible to get track previews using MusicKit


I want to integrate Apple Music into my app, however I don’t want people to have to log in to Apple Music. I just want to get the preview tracks of songs.

Is that possible?


Solution

  • It is possible to use the Apple Music API to retrieve a song's 30-second preview without the user needing to log in.

    1. Get a developer token

    2. Issue a request for a song

    3. Parse the response to get a Song previews URL

    4. Playback the song with a streaming media player (e.g. Howler)

    Example with cURL:

    curl -X GET \
      'https://api.music.apple.com/v1/catalog/us/search?term=drake&types=songs&limit=1' \
      -H 'Authorization: Bearer eyJhbG...'
    
    {
        "results": {
            "songs": {
                "href": "/v1/catalog/us/search?limit=5&term=drake&types=songs",
                "next": "/v1/catalog/us/search?offset=5&term=drake&types=songs",
                "data": [
                    {
                        "id": "1384782182",
                        "type": "songs",
                        "href": "/v1/catalog/us/songs/1384782182",
                        "attributes": {
                            "previews": [
                                {
                                    "url": "https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview125/v4/f5/bc/9e/f5bc9e9c-d62b-332b-04c4-061018d060fc/mzaf_7200764193324270096.plus.aac.p.m4a"
                                }
                            ]
    ...
    

    You can play the preview using the browser if you want to:

    https://audio-ssl.itunes.apple.com/apple-assets-us-std-000001/AudioPreview125/v4/f5/bc/9e/f5bc9e9c-d62b-332b-04c4-061018d060fc/mzaf_7200764193324270096.plus.aac.p.m4a