How to get recommendations based on multiple tracks?

Hello,

I am trying to get recommendations based on the identifier of multiple tracks. But I am unable to get any results

Here is the code I have used to try to get recommendations based on one album

let dataRequest = MusicDataRequest(urlRequest: URLRequest(url: URL(string: "https://api.music.apple.com/v1/me/recommendations/1571344275")!))

let dataResponse = try await dataRequest.response()

For this I am getting the response:

Failed to perform MusicDataRequest.Context(

  url: https://api.music.apple.com/v1/me/recommendations/1571344275,

  currentRetryCounts: [.other: 1]

) with MusicDataRequest.Error(

  status: 404,

  code: 40400,

  title: "Resource Not Found",

  detailText: "Resource with requested id was not found",

I have tried used the identifier of songs/albums, to no success. What am I doing wrong?

Accepted Reply

Hello @ashinthetray,

Thank you for your question about how to get recommendations from Apple Music API.

The reason you are getting this error from the endpoint to get a recommendation is because that endpoint doesn't support any random identifier. It only supports identifiers of a resource of type PersonalRecommendation. Such a resource would look like this in JSON format:

      {
        "attributes" : {
            [...]
        },
        "href" : "/v1/me/recommendations/6-27s5hU6azhJY",
        "id" : "6-27s5hU6azhJY",
        "relationships" : {
            [...]
        },
        "type" : "personal-recommendation"
    }

So, with my own library, I could load a PersonalRecommendation with this URL: https://api.music.apple.com/v1/me/recommendations/6-27s5hU6azhJY.

But you certainly can't use the ID of a resource of type Albums for this endpoint.

With the current state of Apple Music API, the closest thing you can do based on an album is to load its related albums.

With MusicKit for Swift, you can do that very easily:

    let album: Album = ...
    let detailedAlbum = try await album.with([.relatedAlbums])
    if let relatedAlbums = detailedAlbum.relatedAlbums, !relatedAlbums.isEmpty {
        print("Related albums for \(album): \(relatedAlbums)")
    } else {
        print("No related albums could be found for \(album).")
    }

Here's the output I see by running this code for a specific album:

Related albums for Album(id: "617154241", title: "Random Access Memories", artistName: "Daft Punk"): MusicItemCollection<Album>(
  title: "Related Albums",
  items: [
    Album(id: "628734125", title: "Politics", artistName: "Sébastien Tellier"),
    Album(id: "850697616", title: "XSCAPE (Deluxe)", artistName: "Michael Jackson"),
    Album(id: "545398133", title: "The Truth About Love", artistName: "P!nk"),
    Album(id: "623438311", title: "The Conversation (Deluxe Version)", artistName: "Texas"),
    Album(id: "1010891982", title: "Freedom - Single", artistName: "Pharrell Williams"),
    Album(id: "1046165664", title: "Zanaka", artistName: "Jain"),
    Album(id: "882945378", title: "1000 Forms of Fear", artistName: "Sia"),
    Album(id: "118413751", title: "Everything Is Wrong", artistName: "Moby"),
    Album(id: "79018195", title: "Amoureuse", artistName: "Véronique Sanson"),
    Album(id: "482734112", title: "Up All Night (Deluxe Version)", artistName: "One Direction")
  ],
  hasNextBatch: true
)

That said, not every album has a non-empty collection of related albums; it's my understanding that this data is partly driven by our editorial teams.

I just checked the album you were interested in, and it doesn't have any related albums, nor does any other album from that same artists.

I hope this helps.

Best regards,

  • As always, thanks for your reply!

Add a Comment

Replies

Hello @ashinthetray,

Thank you for your question about how to get recommendations from Apple Music API.

The reason you are getting this error from the endpoint to get a recommendation is because that endpoint doesn't support any random identifier. It only supports identifiers of a resource of type PersonalRecommendation. Such a resource would look like this in JSON format:

      {
        "attributes" : {
            [...]
        },
        "href" : "/v1/me/recommendations/6-27s5hU6azhJY",
        "id" : "6-27s5hU6azhJY",
        "relationships" : {
            [...]
        },
        "type" : "personal-recommendation"
    }

So, with my own library, I could load a PersonalRecommendation with this URL: https://api.music.apple.com/v1/me/recommendations/6-27s5hU6azhJY.

But you certainly can't use the ID of a resource of type Albums for this endpoint.

With the current state of Apple Music API, the closest thing you can do based on an album is to load its related albums.

With MusicKit for Swift, you can do that very easily:

    let album: Album = ...
    let detailedAlbum = try await album.with([.relatedAlbums])
    if let relatedAlbums = detailedAlbum.relatedAlbums, !relatedAlbums.isEmpty {
        print("Related albums for \(album): \(relatedAlbums)")
    } else {
        print("No related albums could be found for \(album).")
    }

Here's the output I see by running this code for a specific album:

Related albums for Album(id: "617154241", title: "Random Access Memories", artistName: "Daft Punk"): MusicItemCollection<Album>(
  title: "Related Albums",
  items: [
    Album(id: "628734125", title: "Politics", artistName: "Sébastien Tellier"),
    Album(id: "850697616", title: "XSCAPE (Deluxe)", artistName: "Michael Jackson"),
    Album(id: "545398133", title: "The Truth About Love", artistName: "P!nk"),
    Album(id: "623438311", title: "The Conversation (Deluxe Version)", artistName: "Texas"),
    Album(id: "1010891982", title: "Freedom - Single", artistName: "Pharrell Williams"),
    Album(id: "1046165664", title: "Zanaka", artistName: "Jain"),
    Album(id: "882945378", title: "1000 Forms of Fear", artistName: "Sia"),
    Album(id: "118413751", title: "Everything Is Wrong", artistName: "Moby"),
    Album(id: "79018195", title: "Amoureuse", artistName: "Véronique Sanson"),
    Album(id: "482734112", title: "Up All Night (Deluxe Version)", artistName: "One Direction")
  ],
  hasNextBatch: true
)

That said, not every album has a non-empty collection of related albums; it's my understanding that this data is partly driven by our editorial teams.

I just checked the album you were interested in, and it doesn't have any related albums, nor does any other album from that same artists.

I hope this helps.

Best regards,

  • As always, thanks for your reply!

Add a Comment