How to get resources for a Genre?

I am trying to display albums, playlists and artist for a Genre.

Does anyone know how to do this or if this is not currently supported by the MusicKit and Apple Music API?

Replies

Hello @ashinthetray,

One way you can access related content for a given Genre is by getting catalog charts using the genre query parameter. For example, you can easily get the most played songs or albums of a given genre using this technique.

I hope this helps.

Best regards,

What should be the best approach to decode the chart data for songs?

For example -

struct Charts: Decodable {
    let results: Songs
}

struct Songs: Codable {
    let songs: [ChartsSong]
}

struct ChartsSong: Codable {
    let data: [Song]
    let orderID, next, chart, name: String
    let href: String

    enum CodingKeys: String, CodingKey {
        case data
        case orderID = "orderId"
        case next, chart, name, href
    }
}

let url = URL(string: "https://api.music.apple.com/v1/catalog/us/charts?types=songs&genre=20&limit=1")!

let musicRequest = MusicDataRequest(urlRequest: URLRequest(url: url))

let response = try await musicRequest.response()


let data = try JSONDecoder().decode(Charts.self, from: response.data)

Right now, I'm using a custom structure to decode the data with the ChartsSong struct. Do you think there's a possibility of a Chart item in the future? Should I file feedback stating my use case?

Hello @snuff4,

Thanks for the feedback.

Yes, you should definitely file a new ticket on Feedback Assistant asking for new public API for getting charts. Please include some information about your use-case, which will help us to prioritize the work.

Best regards,

Hello @ashinthetray and @snuff4,

We have introduced a new structured request in iOS 16 beta 1 for fetching Apple Music catalog charts in Swift: MusicCatalogChartsRequest.

It's now easier than ever to load catalog charts with MusicKit, and, of course, you can check if a given chart’s items collection has a next batch, and load it using nextBatch(…).

I hope this helps.

Best regards,