Help with web API recording-work relationship

Hi there,

I’m new to MusicBrainz and trying to find my way round the API to find out some more information about a song that’s currently playing… for example whether there are any cover versions, remixes and whether it samples or is sampled by another song etc. I’m a bit confused about relationships and the best way to get this. From reading the API documentation, I should be able to do a query on a Recording and then get information such as Covers by including Work relationships.

I’ve tried the following query (and variants) but am not seeing any “relationship” entries returned in the JSON. Can anyone advise me as to what I’m doing wrong? Is a query via recording the best way to go or should I be doing a work query?

http://musicbrainz.org/ws/2/recording/?query=%22Right%20Here%20Right%20Now%22%20AND%20artistname:%22Fatboy%20Slim%22&inc=performance+work-rels+work-level-rels&fmt=json&limit=10

The MusicBrainz page for that recording is Right Here Right Now and it shows has-remixes, sampled by and samples.

Thanks, brainz!

inc isn’t a valid parameter for search requests. To get what you’re after, you’d have to take the results of your search, and then query each specific recording entry with the inc parameter you have set to get those details.

MusicBrainz API / Search - MusicBrainz has more details on how the API search works, and MusicBrainz API - MusicBrainz covers lookups (get info based on type and MBID) and browse (get related info i.e. get all release groups for a specific artist MBID)

1 Like

Thanks for the info, I hadn’t realised that.

(This reply is more for my own notes and in case someone else is trying to do something similar, no need to answer!)

This gives me a new challenge, which is to figure out which of the query results is the “canonical” recording that I want to then do a query with the “inc”. My original query gives me 100+ recordings (some of which are marked as compilations, some of which are compilations that aren’t marked with “secondarytype”, and some are remixes). I’ve modified the query to exclude remixes and compilations, and only include releases in UK, Europe and Worldwide.

http://musicbrainz.org/ws/2/recording/?query=%22Right%20Here%20Right%20Now%22%20AND%20artistname:%22Fatboy%20Slim%22%20%20AND%20(NOT%20recording:%22remix%22)%20AND%20(country:GB%20OR%20country:XE%20OR%20country:XW)%20AND%20(NOT%20secondarytype:Compilation)&fmt=json&limit=100

This gets me down to 16 results, which is an improvement. I then figured the best way to get the “canonical” version is to find the one with the earliest release date, which I do with the following jq:

( [ try ( ( if (."first-release-date" | length == 4) then ."first-release-date" + "-01-01" elif (."first-release-date" | length == 7) then ."first-release-date" + "-01" else ."first-release-date" end ) | strptime("%Y-%m-%d") | mktime ) catch 2524608000 ] | min ) as $first | .recordings[] | select( ."first-release-date" <= ($first | strftime("%Y-%m-%d") ) ) |  { "recordingid": .id, "releasedate" : ."first-release-date", "title": .title, "artist": ."artist-credit"[0].name }

The first section up to as $first safely converts the first-release-date to a timestamp, bearing in mind that the date could be expressed in MusicBrainz as “2026-07-31”, or “2026-07”, or “2026”; the try-catch is an extra safety so that if the date is in an unexpected format it defaults to 2524608000 (“2050-01-01”). It then finds the lowest timestamp and stores it in $first.

The second part selects the recording(s) with the earliest dates by converting the timestamp back to date and comparing it to the first-release-date, and then pulls out the relevant keys that I can use for a follow-up query using inc=work-rels.

This may be a crazy way to do things but I’m learning about the API and jq as I go; I’ll probably end up wrapping this in a bash script. I’d probably be able to do all of this a lot more easily in psql but I’m not ready to download the entire schema!