Book

Exploring MusicKit and Apple Music API

Unlock the full power of MusicKit & Apple Music APIs in your apps with the best guide! Use code musickit-blog for a limited-time 35% discount!

I have become a fan of Apple Music again. It never ceases to surprise me. Its recent introduction of the Discovery Station takes music discovery to Spotify’s level. Made for your preferences, it is an amazing way to find underrated songs and artists that vibe with your taste. If you are anything like me, who listens to music day and night and had a chance to immerse yourself in its offerings, you should most likely agree that it is one of the most amazing updates from Apple Music since its inception.

As a developer, you can fetch and integrate this station into your apps, like how I made a whole app for it, Fussion. Let’s dive deeper into the Apple Music API and MusicKit to see how we can access and play the Discovery Station.

Fetching Discovery Station Using Apple API

To fetch the user’s Discovery Station, we use the following endpoint:

https://api.music.apple.com/v1/me/recommendations

One of the recommendations should contain the Discovery Station. The station response will look like the following JSON structure:

{
  "id": "ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm",
  "type": "stations",
  "href": "/v1/catalog/in/stations/ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm",
  "attributes": {
    "isLive": false,
    "name": "Discovery Station",
    "mediaKind": "audio",
    "artwork": {
      "width": 4320,
      "height": 1080,
      "url": "https://is1-ssl.mzstatic.com/image/thumb/Features126/v4/11/ad/7b/11ad7bad-df4f-42d1-4235-996800055de7/661dbb46-6f09-40f9-8789-7591387fdb93.png/{w}x{h}sr.jpg",
      "bgColor": "300a78",
      "textColor1": "ffffff",
      "textColor2": "77e4f4",
      "textColor3": "7bb4db",
      "textColor4": "68b2d9"
    },
    "playParams": {
      "id": "ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm",
      "kind": "radioStation",
      "format": "tracks",
      "stationHash": "CgwIByIICIDRuo8fGAIQAQ",
      "hasDrm": false,
      "mediaType": 0
    },
    "url": "https://music.apple.com/in/station/discovery-station/ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm"
  }
}

Fetching Discovery Station Using MusicKit

While you can use the Apple Music API to get the station, it will require manual decoding. For iOS 16+, you can use MusicKit instead. You start with fetching the personal recommendations provided by Apple Music. These recommendations have various music items, from albums to stations, personalised for the user. We construct a MusicPersonalRecommendationsRequest:

var request = MusicPersonalRecommendationsRequest()
let response = try await request.response()
let recommendations = response.recommendations

The recommendations here is an instance of MusicItemCollection<MusicPersonalRecommendation>. This gives us a music item collection, with collections of albums, playlists, and stations.

Filtering the Stations

Having accessed the recommendations, the next step is extracting stations from them:

let allStations = recommendations.reduce(into: MusicItemCollection<Station>()) { $0 += $1.stations }

This gives us an instance of MusicItemCollection<Station>. From these stations, the goal is to filter the Discovery Station:

guard let discoveryStation = allStations.first { $0.name == "Discovery Station" } else { return }

print(discoveryStation.debugDescription)

If all goes well, discoveryStation should have our Discovery Station! I have tried it with different locales, like the one in Japan and Taiwan, to see if the naming changes according to their language; fortunately, it is still named Discovery Station.

Printing the debugDescription of the station provides us with this information:

Station(
  id: "ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm",
  name: "Discovery Station",
  isLive: false,
  url: "https://music.apple.com/in/station/discovery-station/ra.q-GAI6IDA0YmNhYjA1YmI4MThlY2YxYWJiNzBmZjc1N2FhOTVm"
)

Playing the Discovery Station

Now, with our Discovery Station at hand, play it links:

do {
    guard let station = discoveryStation else { return }
    ApplicationMusicPlayer.shared.queue = [station]
    try await ApplicationMusicPlayer.shared.play()
} catch {
    debugPrint(error)
}

Conclusion

Apple Music’s Discovery Station is a delight for me. I love discovering new music every day now. With MusicKit, fetching and playing the station is as easy as possible.

Happy coding, and keep the station playing!

Book

Exploring MusicKit and Apple Music API

Unlock the full power of MusicKit & Apple Music APIs in your apps with the best guide! Use code musickit-blog for a limited-time 35% discount!

Written by

Rudrank Riyam

Hi, my name is Rudrank. I create apps for Apple Platforms while listening to music all day and night. Author of "Exploring MusicKit". Apple WWDC 2019 scholarship winner.