Exploring MusicKit: User’s Personal Apple Music Station
I am working on a new app FusionMix, that revolves around creating the same “Blend for Spotify” experience for Apple Music users. I am inclining towards the user’s personal Apple Music station to achieve it.
Working with the station is straightforward. Apple Music API has a dedicated endpoint, and we can use MusicKit to parse the station data.
User’s Personal Apple Music Station Endpoint
To fetch the current user’s personal Apple Music station, we use the following endpoint:
https://api.music.apple.com/v1/catalog/{storefront}/stations?filter[identity]=personal
The following endpoint returns an array of stations, and we can get the station data we want.
For example, this is what my station response looks like:
{
"data": [
{
"id": "ra.u-04bcab05bb818ecf1abb70ff757",
"type": "stations",
"href": "/v1/catalog/in/stations/ra.u-04bcab05bb818ecf1abb70ff757",
"attributes": {
"isLive": false,
"name": "Rudrank Riyam’s Station",
"mediaKind": "audio",
"artwork": {
"width": 2400,
"height": 2400,
"url": "https://is1-ssl.mzstatic.com/image/thumb/Features124/v4/7b/1d/f0/7b1df048-0017-8ac0-98c9-735f14849606/mza_7507996640781423701.png/{w}x{h}bb.jpg"
},
"url": "https://music.apple.com/in/station/rudrank-riyams-station/ra.u-04bcab05bb818ecf1abb70ff757",
"playParams": {
"id": "ra.u-04bcab05bb818ecf1abb70ff757",
"kind": "radioStation",
"format": "tracks",
"stationHash": "CgoIByIGCIDRuo8f",
"hasDrm": false,
"mediaType": 0
}
}
}
],
"meta": {
"filters": {
"identity": {
"personal": [
{
"id": "ra.u-04bcab05bb818ecf1abb70ff757",
"type": "stations",
"href": "/v1/catalog/in/stations/ra.u-04bcab05bb818ecf1abb70ff757"
}
]
}
}
}
}
We can see that the data contains an array of Station
objects. To use this in your Swift project with MusicKit, we use MusicDataRequest
to decode it to MusicItemCollection<Station>
structure.
Get User’s Personal Apple Music Station with MusicKit
We define an enum
for potential errors, we might encounter:
enum PersonalStationError: Error {
case invalidURL
case notFound
}
This enum
includes two cases: invalidURL
and notFound
, which represent possible issues we might run into while fetching the personal music station.
Next, we fetch the current country code which we use as a storefront
:
let storefront = try await MusicDataRequest.currentCountryCode
After we have the storefront, we build the URL request using URLComponents
and append the storefront
to the path and include a query item to filter the personal station:
var urlComponents = URLComponents()
urlComponents.scheme = "https"
urlComponents.host = "api.music.apple.com"
urlComponents.path = "/v1/catalog/\(storefront)/stations/personal"
urlComponents.queryItems = [URLQueryItem(name: "filter[identity]", value: "personal")]
Then, we use the MusicDataRequest
that handles both the authorisation and music user token for us:
guard let url = urlComponents.url else {
throw PersonalStationError.invalidURL
}
let request = MusicDataRequest(urlRequest: URLRequest(url: url))
let response = try await request.response()
After getting the response, we decode the data into a collection of MusicKit’s Station:
let stations = try JSONDecoder().decode(MusicItemCollection<Station>.self, from: response.data)
Lastly, we get the first station from our stations
list:
guard let personalStation = stations.first else {
throw PersonalStationError.notFound
}
debugPrint(personalStation)
That’s it! This fetches your personal Apple Music station using MusicKit.
Printing the station to the console, I get the following for my station:
Station(
id: "ra.u-04bcab05bb818ecf1abb70ff757",
name: "Rudrank Riyam’s Station",
isLive: false,
url: "https://music.apple.com/in/station/rudrank-riyams-station/ra.u-04bcab05bb818ecf1abb70ff757"
Play User’s Personal Apple Music Station with MusicKit
To play this station, you need to set the queue of the player and play it:
do {
guard let station = station else { return }
ApplicationMusicPlayer.shared.queue = [station]
try await ApplicationMusicPlayer.shared.play()
} catch {
debugPrint(error)
}
Conclusion
Using MusicKit for fetching a user’s personal Apple Music station is simple. However, remember to handle potential errors appropriately and give clear feedback to your users.
Happy coding, and keep the station playing!